[ILUG] Perl Problem
Gavin McCullagh
gavin at fiachra.ucd.ie
Thu Feb 7 19:10:12 GMT 2002
Hi
the piece of code below is taken directly from the O'Reilly Perl
Cookbook (4.11. Processing Multiple Elements of an Array) with the
exception of the print statements which I have in to see what's going on.
However, it doesn't work! I'm starting to wonder if something's changed
in Perl or if this is a misprint in the book. Apparently it's 1st Edition
August 1998.
gavin at fiachra rho_0.51> ./test.pl
Can't use string ("Peter") as an ARRAY ref while "strict refs" in use at
./test.pl line 22.
If I comment out the use strict (just to see) I get.
gavin at fiachra rho_0.51> ./test.pl
|
Peter Paul Mary Jim Tim
Modification of non-creatable array value attempted, subscript -2 at
./test.pl line 26.
Here's the code:
#!/usr/bin/perl
use strict;
my @friends = qw(Peter Paul Mary Jim Tim);
(my $this,my $that) = shift2(@friends);
# $this contains Peter, $that has Paul, and
# @friends has Mary, Jim, and Tim
print "$this | $that\n";
print "@friends\n";
my @beverages = qw(Dew Jolt Cola Sprite Fresca);
my @pair = pop2(@beverages);
# $pair[0] contains Sprite, $pair[1] has Fresca,
# and @beverages has (Dew, Jolt, Cola)
print "@pair\n";
print "@beverages\n";
sub shift2 (\@) {
return splice(@{$_[0]}, 0, 2);
}
sub pop2 (\@) {
return splice(@{$_[0]}, -2);
}
Can anyone help?
Gavin
More information about the ILUG
mailing list