[thelist] Perl references
Beam
sbeam at syxyz.net
Wed Feb 19 10:01:00 CST 2003
I suspect that it's my failure to grasp referencing
> implementation in Perl, but I would appreciate *any* help here...
>
ok here goes:
rule #1: put the "-w" flag at the end of the #! line on every perl script you
write.
rule #2: put 'use strict' at the top of every perl script
if you don't follow these 2 rules you will quickly go insane, if you havent
already.
Also know that refs in perl are accessed using scalar syntax, and
de-referenced via the -> operator. Or something. See chapter 4 of the camel.
for ($i=0;$i<$#week;$i++) { ## $#arrname gives the index value
$week[$i]{DATE} = [ @date ]; ## this creates a ref to a new, anonymous
# array, that is (for now) identical to @date
my $date_ref = \@date; ## you really want a ref to the original array
$week[$i]{DATE} = &incrementDate($date_ref, 1);
## you could do the above 2 in one line
}
## this gets ONLY the ref to the array @date, which you access like so:
sub incrementDate {
my $arr_ref = shift; ## my means this var is NOT visible outside this block
$arr_ref->[3]++; ## de-reference to get at the 3rd elem of the arr
return $arr_ref;
}
More information about the thelist
mailing list