What if Alex has three children? Who cares about shorter if it doesn't produce the right output.
use strict; use warnings; sub plural($@) { my ($n, $s, $p) = @_; if ($n == 0) { return "no $p"; } elsif ($n == 1) { return "one $s"; } else { return "$n $p"; } } sub count_phrase { my $last = pop @_; return $last if !@_; return join(', ', @_), ' and ', $last; } sub print_person { my ($person) = @_; my $name = $person->{name}; my $age = $person->{age}; my $children = $person->{children}; if ( @$children ) { my @chd_ages = map { $_->{age} } @$children; print("$name has ", count_phrase(@$children, 'child', 'children' +), "\n"); print("The are ", phrase_list(@chd_ages), " years old\n"); } else { print("$name has no children\n"); } print("$name is $age years old\n"); } { my %person = ( name => 'Alex', age => 30, children => [ { age => 2 }, { age => 5 }, ], ); print_person(\%alex); }
In reply to Re: short method to print age.
by ikegami
in thread short method to print age.
by zouz
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |