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

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

Thread Next
From:
daniel
Date:
July 11, 2001 23:27
Subject:
[ID 20010711.005] in Tie::Array, SPLICE ignores context, breakingSHIFT
Message ID:
200107120625.f6C6PkJ13065@biz.bitpusher.com

This is a bug report for perl from daniel@bitpusher.com,
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.)

Background:
----------

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.)

Solution:
--------

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
 daniel@bitpusher.com

[Please do not change anything below this line]
-----------------------------------------------------------------
---
Flags:
    category=library
    severity=medium
---
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:
  Platform:
    osname=linux, osvers=2.4.2-2, archname=i686-linux
    uname='linux biz.bitpusher.com 2.4.2-2 #1 sun apr 8 20:41:30 edt 2001 i686 unknown '
    config_args='-der'
    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
  Compiler:
    cc='cc', ccflags ='-fno-strict-aliasing -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
    optimize='-O2',
    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/libc-2.2.2.so, 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:
    /usr/local/lib/perl5/5.6.1/i686-linux
    /usr/local/lib/perl5/5.6.1
    /usr/local/lib/perl5/site_perl/5.6.1/i686-linux
    /usr/local/lib/perl5/site_perl/5.6.1
    /usr/local/lib/perl5/site_perl
    .

---
Environment for perl v5.6.1:
    HOME=/home/daniel
    LANG=en_US
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=/usr/kerberos/bin:/usr/bin:/bin:/usr/X11R6/bin:/usr/local/bin:/opt/bin:/home/daniel/bin:/home/daniel/bin
    PERL_BADLANG (unset)
    SHELL=/bin/bash


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About