print "Howdy, world!\n"; 1
2 /
/ 3
4 / $phrase = " Howdy, world!\n"; print $phrase ; # #
/ 5
6 / $answer = 42; $pi = 3.14159265; $avocados = 6.02e23; $pet = "Camel"; $sign = "I love my $pet"; $cost = 'It costs $100'; # # # # # # $thence = $whence; # $x = $moles * $avocados; # $cwd = `pwd`; $exit = system("vi $x"); $fido = new Camel "Fido"; # # #
/ 7 $camels = '123'; print $camels + 1, "\n";
8 / @home = ("couch", "chair", "table", "stove"); ($potato, $life, $tennis, $pipe) = @home; ($alpha, $omega) = ($omega, $alpha);
/ 9 $home[0] = "couch"; $home[1] = "chair"; $home[2] = "table"; $home[3] = "stove"; [0]...Array...[n] --> pop [0]...Array...[n] <-- push
1 0 / %longday = ("Sun", "Sunday", "Mon", "Monday", "Tue", "Tuesday", "Wed", "Wednesday", "Thu", "Thursday", "Fri", "Friday", "Sat", "Saturday"); %longday = ( ); "Sun" => "Sunday", "Mon" => "Monday", "Tue" => "Tuesday", "Wed" => "Wednesday", "Thu" => "Thursday", "Fri" => "Friday", "Sat" => "Saturday",
/ 11 $wife{"adam"} = "Eve"; print "Adam's wife is ", $wife{'adam'}, ".\n";
1 2 / $e = exp(1); # 2.718281828459 Noel 25 Ben 76 Clementine 49 Norm 66 Chris 92 Doug 42 Carol 25
/ 13 Ben 12 Clementine 0 Norm 66... 1 #!/usr/bin/perl 2 3 open(grades, "grades") or die "Can't open grades: $!\n"; 4 while ($line = <GRADES>) { 5 ($student, $grade) = split(" ", $line); 6 $grades{$student}.= $grade. " "; 7 } 8 9 foreach $student (sort keys %grades) { 10 $scores = 0; 11 $total = 0; 12 @grades = split(" ", $grades{$student}); 13 foreach $grade (@grades) { 14 $total += $grade; 15 $scores++; 16 } 17 $average = $total / $scores; 18 print "$student: $grades{$student}\etaverage: $average\n"; 19 }
1 4 / % perl -e 'print "Hello, world!\n";' % perl gradation
/ 15 #!/usr/bin/perl % gradation %../bin/gradation #! /bin/sh -- # perl, to stop looping eval 'exec /usr/bin/perl -S $0 ${1+"$@"}' if 0 ;
1 6 / #!/usr/bin/perl -w
/ 17 open(sesame, "filename"); open(sesame, "<filename"); open(sesame, ">filename"); # # #
1 8 / open(sesame, ">>filename"); # open(sesame, " output-pipe-command"); # open(sesame, "input-pipe-command "); # print STDOUT "Enter a number:"; $number = <STDIN>; print STDOUT "The number is $number\n"; # # #
/ 19 chop($number = <STDIN>); # $number = <STDIN>; chop($number); # #
2 0 / $a = 123; $b = 456; print $a + $b; print $a. $b; # 579 # 123456 $a = 123; $b = 3; print $a * $b; print $a x $b; # 369 # 123123123
/ 21 print $a. 'is equal to'. $b. "\n"; print $a, 'is equal to', $b, "\n"; print "$a is equal to $b\n"; # # # print "-" x $scrwid, "\n"; $a = $b; $a = $b + 5; $a = $a * 3;
2 2 / lvalue operator= expression lvalue = lvalue operator expression $a *= 3; $line.= "\n"; $fill x= 80; $val = "2"; # $line # $fill 80 $fill # $val 2 chop($number = <STDIN>);
/ 23 $a = 5; # $a 5 $b = ++$a; # $b $a, 6 $c = $a--; # $c 6, $a 5
2 4 / $a && $b $a $b! $a $a and $b $a or $b not $a open(grades, "grades") or die "Can't open file grades: $!\n";
/ 25 -e $a -r $a -w $a -d $a -f $a -T $a
2 6 / -e "/usr/bin/perl" or warn "Perl is improperly installed\n"; -f "/vmunix" and print "Congrats, we seem to be running BSD Unix\n";
/ 27 0 # "0" 1 # "1" 10-10 # 10-10 = 0 "0" 0.00 # 0.00 = 0 "0" "0" # "0" "" # "0.00" # "0.00" "0" "0.00"+ 0 # "0.00" 0.00 00 \$a undef() # $a $a #
2 8 / if ($debug_level > 0) { # Something has gone wrong. Tell the user. print "Debug: Danger, Will Robinson, danger!\n"; print "Debug: Answer was '54', expected '42'.\n"; } if ($city eq "New York") { print "New York is northeast of Washington, D.C.\n"; } elsif ($city eq "Chicago") { print "Chicago is northwest of Washington, D.C.\n"; }
/ 29 elsif ($city eq "Miami") { print "Miami is south of Washington, D.C. And much warmer!\n"; } else { print "I don't know where $city is, sorry.\n"; } unless ($destination eq $home) { print "I'm not going home.\n"; } while ($tickets_sold < 10000) { $available = 10000 - $tickets_sold; print "$available tickets are available. How many would you like: "; $purchase = <STDIN>; chomp($purchase); $tickets_sold += $purchase; }
3 0 / print "This show is sold out, please come back later.\n"; while ($line = <GRADES>) { while (@ARGV) { process(shift @ARGV); } for ($sold = 0; $sold < 10000; $sold += $purchase) { $available = 10000 - $sold; print "$available tickets are available. How many would you like: "; $purchase = <STDIN>; chomp($purchase); }
/ 31 foreach $user (@users) { if (-f "$home{$user}/.nexrc") { print "$user is cool... they use a perl-aware vi!\n"; } } foreach $key (sort keys %hash) {
3 2 / foreach $user (@users) { if ($user eq "root" or $user eq "lp") { next; } if ($user eq "special") { print "Found the special account.\n"; # do some processing last; } } LINE: while ($line = <ARTICLE>) { last LINE if $line eq "\n"; # next LINE if $line =~ /^#/; # # your ad here }
/ 33 &
3 4 / while ($line = <FILE>) { if ($line =~ /http:/) { print $line; } } while (<FILE>) { print if /http:/; }
/ 35 while (<FILE>) { print if /http:/; print if /ftp:/; print if /mailto:/; # What next? }
3 6 / spp:fe+h20=fe02;h:2112:100:stephen P Potter:/home/spp:/bin/tcsh
/ 37 $_ = "fred xxxxxxx barney"; s/x*//;
3 8 / /\bfred\b/ next LINE if $line =~ /^#/;
/ 39 s/(\s+)\s+(\s+)/$2 $1/ @array = (1 + 2, 3-4, 5 * 6, 7 / 8); sort LIST
4 0 / sort @guys, @gals, other(); (Fred,Barney,Wilma,Betty,Dino (Barney,Betty,Dino,Fred,Wilma) print reverse sort map {lc} keys %hash;
/ 41
4 2 /