print "Howdy, world!\n"; |
perl -e "print \"Howdy, world\!\n\";" |
perl hello |
#!/usr/bin/perl untie $me; open $doors; for($ever){ join $me, 'and', $you; last or sleep and die;} map $our $future or kill my $hopes; my %hope = ('to','be','with','you'); open $doors unless $I& break; $Read= each %hope and wait; times; shift and tell; warn my $friends; warn for($they_are_not_safe); $I& sort and study time; wait for($me); close $doors and sin bind (You_and, me); $please= wait and $see; if ($you){ chop( my $words);} my %hopes& break and die ; if($you){split 'from', $me;} $I= 'cry' and die 'inside'; |
Type | Character | Example | Is a name for: |
---|---|---|---|
Scalar | $ | $cents | An individual value (number or string) |
Array | @ | @large | A list of values, keyed by number |
Hash | % | %interest | A group of values, keyed by string |
Subroutine | & | &how | A callable chunk of Perl code |
Typeglob | * | *struck | Everything named struck |
$answer = 42; # an integer $pi = 3.14159265; # a "real" number $avocados = 6.02e23; # scientific notation $pet = "Camel"; # string $sign = "I love my $pet"; # string with interpolation $cost = 'It costs $100'; # string without interpolation $thence = $whence; # another variable $x = $moles * $avocados; # an expression $cwd = `pwd`; # string output from a command $exit = system("vi $x"); # numeric status of a command $fido = new Camel "Fido"; # an object |
@home = ("couch", "chair", "table", "stove"); # initialize ($potato, $lift, $tennis, $pipe) = @home; # list ($alpha,$omega) = ($omega,$alpha); # swap |
$home[0] = "couch"; $home[1] = "chair"; $home[2] = "table"; $home[3] = "stove"; |
%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", ); |
$wife{"Adam"} = "Eve"; |
Noël 25 Ben 76 Clementine 49 Norm 66 Chris 92 Doug 42 Carol 25 Ben 12 Clementine 0 Norm 66 ...
#!/usr/bin/perl open(GRADES, "grades") or die "Can't open grades: $!\n"; while ($line = <GRADES>) { ($student, $grade) = split(" ", $line); $grades{$student} .= $grade . " "; } foreach $student (sort keys %grades) { $scores = 0; $total = 0; @grades = split(" ", $grades{$student}); foreach $grade (@grades) { $total += $grade; $scores++; } $average = $total / $scores; print "$student: $grades{$student}\tAverage: $average\n"; } |