# Example 1.
# @now will be (40, 51, 20, 9, 0, 109, 5, 8, 0)
@now = localtime();
# Example 2.
# $now will be "Fri Jan 9 20:51:40 2009"
$now = localtime();
####
perl -e "@now = localtime(); for $now(@now) {print $now;}"
432351301092120
perl -e "$now = localtime(); print $now;"
Tue Jan 13 05:26:30 2009
##
##
# Example 3.
# $sec will be 40, $min will be 51, $hr will be 20
($sec,$min,$hr) = localtime();
##
##
my ($x) = localtime(); # Example 4
##
##
my $x = localtime(); # Example 5
##
##
# BAD! [Example 6]
my %h = ( now => localtime() );
##
##
# GOOD [Example 7]
my %h = ( now => scalar localtime() );
##
##
# GOOD [Example 8]
my %h = ( now => [ localtime() ] );
##
##
# Example 9
sub print_time {
my $now = shift @_;
print "The time is now $now\n";
}
# prints "The time is now 40"
print_time( localtime() );
# prints "The time is now Tue Jan 13 05:26:30 2009"
print_time( scalar localtime() );
##
##
# Example 10
my @loc = ($offset, $length);
my $part = substr( $string, @loc );
##
##
print clock(); # Example 11
sub clock {
return localtime();
}
##
##
print scalar localtime(); # Example 12
##
##
my $match_count = ()= /x/g; # Example 13
##
##
# Example 14
while (<>) {
ponder( $_ ); # void context
}
##
##
# Example 15
sub print_context {
if ( wantarray() ) {
print "list\n";
}
elsif ( defined wantarray() ) {
print "scalar\n";
}
else {
print "void\n";
}
}
print_context();
()= print_context();
scalar print_context();
__END__
void
list
scalar
##
##
# Example 16
my $beer_inventory = '99 bottles';
print "how much beer? $beer_inventory\n";
$beer_inventory--; # take one down...
print "how much beer? $beer_inventory\n";
__END__
how much beer? 99 bottles
how much beer? 98
##
##
# Example 17
my $s = "12 monkeys";
my $n = 31337;
my $stringified = ''. $n; # "31337"
my $numified = 0+ $s; # 12
my $boolean = !! $n; # 1
##
##
# Example 18
my $scaley = 'snake';
my @listy = $scaley;
# does the same thing:
#my @listy = ('snake');
##
##
# Example 19
my @t = ( 'one', 'two' );
my $x = ( 'one', 'two' ); # 'two'
my $y = @t[0,1]; # 'two'
my $z = ( 'larry', 'moe', @t[0,1] ); # 'two'
my $r = ( 'larry', 'moe', @t ); # 2
my $f = ( 'old', 'man', localtime() ); # "Fri Jan 9 20:51:40 2009"
##
##
# Example 20
my $friend = 'Perl';
my $literal = '$friend'; # literally, '$friend'
my $expanded = "$friend"; # literally, 'Perl'