Mais conteúdo relacionado
Semelhante a Introduction to Perl
Semelhante a Introduction to Perl (20)
Introduction to Perl
- 58. # This is a hello world program print "Hello, world!"; # print
- 102. my $sum = $meaning_of_life + $number_less_than_1;
- 120. my @fruit = ('apples', 'oranges', 'guavas', 'passionfruit', 'grapes');
- 127. Note use of $ as individual element of an array is a scalar
- 130. print @fruits[1 .. 3]; # prints "oranges", "guavas", # "passionfruit"
- 131. Note use of @ as we are accessing more than one element of the array
- 149. As with arrays, notice the use of $ to indicate that we're accessing a single value
- 153. Again, note use of @ as we are accessing more than one value from the hash
- 169. If a piece of Perl code seems to be missing a variable, then it's probably using $_
- 175. my $num = @ARGV; print "$num arguments: @ARGV";
- 190. < ... > is the “read from filehandle” operator
- 209. $x++; # same as $x += 1 or $x = $x + 1 $y--; # same as $y -= 1 or $y = $y - 1
- 233. @list = ('a', 'random', 'collection', 'of', 'words'); @sorted = sort(@list); # a collection of random words
- 236. uc and lc return upper and lower case versions of a string
- 237. $string = 'MiXeD CaSe'; print "$string", uc $string, "", lc $string;
- 241. chomp removes the last character only if it is a newline and returns true or false appropriately
- 247. cos , sin , tan standard trigonometric functions
- 254. pop removes and returns the last element in an array
- 256. shift and unshift do the same for the start of an array
- 258. sort does a lot more besides, see the docs (perldoc -f sort)
- 262. @array = (1 .. 5); $string = join ', ', @array; # $string is '1, 2, 3, 4, 5'
- 263. split takes a string and converts it into an array
- 278. seek to move to a random position in a file
- 333. 1 + 2 == '3 bears' # true
- 334. 1 + 3 == 'three' # false
- 339. Only need to evaluate EXPR_2 if EXPR_1 evaluates as false
- 340. We can use this to make code easier to follow
- 349. If EXPR is true, execute BLOCK1, otherwise execute BLOCK2
- 350. if ($name eq 'Doctor') { regenerate(); } else { die "Game over!"; }
- 352. if (EXPR1) { BLOCK1 } elsif (EXPR2) { BLOCK2 } else { BLOCK3 }
- 355. if ($name eq 'Doctor') { regenerate(); } elsif ($tardis_location eq $here) { escape(); } else { die "Game over!"; }
- 367. Execute INIT If EXPR is false, exit loop, otherwise execute BLOCK, execute INCR and retest EXPR
- 369. for ($i = 1; $i <= 10; $i++) { print "$i squared is ", $i * $i, ""; }
- 374. foreach $i (1 .. 10) { print "$i squared is ", $i * $i, ""; }
- 376. my %months = (Jan => 31, Feb => 28, Mar => 31, Apr => 30, May => 31, Jun => 30, … ); foreach (keys %months) { print "$_ has $months{$_} days"; }
- 383. redo – jump to start of same iteration of loop
- 399. sub exterminate { foreach (@_) { print "Ex-Ter-Min-Ate $_"; $timelords--; } }
- 402. sub first { &second }; sub second { print @_ }; first('some', 'random', 'data');
- 405. Passing by reference passes the actual variable. Changing the argument alters the external value
- 409. Updating $arg1 and $arg2 doesn’t effect anything outside the subroutine
- 414. sub exterminate { if (rand > .25) { print "Ex-Ter-Min-Ate $_[0]"; $timelords--; return 1; } else { return; } }
- 416. sub exterminate { my @exterminated; foreach (@_) { if (rand > .25) { print "Ex-Ter-Min-Ate $_"; $timelords--; push @exterminated, $_; } } return @exterminated; }
- 439. ^ - matches start of string
- 440. $ - matches end of string
- 441. . - matches any character (except )
- 442. - matches a whitespace character
- 443. - matches a non-whitespace character
- 445. - matches any non-digit
- 446. - matches any "word" character
- 447. - matches any "non-word" character
- 448. - matches a word boundary
- 449. - matches anywhere except a word boundary
- 452. ? - match zero or one
- 453. * - match zero or more
- 454. + - match one or more
- 455. {n} - match exactly n
- 471. while (<FILE>) { if (/^(+)+(+)/) { print "The first word was $1"; print "The second word was $2"; } }
- 473. my @nums = $text =~ /(+)/g; print "I found these integers:"; print "@nums";