Mais conteúdo relacionado Semelhante a Beginning Perl (20) Beginning Perl1. Introduction to Perl An Introduction to Perl Programming Dave Cross Magnum Solutions Ltd [email_address] 55. # This is a hello world program print "Hello, world!"; # print 80. Add int to a floating point number my $sum = $meaning_of_life + $number_less_than_1; 81. Putting a number into a string print "$name says, 'The meaning of life is $sum.'"; 84. Use a backslash to escape special characters in double quoted strings print "He said amp;quot;The price is 300amp;quot;"; 86. This is a tidier alternative print qq(He said "The price is 300"); 87. Also works for single quotes print q(He said "That's too expensive"); 92. my @fruit = ('apples', 'oranges', 'guavas', 'passionfruit', 'grapes'); 99. Note use of $ as individual element of an array is a scalar 102. print @fruits[1 .. 3]; # prints "oranges", "guavas", # "passionfruit" 103. Note use of @ as we are accessing more than one element of the array 117. As with arrays, notice the use of $ to indicate that we're accessing a single value 119. Returns a list of elements from a hash print @french{'one','two','three'}; # prints "un", "deux" & "trois" 120. Again, note use of @ as we are accessing more than one value from the hash 136. If a piece of Perl code seems to be missing a variable, then it's probably using $_ 143. my $num = @ARGV; print "$num arguments: @ARGV"; 145. Contains the environment variables that your script has access to. 146. Keys are the variable names Values are the… well… values! 156. < ... > is the “read from filehandle” operator 157. STDIN is the standard input filehandle 166. $speed = $distance / $time; $vol = $length * $breadth * $height; $area = $pi * ($radius ** 2); $odd = $number % 2; 169. Even shorter $x++; # same as $x += 1 or $x = $x + 1 $y--; # same as $y -= 1 or $y = $y - 1 188. @list = ('a', 'random', 'collection', 'of', 'words'); @sorted = sort(@list); # a collection of random words 190. uc and lc return upper and lower case versions of a string $string = 'MiXeD CaSe'; print "$string", uc $string, "", lc $string; 193. chomp removes the last character only if it is a newline and returns true or false appropriately 195. Unlike many other languages you can assign to a substring substr($string, 0, 5) = 'Greetings'; print $string; # prints 'Greetings world' 197. cos , sin , tan standard trigonometric functions 203. pop removes and returns the last element in an array $value = pop @array; 204. shift and unshift do the same for the start of an array 206. sort does a lot more besides, see the docs (perldoc -f sort) 209. split takes a string and converts it into an array $string = '1~2~3~4~5'; @array = split(/~/, $string); # @array is (1, 2, 3, 4, 5) 215. You can then read the file with <FILE> $line = <FILE>; # one line @lines = <FILE>; # all lines 218. seek to move to a random position in a file seek(FILE, 0, 0); 219. tell to get current file position $where = tell FILE; 220. truncate to truncate file to given size truncate FILE, $where; 251. Is one greater than another $x > $y or $x gt $y $x >= $y or $x ge $y 252. Also < ( lt ) and <= ( le ) 257. 1 + 2 == '3 bears' # true 258. 1 + 3 == 'three' # false 265. We can use this to make code easier to follow open FILE, 'something.dat' or die "Can't open file: $!"; 271. If EXPR is true, execute BLOCK1, otherwise execute BLOCK2 272. if ($name eq 'Doctor') { regenerate(); } else { die "Game over!"; } 274. If EXPR1 is true, execute BLOCK1 else if EXPR2 is true, execute BLOCK2 otherwise execute BLOCK3 277. Repeat BLOCK while EXPR is true while ($dalek_prisoners) { print "Ex-ter-min-ate"; $dalek_prisoners--; } 279. Execute BLOCK until EXPR is true until ($regenerations == 12) { print "Regenerating"; regenerate(); $regenerations++; } 282. Execute INIT If EXPR is false, exit loop, otherwise execute BLOCK, execute INCR and retest EXPR 287. foreach $i (1 .. 10) { print "$i squared is ", $i * $i, ""; } 291. This is the same as while (defined($_ = <STDIN>)) { print $_; } 294. redo - jump to start of same iteration of loop 308. sub exterminate { my ($name) = @_; print "Ex-Ter-Min-Ate $name"; $timelords--; } 310. sub exterminate { foreach (@_) { print "Ex-Ter-Min-Ate $_"; $timelords--; } } 312. &my_sub passes on the contents of @_ to the called subroutine sub first { &second }; sub second { print @_ }; first('some', 'random', 'data'); 315. Passing by value passes the actual variable. Changing the argument alters the external value 319. Updating $arg1 and $arg2 doesn’t effect anything outside the subroutine 347. ^ - matches start of string 348. $ - matches end of string 349. . - matches any character (except ) 350. - matches a whitespace character 351. - matches a non-whitespace character 353. - matches any non-digit 354. - matches any "word" character 355. - matches any "non-word" character 356. - matches a word boundary 357. - matches anywhere except a word boundary 360. ? - match zero or one 361. * - match zero or more 362. + - match one or more 363. {n} - match exactly n 369. Use - to define a contiguous set 371. Use ^ to match inverse set 378. The captured parts are in $1, $2, etc… while (<FILE>) { if (/^(+)+(+)/) { print "The first word was $1"; print "The second word was $2"; } } 380. my @nums = $text =~ /(+)/g; print "I found these integers:"; print "@nums"; 390. Can now treat it just like any other scalar $var = $scalar_ref; $refs[0] = $array_ref; $another_ref = $refs[0]; 392. { LIST } creates anonymous hash and returns a reference $href = { 1 => 'one', 2 => 'two' }; $href = { %hash }; 410. Instead of ${$aref}[0] you can use $aref->[0] 411. Instead of ${$href}{key} you can use $href->{key} 413. $aref = [ 1, 2, 3 ]; print ref $aref; # prints ARRAY 414. $href = { 1 => 'one', 2 => 'two' }; print ref $href; # prints HASH 418. @arr1 = (1, 2, 3); @arr2 = (4, 5, 6); check_size(@arr1, @arr2); sub check_size { my (@a1, @a2) = @_; print @a1 == @a2 ? 'Yes' : 'No'; } 427. @arr_2d contains (1, 2, 3, 4, 5, 6, 7, 8, 9) 447. my @records; my @cols = ('s_name', 'f_name', 'job'); while (<FILE>) { chomp; my %rec; @rec{@cols} = split /,/; push @records, rec; } 448. Using an Array of Hashes foreach (@records) { print "$_->{f_name} ", "$_->{s_name} ". "is a $_->{job}"; } 565. Perl in a Nutshell Siever, Spainhour & Patwardhan (O'Reilly)