February 2012
Intermediate to advanced
1184 pages
37h 17m
English
Here is a record with six disparate fields:
$rec = {
TEXT => $string,
SEQUENCE => [ @old_values ],
LOOKUP => { %some_table },
THATCODE => \&some_function,
THISCODE => sub { $_[0] ** $_[1] },
HANDLE => \*STDOUT,
};The TEXT field is a simple string, so you can just print it:
print $rec–>{TEXT};SEQUENCE and
LOOKUP are regular array and hash
references:
print $rec–>{SEQUENCE}[0];
$last = pop @{ $rec–>{SEQUENCE} };
print $rec–>{LOOKUP}{"key"};
($first_k, $first_v) = each %{ $rec–>{LOOKUP} };THATCODE is a named subroutine and
THISCODE is an anonymous subroutine, but
they’re invoked identically:
$that_answer = $rec–>{THATCODE}–>($arg1, $arg2);
$this_answer = $rec–>{THISCODE}–>($arg1, $arg2);With an extra pair of braces, you can treat $rec–>{
as an indirect object:HANDLE}
print { $rec–>{HANDLE} } "a string\n";If you’re using the IO::Handle module, you can even treat the handle as a regular
object:
use IO::Handle;
$rec–>{HANDLE}–>autoflush(1);
$rec–>{HANDLE}–>print("a string\n");Read now
Unlock full access