Perl language structure: Difference between revisions

Content deleted Content added
Format code
Line 219:
# cannot be predicted.
while (($name, $address) = each %addressbook) {
print "$name lives at $address\n";
}
 
# Similar to the above, but sorted alphabetically
foreach my $next_name (sort keys %addressbook) {
print "$next_name lives at $addressbook{$next_name}\n";
}
</source>
Line 323:
 
<source lang="perl">
my ($x, $y, $z) = @_;
</source>
 
Line 339:
<source lang="perl">
sub function1 {
my %args = @_;
print "'x' argument was '$args{x}'\n";
}
function1( x => 23 );
Line 369:
<source lang="perl">
sub either {
return wantarray ? (1, 2) : 'Oranges';
}
 
Line 452:
<source lang="perl">
sub Point::new {
# Here, Point->new(4, 5) will result in $class being 'Point'.
# It's a variable to support subclassing (see the perloop manpage).
my ($class, $x, $y) = @_;
bless [$x, $y], $class; # Implicit return
}
 
sub Point::distance {
my ($self, $from) = @_;
my ($dx, $dy) = ($$self[0] - $$from[0], $$self[1] - $$from[1]);
sqrt($dx * $dx + $dy * $dy);
}
</source>