PERLDATA(7) Perl Programmers Reference Guide PERLDATA(7) NAME perldata- Perl DESCRIPTION Variable names Perl " hash"0 `::' (`'') ( perlmod Packages) perlref . Perl `$' ( the perlop manpage the perlre manpage) perl perlvar '$', '$' `s' scalar@ $days # "days" $days[28] # @days29 $days{'Feb'} # %days `Feb' $#days # @days '@', "these" "those" (......) @days # ($days[0], $days[1],... $days[n]) @days[3,4,5] # ($days[3],$days[4],$days[5]) @days{'a','c'} # ($days{'a'},$days{'c'}) '%' : %days # (key1, val1, key2, val2 ...) '&', , "do" '*' , (if ever ;-) $foo @foo $foo[1] @foo , $foo. '$', '@', '%', "log" `open(LOG,'logfile')' `open(log,'logfile')'). --"FOO","Foo", "foo" perlref $% $$. ( $$ id) Context Perl Perl `fish'`sheep' int( ) integer <> , <> STDIN integer sort( ) sort<>, <>STDINsortsort `use warnings' Perl -w "void context" "fred"; ; `getpwuid(0);';. ,. perlfunc "wantarray" Scalar values Perl , ,. : , (), . , . , . . "","","". , . Perl ,,(). , ,. 0 TRUE. , . ("empty"), . ,"". ,, , , ,. Perl ,, ,the perlref manpage. defined() (),undef() . 0,0"0" (-w,). 0, awk: if ($str == 0 && $str ne "0") { warn "That doesn't look like a number"; } ,IEEE ,`NaN' . , POSIX::strtod() (perlre). warn "has nondigits" if /\D/; warn "not a natural number" unless /^\d+$/; # rejects -3 warn "not an integer" unless /^-?\d+$/; # rejects +3 warn "not an integer" unless /^[+-]?\d+$/; warn "not a decimal number" unless /^-?\d+\.?\d*$/; # rejects .2 warn "not a decimal number" unless /^-?(?:\d+(?:\.\d*)?|\.\d+)$/; warn "not a C float" unless /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/; . $#days@days. ,; ,0. $#days . , , . (Perl 4, .) (). . . : @whatever = (); $#whatever = -1; , . (,,C, .) : scalar(@whatever) == $#whatever - $[ + 1; Perl 5 $[ : $[ . (,$[ ) ,: scalar(@whatever) == $#whatever + 1; , : $element_count = scalar(@whatever); , , , , ; , , ,. Perl. , 10,000,%HASH "1/16", 16, 10,000. . , keys() . keys(%users) = 1000; # 1024 Scalar value constructors : 12345 12345.67 .23E-10 # a very small number 3.14_15_92 # a very important number 4_294_967_296 # underscore for legibility 0xff # hex 0xdead_beef # more hex 0377 # octal 0b011011 # binary (Unix 0b110_110_100) ( nibbles 0b1010_0110) . Unix shells: ; (`\'' `\\')). C perlop "Quote and Quote-like Operators" ,,(:'0xff'),. hex() oct() . perlfunc hex oct . . , (, $@, .). ``The price is $100.'' $Price = '$100'; # not interpolated print "The price is $Price.\n"; # interpolated perl double interpolation$100 shell, , . $who = "Larry"; print PASSWD "${who}::0:0:Superuser:/:/bin/perl\n"; print "We use ${who}speak when ${who}'s here.\n"; , Perl $whospeak, $who::0, $who's . who $0 $s. , , . , $days{'Feb'} $days{Feb} . . Version Strings : Version Strings (v-strings) have been deprecated. They will not be available after Perl 5.8. The marginal benefits of v-strings were greatly outweighed by the potential for Surprise and Confusion. `v1.20.300.4000' . v-strings"\x{1}\x{14}\x{12c}\x{fa0}" . Unicode , (`cmp',`gt',`lt' ). , `v' . print v9786; # prints UTF-8 encoded SMILEY, "\x{263a}" print v102.111.111; # prints "foo" print 102.111.111; # same require use ."$^V" Perl. perlvar "$^V" v-strings IPv4 Socket inet_aton()/inet_ntoa() Perl 5.8.1 v-strings (`v65') `=>' ( hash hash ) v-strings ('v65') Perl 5.6.0 Perl 5.8.0 v-strings v-strings`v65.66' 65.66.67 v-strings __FILE__, __LINE__, __PACKAGE__ ,,. ; . (`package;' ), __PACKAGE__ . ^D ^Z, __END__ __DATA__ . . __DATA__ `PACKNAME::DATA' ,`PACKNAME' __DATA__ . __DATA__ . `close DATA'. __DATA__ , __END__ __DATA__ (`require' `do' ) `main::DATA' . SelfLoader __DATA__, . BEGIN DATA: BEGIN , __DATA__ ( __END__) . . "". , , ,`use warnings' ,-w , Perl. . : use strict 'subs'; . . , `no strict 'subs'' $" ("use English;" $LIST_SEPARATOR ) $temp = join($", @ARGV); system "echo $temp"; system "echo @ARGV"; ():`/$foo[bar]/' `/${foo}[bar]/' (`[bar]' ) `/${foo[bar]}/'/ (`[bar]' @foo ) ? @foo , . @foo , Perl `[bar]' , . , , . here-document perlop "Quote and Quote-like Operators" List value constructors (,): (LIST) , , C. : @foo = ('cc', '-E', $bar); @foo, $foo = ('cc', '-E', $bar); $bar $foo. , ; 3$foo: @foo = ('cc', '-E', $bar); $foo = @foo; # $foo gets 3 , : @foo = ( 1, 2, 3, ); here-document , , : @sauces = <. , ,, . . (@foo,@bar,&SomeSub,%glarch) @foo,@bar, SomeSub , %glarch . , perlref (). . ((),(),()) (). , . ()`1,,3' `1,' 3, `1,,3' `(1,),(3)' `1,3' (`1,,,3' `(1,),(,),3' `1,3' ) . . : # Stat returns list value. $time = (stat($file))[8]; # SYNTAX ERROR HERE. $time = stat($file)[8]; # OOPS, FORGOT PARENTHESES # Find a hex digit. $hexdigit = ('a','b','c','d','e','f')[$digit-10]; # A "reverse comma operator". return (pop(@foo),pop(@foo))[0]; , : ($a, $b, $c) = (1, 2, 3); ($map{'red'}, $map{'blue'}, $map{'green'}) = (0x00f, 0x0f0, 0xf00); `undef': ($dev, $ino, undef, undef, $uid, $gid) = stat($file); , : $x = (($foo,$bar) = (3,2,1)); # set $x to 3, not 2 $x = (($foo,$bar) = f()); # set $x to f()'s return count , , 0, FALSE. $count = () = $string =~ /\d+/g; $count $string () ()$count $count = $string =~ /\d+/g; true false : ($a, $b, @rest) = split; my($a, $b, %rest) = @_; , , undefined.my() local(). : # same as map assignment above %map = ('red',0x00f,'blue',0x0f0,'green',0xf00); , . . ,. . `=>' .`=>' , , : , (`=>' ). : %map = ( red => 0x00f, blue => 0x0f0, green => 0xf00, ); : $rec = { witch => 'Mable the Merciless', cat => 'Fluffy the Ferocious', date => '10/31/1776', }; or for using call-by-named-parameter to complicated functions: $field = $query->radio_group( name => 'group_name', values => ['eenie','meenie','minie'], default => 'meenie', linebreak => 'true', labels =>\%labels ); . perlfunc "sort" Subscripts (`@') @myarray = (5, 50, 500, 5000); print "Element Number 2 is", $myarray[2], "\n"; 0 $myarray[-1] 5000,$myarray[-2] 500 Hash %scientists = ( "Newton" => "Isaac", "Einstein" => "Albert", "Darwin" => "Charles", "Feynman" => "Richard", ); print "Darwin's First Name is ", $scientists{"Darwin"}, "\n"; Slices . . $whoami = $ENV{"USER"}; # one element from the hash $parent = $ISA[0]; # one element from the array $dir = (getpwnam("daemon"))[7]; # likewise, but with list ,, . . ($him, $her) = @folks[0,-1]; # array slice @them = @folks[0 .. 3]; # array slice ($who, $home) = @ENV{"USER", "HOME"}; # hash slice ($uid, $dir) = (getpwnam("daemon"))[2,7]; # list slice , . @days[3..5] = qw/Wed Thu Fri/; @colors{'red','blue','green'} = (0xff0000, 0x0000ff, 0x00ff00); @folks[0, -1] = @folks[-1, 0]; : ($days[3], $days[4], $days[5]) = qw/Wed Thu Fri/; ($colors{'red'}, $colors{'blue'}, $colors{'green'}) = (0xff0000, 0x0000ff, 0x00ff00); ($folks[0], $folks[-1]) = ($folks[-1], $folks[0]); , `foreach' . foreach (@array[ 4 .. 10 ]) { s/peter/paul/ } foreach (@hash{qw[key1 key2]}) { s/^\s+//; # trim leading whitespace s/\s+$//; # trim trailing whitespace s/(\w+)/\u\L$1/g; # "titlecase" words } , : @a = ()[1,0]; # @a has no elements @b = (@a)[0,1]; # @b has no elements @c = (0,1)[2,3]; # @c has no elements : @a = (1)[1,0]; # @a has two elements @b = (1,undef)[1,0,2]; # @b has three elements ,: while ( ($home, $user) = (getpwent)[7,0]) { printf "%-8s %s\n", $user, $home; } , . , , 02. '@''%'. (). ('$''@')(). Typeglobs and Filehandles Perl . *, . , , . ,. : *this = *that; $this $that, @this @that,%this %that, &this &that, . . : local *Here::blue =\$There::green; $Here::blue $There::green, @Here::blue @There::green, %Here::blue %There::green, . perlmod Symbol Tables . , import/export. , . , : $fh = *STDOUT; , : $fh =\*STDOUT; perlsub . local() . , .: sub newopen { my $path = shift; local *FH; # not my! open (FH, $path) or return undef; return *FH; } $fh = newopen('/etc/passwd'); *foo{THING} , ,.*HANDLE{IO} HANDLE . , *FH; *foo{THING} . , *FH (open(), opendir(), pipe(), socketpair(), sysopen(), socket(), accept()) ,,. open(my $fh, ...) open(local $fh,...) ,. ,, sub myopen { open my $fh, "@_" or die "Can't open '@_': $!"; return $fh; } { my $f = myopen("; # $f implicitly closed here } `my $fh='zzz'; open($fh, ...)' `open( *{'zzz'}, ...)' `use strict 'refs'' Symbol IO::Handle . These modules have the advantage of not hiding different types of the same name during the local(). open() in the perlfunc manpage .(:,,,.) SEE ALSO the perlvar manpage Perlthe perlref manpage, the perlsub manpage, Symbol Tables in the perlmod manpage *foo{THING} redcandle 2001124 http://cmpp.linuxforum.net man man https://github.com/man-pages-zh/manpages- zh perl v5.8.3 2003-11-25 PERLDATA(7)