develooper Front page | perl.perl5.porters | Postings from July 2001

[ID 20010711.005] in Tie::Array, SPLICE ignores context, breakingSHIFT

Thread Next
July 11, 2001 23:27
[ID 20010711.005] in Tie::Array, SPLICE ignores context, breakingSHIFT
Message ID:

This is a bug report for perl from,
generated with the help of perlbug 1.33 running under perl v5.6.1.

[Please enter your report here]

(This bug report includes a complete description and a fix.)


In normal (non-tied) arrays, splice returns the last value
removed from the array if called in a scalar context.  E.g.:

@a = ('red', 'green', 'blue');
$foo = splice @a, 1, 2;

results in $foo being 'blue'.

Direct Symptom/Bug Description:

In Tie::Array's implementation, context is ignored and it
always returns a list value.  This produces very wrong
behavior if called in a scalar context, since it yields
the number of values returned:

use MyTiedArray; # a class which inherits SPLICE from Tie::Array
tie @a, 'MyTiedArray';
@a = ('red', 'green', 'blue);
$foo = splice @a, 1, 2;

result in $foo being 2, because splice got its value back from
Tie::Array's SPLICE which returned the array ('green', 'blue'),

Important Consequence of Bug:

A prominent consequence is the way in which this bug in SPLICE
affects shift.  Specifically, shift returns 1 for and non-empty
array.  E.g.:

use MyTiedArray; # inherits both SPLICE and SHIFT from Tie::Array
tie @a, 'MyTiedArray';
@a = ('red', 'green', 'blue);
$foo = shift @a;

and voila, $foo is 1.  (@foo = shift @a doesn't help because
the shift already forced scalar context on the result from SPLICE.)


Interestingly, as of 5.6.1 there is a commented out version of
sub SHIFT in Tie::Array (line 14) which makes shift work correctly.
It is not the correct solution, however.  The correct solution is
to fix SPLICE.

Specifically, the return from SPLICE (line 73 of Tie::Array) should
be changed to:

return wantarray ? @result : pop @result;

(instead of just return @result).

Doing this causes tied arrays inheriting SPLICE from Tie::Array to
handle splice the same way as non-tied arrays, as they should.

-Daniel Lieberman

[Please do not change anything below this line]
Site configuration information for perl v5.6.1:

Configured by daniel at Wed Jul 11 21:55:44 PDT 2001.

Summary of my perl5 (revision 5.0 version 6 subversion 1) configuration:
    osname=linux, osvers=2.4.2-2, archname=i686-linux
    uname='linux 2.4.2-2 #1 sun apr 8 20:41:30 edt 2001 i686 unknown '
    hint=previous, useposix=true, d_sigaction=define
    usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef
    useperlio=undef d_sfio=undef uselargefiles=define usesocks=undef
    use64bitint=undef use64bitall=undef uselongdouble=undef
    cc='cc', ccflags ='-fno-strict-aliasing -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
    cppflags='-fno-strict-aliasing -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64'
    ccversion='', gccversion='2.96 20000731 (Red Hat Linux 7.1 2.96-81)', gccosandvers=''
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
    alignbytes=4, usemymalloc=n, prototype=define
  Linker and Libraries:
    ld='cc', ldflags =' -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib
    libs=-lnsl -lndbm -lgdbm -ldl -lm -lc -lcrypt -lutil
    perllibs=-lnsl -ldl -lm -lc -lcrypt -lutil
    libc=/lib/, so=so, useshrplib=false, libperl=libperl.a
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynamic'
    cccdlflags='-fpic', lddlflags='-shared -L/usr/local/lib'

Locally applied patches:

@INC for perl v5.6.1:

Environment for perl v5.6.1:
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PERL_BADLANG (unset)

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About