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

weird results from reverse( %x = reverse %h ) #7526

Closed
p5pRT opened this issue Oct 6, 2004 · 14 comments
Closed

weird results from reverse( %x = reverse %h ) #7526

p5pRT opened this issue Oct 6, 2004 · 14 comments

Comments

@p5pRT
Copy link

p5pRT commented Oct 6, 2004

Migrated from rt.perl.org#31865 (status was 'resolved')

Searchable as RT31865$

@p5pRT
Copy link
Author

p5pRT commented Oct 6, 2004

From bzm@2bz.de

Created by bzm@2bz.de

lisa​:~ borisz$ perl -MData​::Dumper -le '%h = (1 => 2, a=>"b", c => 2);
%h = reverse ( %x = reverse %h); print Dumper({x => \%x, h =>\%h});'
$VAR1 = {
  'h' => {
  '' => 2
  },
  'x' => {
  'b' => 'a',
  '2' => 'c'
  }
  };

Note that %h is damaged now. I really expect to loose the dupes.

Some more opinions here​:
http​://www.perlmonks.com/index.pl?node_id=394658

Perl Info

Flags:
     category=core
     severity=medium

Site configuration information for perl v5.8.4:�

Configured by sherm at Sat Apr 24 12:35:38 EDT 2004.

Summary of my perl5 (revision 5 version 8 subversion 4) configuration:
   Platform:
     osname=darwin, osvers=7.3.0, archname=darwin-thread-multi-2level
     uname='darwin sherm-pendleys-computer.local 7.3.0 darwin kernel  
version 7.3.0: fri mar 5 14:22:55 pst 2004;  
root:xnuxnu-517.3.15.obj~4release_ppc power macintosh powerpc '
     config_args='-Dprefix=/usr/local/cb/perl5.8.4 -Dusethreads  
-Duseshrplib -Dcf_email=perl@dot-app.org -des'
     hint=recommended, useposix=true, d_sigaction=define
     usethreads=define use5005threads=undef useithreads=define  
usemultiplicity=define
     useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
     use64bitint=undef use64bitall=undef uselongdouble=undef
     usemymalloc=n, bincompat5005=undef
   Compiler:
     cc='cc', ccflags ='-pipe -fno-common -DPERL_DARWIN -no-cpp-precomp  
-fno-strict-aliasing -I/usr/local/include',
     optimize='-Os',
     cppflags='-no-cpp-precomp -pipe -fno-common -DPERL_DARWIN  
-no-cpp-precomp -fno-strict-aliasing -I/usr/local/include'
     ccversion='', gccversion='3.3 20030304 (Apple Computer, Inc. build  
1495)', gccosandvers=''
     intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=4321
     d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=8
     ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t',  
lseeksize=8
     alignbytes=8, prototype=define
   Linker and Libraries:
     ld='env MACOSX_DEPLOYMENT_TARGET=10.3 cc', ldflags ='  
-L/usr/local/lib'
     libpth=/usr/local/lib /usr/lib
     libs=-ldbm -ldl -lm -lc
     perllibs=-ldl -lm -lc
     libc=/usr/lib/libc.dylib, so=dylib, useshrplib=true,  
libperl=libperl.dylib
     gnulibc_version=''
   Dynamic Linking:
     dlsrc=dl_dyld.xs, dlext=bundle, d_dlsymun=undef, ccdlflags=' '
     cccdlflags=' ', lddlflags=' -bundle -undefined dynamic_lookup  
-L/usr/local/lib'

Locally applied patches:



@INC for perl v5.8.4:
     /sw/lib/perl5
     /sw/lib/perl5/darwin
     /usr/local/cb/perl5.8.4/lib/5.8.4/darwin-thread-multi-2level
     /usr/local/cb/perl5.8.4/lib/5.8.4
      
/usr/local/cb/perl5.8.4/lib/site_perl/5.8.4/darwin-thread-multi-2level
     /usr/local/cb/perl5.8.4/lib/site_perl/5.8.4
     /usr/local/cb/perl5.8.4/lib/site_perl
     .


Environment for perl v5.8.4:
     DYLD_LIBRARY_PATH (unset)
     HOME=/Users/borisz
     LANG (unset)
     LANGUAGE (unset)
     LD_LIBRARY_PATH (unset)
     LOGDIR (unset)
      
PATH=/sw/bin:/sw/sbin:/usr/local/cb/perl5.8.4/bin:/usr/local/bin:/bin:/ 
sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin
     PERL5LIB=/sw/lib/perl5:/sw/lib/perl5/darwin
     PERL_BADLANG (unset)
     SHELL=/bin/bash
--
Boris


@p5pRT
Copy link
Author

p5pRT commented Oct 6, 2004

From @rgs

Boris Zentner (via RT) wrote​:

lisa​:~ borisz$ perl -MData​::Dumper -le '%h = (1 => 2, a=>"b", c => 2);
%h = reverse ( %x = reverse %h); print Dumper({x => \%x, h =>\%h});'
$VAR1 = {
'h' => {
'' => 2
},
'x' => {
'b' => 'a',
'2' => 'c'
}
};

Note that %h is damaged now. I really expect to loose the dupes.

Another "stuff on the stack is not refcounted" bug ?

@p5pRT
Copy link
Author

p5pRT commented Oct 6, 2004

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Oct 6, 2004

From rick@bort.ca

On Wed, Oct 06, 2004 at 06​:50​:17PM +0200, Rafael Garcia-Suarez wrote​:

Boris Zentner (via RT) wrote​:

lisa​:~ borisz$ perl -MData​::Dumper -le '%h = (1 => 2, a=>"b", c => 2);
%h = reverse ( %x = reverse %h); print Dumper({x => \%x, h =>\%h});'
$VAR1 = {
'h' => {
'' => 2
},
'x' => {
'b' => 'a',
'2' => 'c'
}
};

Note that %h is damaged now. I really expect to loose the dupes.

Another "stuff on the stack is not refcounted" bug ?

I dunno, but some more data points​:

  $ perl5.8.3 -le 'print (%x = ("a",1,"b",2,"b",3));'
  bb
  $ perl5.6.1 -le 'print (%x = ("a",1,"b",2,"b",3));'
  a1bb3

I'm not sure what it should print but those are both obviously wrong.

--
Rick Delaney
rick@​bort.ca

@p5pRT
Copy link
Author

p5pRT commented Oct 6, 2004

From @gbarr

On 6 Oct 2004, at 17​:50, Rafael Garcia-Suarez wrote​:

Boris Zentner (via RT) wrote​:

lisa​:~ borisz$ perl -MData​::Dumper -le '%h = (1 => 2, a=>"b", c => 2);
%h = reverse ( %x = reverse %h); print Dumper({x => \%x, h =>\%h});'
$VAR1 = {
'h' => {
'' => 2
},
'x' => {
'b' => 'a',
'2' => 'c'
}
};

Note that %h is damaged now. I really expect to loose the dupes.

Another "stuff on the stack is not refcounted" bug ?

I am not so sure. I do not have a blead perl to run right now, but
compare

perl -MDevel​::Peek -e '%h = (1 => 2, a=>"b", c => 2); Dump($_) for @​x =
reverse %h'

perl -MDevel​::Peek -e '%h = (1 => 2, a=>"b", c => 2); Dump($_) for %x =
reverse %h'

It seems that what is left on the stack after a hash assign in a list
context is bad.

But reading the code it seems to me that the code above would not do
what the user expected anyway. The code in pp_aassign looks like it is
trying to do the same thing regardless of the LHS being an array or a
hash. So it would seem the stack would have the same contents as the
RHS and not whats left after the hash assignment had removed
duplicates.

Graham.

@p5pRT
Copy link
Author

p5pRT commented Oct 6, 2004

From @demerphq

I dunno, but some more data points​:

$ perl5\.8\.3 \-le 'print \(%x = \("a"\,1\,"b"\,2\,"b"\,3\)\);'
bb
$ perl5\.6\.1 \-le 'print \(%x = \("a"\,1\,"b"\,2\,"b"\,3\)\);'
a1bb3

I'm not sure what it should print but those are both obviously wrong.

Maybe its also OS specific? On Win32 I couldn't repeat this behaviour using
either AS Perl 5.6.1 (658) or under a home built Perl 5.8.5 nor under the
Cygwin Perl 5.8.5 either.

D​:\Development>perl -le "print (%x = (qw(a 1 b 2 c 3)));"
a1b2c3

D​:\Development>perl -v

This is perl, v5.6.1 built for MSWin32-x86-multi-thread
(with 1 registered patch, see perl -V for more detail)

Copyright 1987-2001, Larry Wall

Binary build 638 provided by ActiveState Corp. http​://www.ActiveState.com
ActiveState is a division of Sophos.
Built Apr 13 2004 19​:24​:21

D​:\Development>perl5.8.5 -le "print (%x = (qw(a 1 b 2 c 3)));"
a1b2c3

D​:\Development>e​:\perl5.8.5\bin\perl -le "print (%x = (qw(a 1 b 2 c 3)));"
a1b2c3

D​:\Development>perl5.8.5 -v

This is perl, v5.8.5 built for cygwin-thread-multi-64int

Copyright 1987-2004, 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.com/, the Perl Home Page.

D​:\Development\rating\csb_sp>e​:\perl5.8.5\bin\perl -v

This is perl, v5.8.5 built for MSWin32-x86-multi-thread
(with 1 registered patch, see perl -V for more detail)

Copyright 1987-2004, 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.com/, the Perl Home Page.

@p5pRT
Copy link
Author

p5pRT commented Oct 6, 2004

From @demerphq

Maybe its also OS specific? On Win32 I couldn't repeat this behaviour
using
either AS Perl 5.6.1 (658) or under a home built Perl 5.8.5 nor under the
Cygwin Perl 5.8.5 either.

Er, I meant AS 638. Sorry.

Also it looks like AS 638 gets it more right but not correct than later
versions​:

D​:\Development>perl5.6.1 -MData​::Dumper -le "%h = (1 => 2, a=>'b', c => 2);
%h = reverse ( %x = reverse %h); pr
int Dumper({x => \%x, h =>\%h});"
$VAR1 = {
  'h' => {
  '' => 2,
  '1' => 2,
  'a' => 'b'
  },
  'x' => {
  '2' => '1',
  'b' => 'a'
  }
  };

D​:\Development>perl5.8.5 -MData​::Dumper -le "%h = (1 => 2, a=>'b', c => 2);
%h = reverse ( %x = reverse %h
); print Dumper({x => \%x, h =>\%h});"
$VAR1 = {
  'h' => {
  '' => 2
  },
  'x' => {
  'b' => 'a',
  '2' => 'c'
  }
  };

@p5pRT
Copy link
Author

p5pRT commented Oct 6, 2004

From rick@bort.ca

On Wed, Oct 06, 2004 at 06​:58​:58PM +0100, Graham Barr wrote​:

But reading the code it seems to me that the code above would not do
what the user expected anyway. The code in pp_aassign looks like it is
trying to do the same thing regardless of the LHS being an array or a
hash. So it would seem the stack would have the same contents as the
RHS and not whats left after the hash assignment had removed
duplicates.

It looks like it's trying to treat arrays and hashes differently to me,
in particular note the comment​:

  /* Removes from the stack the entries which ended up as
  * duplicated keys in the hash (fix for [perl #24380]) */

Maybe that fix just didn't have enough test cases. As for what it
should do, I just found in perlop

  Similarly, a list assignment in list context produces the
  list of lvalues assigned to, and a list assignment in
  scalar context returns the number of elements produced by
  the expression on the right hand side of the assignment.

So I think Boris's code should do what he expected.

--
Rick Delaney
rick@​bort.ca

@p5pRT
Copy link
Author

p5pRT commented Oct 6, 2004

From rick@bort.ca

On Wed, Oct 06, 2004 at 07​:00​:59PM +0100, Orton, Yves wrote​:

I dunno, but some more data points​:

$ perl5\.8\.3 \-le 'print \(%x = \("a"\,1\,"b"\,2\,"b"\,3\)\);'
bb
$ perl5\.6\.1 \-le 'print \(%x = \("a"\,1\,"b"\,2\,"b"\,3\)\);'
a1bb3

I'm not sure what it should print but those are both obviously wrong.

Maybe its also OS specific? On Win32 I couldn't repeat this behaviour using
either AS Perl 5.6.1 (658) or under a home built Perl 5.8.5 nor under the
Cygwin Perl 5.8.5 either.

D​:\Development>perl -le "print (%x = (qw(a 1 b 2 c 3)));"
  ^
  b in my example
a1b2c3

The required trigger is a duplicate key which you don't have in your
trials.

--
Rick Delaney
rick@​bort.ca

@p5pRT
Copy link
Author

p5pRT commented Oct 6, 2004

From @demerphq

D​:\Development>perl -le "print (%x = (qw(a 1 b 2 c 3)));"
^
b in my example
a1b2c3

The required trigger is a duplicate key which you don't have in your
trials.

Whoops. So I didn't sorry for the noise (I got the same results as you once
the key was duped).

Yves

@p5pRT
Copy link
Author

p5pRT commented Oct 6, 2004

From @gbarr

On 6 Oct 2004, at 19​:11, Rick Delaney wrote​:

On Wed, Oct 06, 2004 at 06​:58​:58PM +0100, Graham Barr wrote​:

But reading the code it seems to me that the code above would not do
what the user expected anyway. The code in pp_aassign looks like it is
trying to do the same thing regardless of the LHS being an array or a
hash. So it would seem the stack would have the same contents as the
RHS and not whats left after the hash assignment had removed
duplicates.

It looks like it's trying to treat arrays and hashes differently to me,
in particular note the comment​:

    /\* Removes from the stack the entries which ended up as
     \* duplicated keys in the hash \(fix for \[perl \#24380\]\) \*/

Yes, you are right. I was looking at old source.

But the code seems flawed. It counts duplicates during the assignment
loop.
Then at the end does a single move on the stack. The move should happen
during assignment loop.

Graham.

@p5pRT
Copy link
Author

p5pRT commented Oct 8, 2004

From @davidnicol

here's the arguably obvious workaround, since the bug applies to the
list returned
from assignment of a list to a hash, don't use
assignment-of-an-array-to-a-hash as an
rvalue. One assumes that Boris did this to get the result he wanted.

$ perl -MData​::Dumper -le '%h = (1 => 2, a=>"b", c => 2); %x = reverse %h; %h =
reverse %x; print Dumper({x => \%x, h =>\%h});'
$VAR1 = {
  'h' => {
  'c' => '2',
  'a' => 'b'
  },
  'x' => {
  'b' => 'a',
  '2' => 'c'
  }
  };

lisa​:~ borisz$ perl -MData​::Dumper -le '%h = (1 => 2, a=>"b", c => 2);
%h = reverse ( %x = reverse %h); print Dumper({x => \%x, h =>\%h});'
$VAR1 = {
'h' => {
'' => 2
},
'x' => {
'b' => 'a',
'2' => 'c'
}
};

Note that %h is damaged now. I really expect to loose the dupes.

--
David L Nicol
"Valuable ideas can withstand scrutiny" -- authors of /XP Explained/

@p5pRT
Copy link
Author

p5pRT commented Sep 11, 2010

From @iabyn

Fixed in blead with commit 4596056

@p5pRT
Copy link
Author

p5pRT commented Sep 11, 2010

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

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

No branches or pull requests

1 participant