Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tied SPLICE unshift(@x, shift(@x)) doesn't work #6553

Closed
p5pRT opened this issue Jun 4, 2003 · 4 comments
Closed

tied SPLICE unshift(@x, shift(@x)) doesn't work #6553

p5pRT opened this issue Jun 4, 2003 · 4 comments

Comments

@p5pRT
Copy link
Collaborator

@p5pRT p5pRT commented Jun 4, 2003

Migrated from rt.perl.org#22571 (status was 'rejected')

Searchable as RT22571$

@p5pRT
Copy link
Collaborator Author

@p5pRT p5pRT commented Jun 4, 2003

From @muir

Created by @muir

With a tied array,

  shift(@​x, unshift(@​x))

replaces the first element with '1'.

There is a similar breakage with
 
  push(@​x, shift(@​x))

This is probably related to perlbug #22570.

------------ cut here ----------------

use strict;

print "1..1\n";

my $x = [ 'a', 'b', 'c', 'd', 'e' ];

tie @​$x, 'OverArray', $x;

unshift(@​$x, shift(@​$x));

#print "x = @​$x\n";

print ($x->[0] eq 'a' ? "ok 1\n" : "not ok 1\n");

package OverArray;

sub UNTIE
{
}

sub DESTROY
{
}

sub TIEARRAY
{
  my $pkg = shift;
  my $orig = shift;
  my $self = bless [ [ @​$orig ], $orig ], $pkg;
  return $self;
}

sub FETCH
{
  my $self = shift;
  my ($fake, $real) = @​$self;
  my $index = shift;
  return $fake->[$index];
}

sub STORE
{
  my $self = shift;
  my ($fake, $real) = @​$self;
  my ($index, $value) = @​_;
  $fake->[$index] = $value;
}

sub FETCHSIZE
{
  my $self = shift;
  my ($fake, $real) = @​$self;
  return scalar(@​$fake);
}

sub STORESIZE
{
  my $self = shift;
  my ($fake, $real) = @​$self;
  my $count = shift;
  $self->SPLICE($count - scalar(@​$fake))
  if $count < @​$fake;
  $#$fake = $count-1;
}

sub EXTEND
{
  my $self = shift;
  my ($fake, $real) = @​$self;
  my $count = shift;
  $#$fake = $count-1 if $count > @​$fake;
}

sub EXISTS
{
  my $self = shift;
  my ($fake, $real) = @​$self;
  my $index = shift;
  return exists($fake->[$index]);
}

sub DELETE
{
  my $self = shift;
  my ($fake, $real) = @​$self;
  my $index = shift;
  delete $fake->[$index];
}

sub CLEAR
{
  my $self = shift;
  my ($fake, $real) = @​$self;
  $fake->STORESIZE(0);
}

sub PUSH
{
  my $self = shift;
  my ($fake, $real) = @​$self;
  push(@​$fake, @​_);
}

sub POP
{
  my $self = shift;
  return $self->SPLICE(-1,1);
}

sub SHIFT
{
  my $self = shift;
  return $self->SPLICE(0,1);
}

sub UNSHIFT
{
  my $self = shift;
  return $self->SPLICE(0,0,@​_);
}

sub SPLICE
{
  my $self = shift;
  my ($fake, $real) = @​$self;
  my $offset = shift || 0;
  my $length = shift;
  $offset += @​$fake if $offset < 0;
  $length = $#$fake - $offset
  unless defined $length;
  my (@​rv) = splice(@​$fake, $offset, $length, @​_);
  return @​rv;
}

------------ cut here ----------------

Perl Info

Flags:
    category=core
    severity=medium

Site configuration information for perl v5.8.0:

Configured by muir at Wed Dec 18 00:21:13 PST 2002.

Summary of my perl5 (revision 5.0 version 8 subversion 0) configuration:
  Platform:
    osname=freebsd, osvers=4.7-release, archname=i386-freebsd
    uname='freebsd charm.idiom.com 4.7-release freebsd 4.7-release #4: thu oct 10 22:04:24 pdt 2002 muir@charm.idiom.com:charmbuildobjcharmbuildsrcsyscharm i386 '
    config_args='-sde -Dprefix=/usr/local -Darchlib=/usr/local/lib/perl5/5.8.0/mach -Dprivlib=/usr/local/lib/perl5/5.8.0 -Dman3dir=/usr/local/lib/perl5/5.8.0/man/man3 -Dsitearch=/usr/local/lib/perl5/site_perl/5.8.0/mach -Dsitelib=/usr/local/lib/perl5/site_perl/5.8.0 -Ui_malloc -Ui_iconv -Uinstallusrbinperl -Dccflags=-DAPPLLIB_EXP="/usr/local/lib/perl5/5.8.0/BSDPAN" -Ui_gdbm -Dusemymalloc=n'
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef
    useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
    use64bitint=undef use64bitall=undef uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
  Compiler:
    cc='cc', ccflags ='-DAPPLLIB_EXP="/usr/local/lib/perl5/5.8.0/BSDPAN" -DHAS_FPSETMASK -DHAS_FLOATINGPOINT_H -fno-strict-aliasing -I/usr/local/include',
    optimize='-O -pipe -m486 -mcpu=i686 -march=pentiumpro ',
    cppflags='-DAPPLLIB_EXP="/usr/local/lib/perl5/5.8.0/BSDPAN" -DHAS_FPSETMASK -DHAS_FLOATINGPOINT_H -fno-strict-aliasing -I/usr/local/include'
    ccversion='', gccversion='2.95.4 20020320 [FreeBSD]', 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, prototype=define
  Linker and Libraries:
    ld='cc', ldflags ='-Wl,-E  -L/usr/local/lib'
    libpth=/usr/lib /usr/local/lib
    libs=-lgdbm -lm -lc -lcrypt -lutil
    perllibs=-lm -lc -lcrypt -lutil
    libc=, so=so, useshrplib=false, libperl=libperl.a
    gnulibc_version=''
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags=' '
    cccdlflags='-DPIC -fPIC', lddlflags='-shared  -L/usr/local/lib'

Locally applied patches:
    


@INC for perl v5.8.0:
    /usr/local/lib/perl5/site_perl/5.8.0/mach
    /usr/local/lib/perl5/site_perl/5.8.0
    /usr/local/lib/perl5/site_perl/5.6.1
    /usr/local/lib/perl5/site_perl/5.005
    /usr/local/lib/perl5/site_perl
    /usr/local/lib/perl5/5.8.0/BSDPAN
    /usr/local/lib/perl5/5.8.0/mach
    /usr/local/lib/perl5/5.8.0
    .


Environment for perl v5.8.0:
    HOME=/home/muir
    LANG (unset)
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=.:/home/muir/bin/charm:/home/muir/bin:/home/muir/bin/share:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/shbin:/usr/local/sbin:/usr/local/bin:/usr/local/ptybin:/usr/X11R6/bin:/usr/bin/X11:/usr/local/tex/bin:/usr/ucb:/usr/bin:/bin:/etc:/usr/etc:/usr/games:/lib:/usr/lib:/usr/local/java/bin:/usr/lib/uucp:/usr/openwin/bin:/usr/openwin/bin/xview:/usr/openwin/demo:/usr/adm:/home/muir/tmp
    PERL_BADLANG (unset)
    SHELL=/usr/local/bin/tcsh

@p5pRT
Copy link
Collaborator Author

@p5pRT p5pRT commented Jun 4, 2003

From @muir

I've narrowed the bug down. Turns out that tied
SHIFT doesn't have wantarray set correctly.

The following illustrates.

This probalby means that this bug is unrelated to #22570.

-Dave

------------------- cut here ---------------
print "1..1\n";

my $x = [ 'a', 'b', 'c', 'd', 'e' ];

tie @​$x, 'OverArray', $x;

my (@​a) = shift(@​$x);

print ($a[0] eq 2 ? "ok 1\n" : "not ok 1\n");

package OverArray;

sub TIEARRAY
{
  my $pkg = shift;
  my $orig = shift;
  my $self = bless [ [ @​$orig ], $orig ], $pkg;
  return $self;
}

sub SHIFT
{
  my $self = shift;
  return (wantarray ? 2 : 3);
}

------------------- cut here ---------------

@p5pRT
Copy link
Collaborator Author

@p5pRT p5pRT commented May 5, 2010

From @iabyn

I'm marking this ticket as rejected since it turns out that it's a bug
in the code rather than in perl itself. the SHIFT method is called in
scalar context, so that needs to be taken into account. Replcing its
last line with the following makes the problem go away​:

return ($self->SPLICE(0,1))[0];

@p5pRT
Copy link
Collaborator Author

@p5pRT p5pRT commented May 5, 2010

@iabyn - Status changed from 'open' to 'rejected'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.