Download presentation
Presentation is loading. Please wait.
Published byTrent Asby Modified over 9 years ago
1
Everything You Ever Wanted To Know About $# But Couldn’t Google November 13 th, 2014 – Houston Perl Mongers Robert Stone HostGator.com
2
Which Sigil Are We Talking About? - $# commit 8d063cd8450e59ea1c611a2f4f5a21059a2804f1 Author: Larry Wall Date: Fri Dec 18 00:00:00 1987 +0000 a "replacement" for awk and sed [ Perl is kind of designed to make awk and sed semi-obsolete. This posting will include the first 10 patches after the main source. The following description is lifted from Larry's manpage. --r$ ] Perl is a interpreted language optimized for scanning arbitrary text files, extracting information from those text files, and printing reports based on that information. It's also a good language for many system management tasks. The language is intended to be practical (easy to use, efficient, complete) rather than beautiful (tiny, elegant, minimal). It combines (in the author's opinion, anyway) some of the best features of C, sed, awk, and sh, so people familiar with those languages should have little difficulty with it. (Language historians will also note some vestiges of csh, Pascal, and even BASIC-PLUS.) Expression syntax corresponds quite closely to C expression syntax. If you have a problem that would ordinarily use sed or awk or sh, but it exceeds their capabilities or must run a little faster, and you don't want to write the silly thing in C, then perl may be for you. There are also translators to turn your sed and awk scripts into perl scripts. Initial Perl Commit Stand Alone $# Introduced in the Initial Perl 1.0 commit
3
Which Sigil Are We Talking About? - $#.Ip $# 8 The output format for printed numbers. This variable is a half-hearted attempt to emulate awk's OFMT variable. There are times, however, when awk and perl have differing notions of what is in fact numeric. Also, the initial value is %.20g rather than %.6g, so you need to set $# explicitly to get awk's value. (Mnemonic: # is the number sign.) perl.man.2 Stand Alone $# Introduced in the Initial Perl 1.0 commit Equivalent to $OFMT “a half-hearted attempt to emulate awk’s OFMT variable”
4
Which Sigil Are We Talking About? - $# [drzigman@lion ~]$ perl -le '$#="%.2f"; print 1/7;' 0.14 [drzigman@lion ~]$ perl -le '$#="%.3f"; print 1/7;' 0.143 [drzigman@lion ~]$ perl -le '$#="%.4f"; print 1/7;' 0.1429 [drzigman@lion ~]$ perl -le '$#="%.5f"; print 1/7;' 0.14286 Stand Alone $# Introduced in the Initial Perl 1.0 commit Equivalent to $OFMT “a half-hearted attempt to emulate awk’s OFMT variable” OFMT is similar to the sprintf placeholder Used for setting the default number formatting. Place Holder DescriptionOutput of 3.14159 Output of 12 %dDec Signed Int3900417612 %oOct Signed Int6501402014 %xHex Signed IntC23D7810 %fFloating Point3.14159012.000000 %sStringSEG FAULT“” (nothing)
5
Which Sigil Are We Talking About? - $# Stand Alone $# Introduced in the Initial Perl 1.0 commit Equivalent to $OFMT “a half-hearted attempt to emulate awk’s OFMT variable” OFMT is similar to the sprintf placeholder Used for setting the default number formatting. Deprecated in Perl 5 Obsoleted in Perl 5.10.0 [drzigman@lion ~]$ perlbrew use perl-5.8.9 [drzigman@lion ~]$ perl -wle '$#="%.5f"; print 1/7;' Use of $# is deprecated at -e line 1. 0.14286 [drzigman@lion ~]$ [drzigman@lion ~]$ perlbrew use perl-5.10.1 [drzigman@lion ~]$ perl -wle '$#="%.5f"; print 1/7;' $# is no longer supported at -e line 1. 0.142857142857143 [drzigman@lion ~]$
6
There is more!?! - $#array $#array is fundamentally different from $# Read as “The last index of array” How does $#array work? Returns the last index of the array Return -1 if the array is empty #!/usr/bin/env perl my @array = ( 'apple', 'orange', 'banana' ); print "Last Index is: ". $#array. "\n"; [drzigman@lion ~]$ perl last_index_of_array3.pl Last Index is: 2 #!/usr/bin/env perl my @array = ( ); print "Last Index is: ". $#array. "\n"; [drzigman@lion ~]$ perl last_index_of_array0.pl Last Index is: -1
7
There is more!?! - $#array - Usage How is it Used? Iterate through elements by index Get the last element of an array It can also be used with ArrayRefs #!/usr/bin/env perl my @array = ( 'apple', 'orange', 'banana' ); for my $index ( 0.. $#array ) { print "At Index $index is “. $array[$index]. "\n"; } [drzigman@lion ~]$ perl iterate_by_index.pl At Index 0 is apple At Index 1 is orange At Index 2 is banana #!/usr/bin/env perl my @array = ( 'apple', 'orange', 'banana' ); print "Last Element Is: “. $array[ $#array ]. "\n"; [drzigman@lion ~]$ perl last_element.pl Last Element Is: banana #!/usr/bin/env perl my $array = [ 'apple', 'orange', 'banana' ]; print "Last Element Is: “. $array->[ $#{$array} ]. "\n"; [drzigman@lion ~]$ perl last_element_of_ref.pl Last Element Is: banana
8
Something is Rotten in the State of Denmark! – Code Smell Is it’s usage a Code Smell? Arguments For Code Smell Status Not obviously documented Found in perldata Challenging to Research http://symbolhound.com Returns -1 on empty array Usage can be surprising Getting the last element of an array Arguments Against Code Smell Status A developer only needs to learn it once This is a core tenet of Perl Usage can be cleaner then other methods C Style for loop What do you think?
9
References and Further Reading Obsolete Documentation for $# http://search.cpan.org/~andyd/perl5.003_07/pod/perlvar.pod OFMT in awk https://www.gnu.org/software/gawk/manual/html_node/OFMT.html Obsoletion of $# Notice http://search.cpan.org/~dapm/perl- 5.10.1/pod/perl5100delta.pod#The_%24%2a_and_%24%23_variables_have_bee n_removed http://search.cpan.org/~dapm/perl- 5.10.1/pod/perl5100delta.pod#The_%24%2a_and_%24%23_variables_have_bee n_removed The first Perl commit http://perl5.git.perl.org/perl.git/commit/8d063cd8450e59ea1c611a2f4f5a21059a Documentation for $#array http://perldoc.perl.org/perldata.html#Variable-names
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.