Perl 입문

Perl                   。            ,      、 web   、     、 GUI           。            (  、  、  )      (  、  、  )。     ,              ,           ,                      。   Perl    Unix     Perl  : perl progname.pl       ,            : #!/usr/bin/env perl    /path/to/script.pl  。  ,        ,  chmod 755 script.pl(Unix )。 (          env  。         perl             ,  #!/usr/bin/perl)           ,Perl    。                   : 1. #!/usr/bin/perl 2. use strict; 3. use warnings;       perl                。         ,          。         use strict;  ,        ,            。    use warnings; ,      (       -w  )       。          Perl              。             ,     main()           。 Perl       : print "Hello, world";    (#)        : # This is a comment(      )       Perl  : 1. print 2. "Hello, world" 3. ; ……         : 1. # this would print with a linebreak in the middle 2. print "Hello 3. world";             : 1. print "Hello, world"; 2. print 'Hello, world';   ,      “  ”        (
) ( ): 1. print "Hello, $name
"; # works fine 2. print 'Hello, $name
'; # prints $name
literally : print 42; 。 。 1. print("Hello, world
"); 2. print "Hello, world
"; Perl Perl 3 : (scalars)、 (arrays) (hashes)。 * (Scalars) : 1. my $animal = "camel"; 2. my $answer = 42; 、 , Perl 。 , , my 。( use strict; ) : 1. print $animal; 2. print "The animal is $animal
"; 3. print "The square of $answer is ", $answer * $answer, "
"; “ ” 。 。 , “ ”$_。 Perl , 。 print; # prints contents of $_ by default( $_ ) * (Arrays) : 1. my @animals = ("camel", "llama", "owl"); 2. my @numbers = (23, 42, 69); 3. my @mixed = ("camel", 42, 1.23); $#array : print $mixed[$#mixed]; # last element, prints 1.23 $#array + 1 。 , 。 Perl (“ ”), @array :( :“ ” Perl , Perl ) if (@animals < 5) { ... } # : @animals 5 , $, ; , 。 : ( : , @, ( )。) 1. @animals[0,1]; # gives ("camel", "llama"); 2. @animals[0..2]; # gives ("camel", "llama", "owl"); 3. @animals[1..$#animals]; # gives all except the first element “ ”。 : 1. my @sorted = sort @animals; 2. my @backwards = reverse @numbers; , @ARGV; @_。 * / : 1. my %fruit_color = ("apple", "red", "banana", "yellow"); => : 1. my %fruit_color = ( 2. apple => "red", 3. banana => "yellow", 4. ); : 1. $fruit_color{"apple"}; # gives "red" keys() values() 。 1. my @fruits = keys %fruit_colors; 2. my @colors = values %fruit_colors; , 。 、 , 。 %ENV 。 , 。 , Perl 。 , / / 。 1. my $variables = { 2. scalar => { 3. description => "single item", 4. sigil => '$', 5. }, 6. array => { 7. description => "ordered list of items", 8. sigil => '@', 9. }, 10. hash => { 11. description => "key/value pairs", 12. sigil => '%', 13. }, 14. }; 15. 16. print "Scalars begin with a $variables->{'scalar'}->{'sigil'}
"; : 1. my $var = "value"; ,my 。 :( : , “use strict;” ) 1. $var = "value"; , , 。my , ( )。 1. my $x = "foo"; 2. my $some_condition = 1; 3. if ($some_condition) { 4. my $y = "bar"; 5. print $x; # prints "foo" 6. print $y; # prints "bar" 7. } 8. print $x; # prints "foo" 9. print $y; # prints nothing; $y has fallen out of scope my Perl use strict, 。 , , print $y 。 use strict ! Perl 。Perl 5.10 ( given/when)。 Perl 。 , 。 * if 1. if ( condition ) { 2. ... 3. } elsif ( other condition ) { 4. ... 5. } else { 6. ... 7. } : 1. unless ( condition ) { 2. ... 3. } if (!condition) 。 , , 。 , : 1. # the traditional way 2. if ($zippy) { 3. print "Yow!"; 4. } 5. 6. # the Perlish post-condition way 7. print "Yow!" if $zippy; 8. print "We have no bananas" unless $bananas; * while 1. while ( condition ) { 2. ... 3. } unless , : 1. until ( condition ) { 2. ... 3. } while: print "LA LA LA
" while 1; # loops forever * for C : 1. for ($i = 0; $i <= $max; $i++) { 2. ... 3. } Perl foreach ,C for Perl 。 * foreach 1. foreach (@array) { 2. print "This element is $_
"; 3. } 4. 5. print $list[$_] foreach 0 .. $max; 6. 7. # you don't have to use the default $_ either... 8. foreach my $key (keys %hash) { 9. print "The value of $key is $hash{$key}
"; 10. } Perl 。 , print,sort reverse。 Perl ( ) * 1. + 2. - 3. * 4. / * 1. == 2. != 3. < 4. > 5. <= 6. >= * 1. eq 2. ne 3. lt 4. gt 5. le 6. ge ( ? , Perl (99 100) (100 99 )) * 1. && and 2. || or 3. ! not * 1. = 2. . 3. x 4. .. ( ) = , : 1. $a += 1; # same as $a = $a + 1 2. $a -= 1; # same as $a = $a - 1 3. $a .= "
"; # same as $a = $a . "
"; I/O open() , : 1. open(my $in, "<", "input.txt") or die "Can't open input.txt: $!"; 2. open(my $out, ">", "output.txt") or die "Can't open output.txt: $!"; 3. open(my $log, ">>", "my.log") or die "Can't open my.log: $!"; <> 。 , ; , , 。 1. my $line = <$in>; 2. my @lines = <$in>; , “ (slurping)”。 , 。 Perl , 。 <> while : 1. while (<$in>) { # assigns each line in turn to $_ 2. print "Just read in this line: $_"; 3. } print() 。 ,print() ( : , ) 1. print STDERR "This is your final warning.
"; 2. print $out $record; 3. print $log $logmessage; , close() 。( , ,Perl 。) close $in or die "$in: $!"; Perl : * 1. if (/foo/) { ... } # true if $_ contains "foo" 2. if ($a =~ /foo/) { ... } # true if $a contains "foo" // ( , :m//) $_, =~ 。 * 1. s/foo/bar/; # replaces foo with bar in $_ 2. $a =~ s/foo/bar/; # replaces foo with bar in $a 3. $a =~ s/foo/bar/g; # replaces ALL INSTANCES of foo with bar in $a * 。 , 。 1. . (
) 2. \s ( , tab, , ...) 3. \S 4. \d (0-9) 5. \D 6. \w (a-z, A-Z, 0-9, _) 7. \W 8. [aeiou] 9. [^aeiou] 10. (foo|bar|baz) foo bar baz 11. 12. ^ 13. $ : 1. * 2. + 3. ? 4. {3} 3 5. {3,6} 3 6 6. {3,} 3 : 1. /^\d+/ 2. /^$/ , 3. /(\d\s){3}/ 3 4. ( "3 4 5 ") 5. /(a.)+/ , a 6. ( "abacadaf") 7. 8. # STDIN , : 9. while (<>) { 10. next if /^$/; 11. print; 12. } * 。 。 $1、$2…… 。 1. # a cheap and nasty way to break an email address up into parts 2. 3. if ($email =~ /([^@]+)@(.+)/) { 4. print "Username is $1
"; 5. print "Hostname is $2
"; 6. } : 1. sub logger { 2. my $logmessage = shift; 3. open my $logfile, ">>", "my.log" or die "Could not open my.log: $!"; 4. print $logfile $logmessage; 5. } , : 1. logger("We have a logger subroutine!"); shift? @_ 。shift @_。 my $logmessage = shift; , $logmessage。 @_: 1. my ($logmessage, $priority) = @_; # common( ) 2. my $logmessage = $_[0]; # uncommon, and ugly( , ) : # sub square { # my $num = shift; # my $result = $num * $num; # return $result; # } : 1. $sq = square(8); Perl Perl , Perl 。 Perl , Perl , 。 Perl Perl , , CPAN(
www.cpan.org) 。 Perl 。 Perl(www.51perl.com) , :www.51perl.com/perl-brief-introduction/ , , , 。 :http://perldoc.perl.org/perlintro.html http://www.51perl.com/perl-brief-introduction/

좋은 웹페이지 즐겨찾기