beginners@perl.org:
I'm working on some classes with attributes that are array and hash
references, and am confused by what happens when I attempt to slice an
array or hash reference. For example:
7. "$ra->[0, 1, 2]" evaluates to $ra->[2].
8. "$ra->[0 .. 2]" produces two "Use of uninitialized value in range
(or flip)" warnings and evaluates to $ra->[1].
I see similar results with functions that return array references:
30. fra->[0, 1, 2]
31. fra->[0 .. 2]
And object methods:
42. $oa->ra->[0, 1, 2]
43. $oa->ra->[0, 1, 2]
Any suggestions?
TIA,
David
2011-12-18 17:35:52 dpchrist@p43400e ~/sandbox/perl
$ cat /etc/debian_version
6.0.3
2011-12-18 17:36:17 dpchrist@p43400e ~/sandbox/perl
$ perl -v
This is perl, v5.10.1 (*) built for i486-linux-gnu-thread-multi
(with 53 registered patches, see perl -V for more detail)
Copyright 1987-2009, Larry Wall
Perl may be copied only under the terms of either the Artistic License
or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
2011-12-18 17:36:20 dpchrist@p43400e ~/sandbox/perl
$ cat slice.pl
#!/usr/bin/perl
# $Id: slice.pl,v 1.2 2011-12-19 01:35:17 dpchrist Exp $
use strict;
use warnings;
$| = 1;
package MyArray;
sub new { my $class = shift; return bless [@_], $class }
sub a { my $self = shift; return @$self }
sub ra { my $self = shift; return [@$self] }
package main;
use Array::Utils ':all';
use Data::Dumper;
$Data::Dumper::Indent = 0;
use Test::More tests => 45;
### sources
my @a = qw( a b c d e f );
### references to, and copies of, sources
my $ra = \@a;
my $oa = MyArray->new(@a);
sub fa { @a };
sub fra { $ra };
### test expected values
my $a = [qw( a )];
my $abc = [qw( a b c )];
my $abcdef = [qw( a b c d e f )];
my $rxA = 'ARRAY\(0x[0-9a-g]{7}\)';
my @code_exp = (
'@a', $abcdef, # 1
'$a[0]', $a, # 2
'@a[0, 1, 2]', $abc, # 3
'@a[0 .. 2]', $abc, # 4
'@{$ra}', $abcdef, # 5
'$ra->[0]', $a, # 6
'$ra->[0, 1, 2]', $abc, # 7
'$ra->[0 .. 2]', $abc, # 8
'(@{$ra})[0, 1, 2]', $abc, # 9
'(@{$ra})[0 .. 2]', $abc, # 10
'@$ra', $abcdef, # 11
'(@$ra)[0, 1, 2]', $abc, # 12
'(@$ra)[0 .. 2]', $abc, # 13
'&fa()', $abcdef, # 14
'&fa', $abcdef, # 15
'fa()', $abcdef, # 16
'fa', $abcdef, # 17
'(fa)[0]', $a, # 18
'(fa)[0, 1, 2]', $abc, # 19
'(fa)[0 .. 2]', $abc, # 20
'&fra()', $rxA, # 21
'&fra', $rxA, # 22
'fra()', $rxA, # 23
'fra', $rxA, # 24
'@{&fra()}', $abcdef, # 25
'@{&fra}', $abcdef, # 26
'@{fra()}', $abcdef, # 27
'@{fra}', $abcdef, # 28
'fra->[0]', $a, # 29
'fra->[0, 1, 2]', $abc, # 30
'fra->[0 .. 2]', $abc, # 31
'(@{&fra()})[0, 1, 2]', $abc, # 32
'(@{&fra})[0, 1, 2]', $abc, # 33
'(@{fra()})[0, 1, 2]', $abc, # 34
'(@{fra})[0, 1, 2]', $abc, # 35
'$oa->a', $abcdef, # 36
'($oa->a)[0]', $a, # 37
'($oa->a)[0, 1, 2]', $abc, # 38
'($oa->a)[0 .. 2]', $abc, # 39
'$oa->ra', $rxA, # 40
'$oa->ra->[0]', $a, # 41
'$oa->ra->[0, 1, 2]', $abc, # 42
'$oa->ra->[0 .. 2]', $abc, # 43
'(@{$oa->ra})[0, 1, 2]', $abc, # 44
'(@{$oa->ra})[0 .. 2]', $abc, # 45
);
while (@code_exp) {
my $code = shift @code_exp;
my $exp = shift @code_exp;
my $got = [ eval $code ];
my $ok = (ref($exp) eq 'ARRAY')
? !array_diff(@$got, @$exp)
: $got =~ qr/$exp/;
my $msg = sprintf("%-20s %s",
$code,
( ref($exp) eq 'ARRAY'
? join(' ', @$exp)
: 'qr/' . $exp . '/'
),
);
ok($ok, $msg) or warn Data::Dumper->Dump([$got], [qw(got)]);
}
2011-12-18 17:36:28 dpchrist@p43400e ~/sandbox/perl
$ perl slice.pl
1..45
ok 1 - @a a b c d e f
ok 2 - $a[0] a
ok 3 - @a[0, 1, 2] a b c
ok 4 - @a[0 .. 2] a b c
ok 5 - @{$ra} a b c d e f
ok 6 - $ra->[0] a
not ok 7 - $ra->[0, 1, 2] a b c
# Failed test '$ra->[0, 1, 2] a b c'
# at slice.pl line 105.
$got = ['c']; at slice.pl line 105.
Use of uninitialized value in range (or flip) at (eval 11) line 1.
Use of uninitialized value in range (or flop) at (eval 11) line 1.
not ok 8 - $ra->[0 .. 2] a b c
# Failed test '$ra->[0 .. 2] a b c'
# at slice.pl line 105.
$got = ['b']; at slice.pl line 105.
ok 9 - (@{$ra})[0, 1, 2] a b c
ok 10 - (@{$ra})[0 .. 2] a b c
ok 11 - @$ra a b c d e f
ok 12 - (@$ra)[0, 1, 2] a b c
ok 13 - (@$ra)[0 .. 2] a b c
ok 14 - &fa() a b c d e f
ok 15 - &fa a b c d e f
ok 16 - fa() a b c d e f
ok 17 - fa a b c d e f
ok 18 - (fa)[0] a
ok 19 - (fa)[0, 1, 2] a b c
ok 20 - (fa)[0 .. 2] a b c
ok 21 - &fra() qr/ARRAY\(0x[0-9a-g]{7}\)/
ok 22 - &fra qr/ARRAY\(0x[0-9a-g]{7}\)/
ok 23 - fra() qr/ARRAY\(0x[0-9a-g]{7}\)/
ok 24 - fra qr/ARRAY\(0x[0-9a-g]{7}\)/
ok 25 - @{&fra()} a b c d e f
ok 26 - @{&fra} a b c d e f
ok 27 - @{fra()} a b c d e f
Ambiguous use of @{fra} resolved to @fra at (eval 31) line 1.
Variable "@fra" is not imported at (eval 31) line 2.
(Did you mean &fra instead?)
not ok 28 - @{fra} a b c d e f
# Failed test '@{fra} a b c d e f'
# at slice.pl line 105.
$got = [undef]; at slice.pl line 105.
ok 29 - fra->[0] a
not ok 30 - fra->[0, 1, 2] a b c
# Failed test 'fra->[0, 1, 2] a b c'
# at slice.pl line 105.
$got = ['c']; at slice.pl line 105.
Use of uninitialized value in range (or flip) at (eval 34) line 1.
Use of uninitialized value in range (or flop) at (eval 34) line 1.
not ok 31 - fra->[0 .. 2] a b c
# Failed test 'fra->[0 .. 2] a b c'
# at slice.pl line 105.
$got = ['b']; at slice.pl line 105.
ok 32 - (@{&fra()})[0, 1, 2] a b c
ok 33 - (@{&fra})[0, 1, 2] a b c
ok 34 - (@{fra()})[0, 1, 2] a b c
Ambiguous use of @{fra} resolved to @fra at (eval 38) line 1.
Variable "@fra" is not imported at (eval 38) line 1.
(Did you mean &fra instead?)
not ok 35 - (@{fra})[0, 1, 2] a b c
# Failed test '(@{fra})[0, 1, 2] a b c'
# at slice.pl line 105.
$got = [undef]; at slice.pl line 105.
ok 36 - $oa->a a b c d e f
ok 37 - ($oa->a)[0] a
ok 38 - ($oa->a)[0, 1, 2] a b c
ok 39 - ($oa->a)[0 .. 2] a b c
ok 40 - $oa->ra qr/ARRAY\(0x[0-9a-g]{7}\)/
ok 41 - $oa->ra->[0] a
not ok 42 - $oa->ra->[0, 1, 2] a b c
# Failed test '$oa->ra->[0, 1, 2] a b c'
# at slice.pl line 105.
$got = ['c']; at slice.pl line 105.
Use of uninitialized value in range (or flip) at (eval 46) line 1.
Use of uninitialized value in range (or flop) at (eval 46) line 1.
not ok 43 - $oa->ra->[0 .. 2] a b c
# Failed test '$oa->ra->[0 .. 2] a b c'
# at slice.pl line 105.
$got = ['b']; at slice.pl line 105.
ok 44 - (@{$oa->ra})[0, 1, 2] a b c
ok 45 - (@{$oa->ra})[0 .. 2] a b c
# Looks like you failed 8 tests of 45.
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/