Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.91k stars 542 forks source link

tied hash store loses value regexpness #13679

Open p5pRT opened 10 years ago

p5pRT commented 10 years ago

Migrated from rt.perl.org#121477 (status was 'open')

Searchable as RT121477$

p5pRT commented 10 years ago

From zefram@fysh.org

Created by zefram@fysh.org

Generally\, a value assigned to an element of a tied hash gets passed through to the tied STORE method\, so any kind of scalar can be handled by a tied hash\, just as an ordinary hash can contain any kind of scalar. But it doesn't work for regexp scalars. Here's what one looks like in an ordinary hash​:

$ perl -MDevel​::Peek -lwe '$a{x} = ${qr/foo/}; Dump $a{x}' SV = REGEXP(0x14aa9a0) at 0x147ba68   REFCNT = 1   FLAGS = (POK\,FAKE\,pPOK)   PV = 0x1490460 "(?^​:foo)"   CUR = 8   LEN = 0   EXTFLAGS = 0x680000 (CHECK_ALL\,USE_INTUIT_NOML\,USE_INTUIT_ML)   INTFLAGS = 0x0   NPARENS = 0   LASTPAREN = 0   LASTCLOSEPAREN = 0   MINLEN = 3   MINLENRET = 3   GOFS = 0   PRE_PREFIX = 4   SEEN_EVALS = 0   SUBLEN = 0   SUBBEG = 0x0   ENGINE = 0x2b04257e50e0   MOTHER_RE = 0x1496c28   PAREN_NAMES = 0x0   SUBSTRS = 0x149a980   PPRIVATE = 0x149ee90   OFFS = 0x1490840

And here's what happens with a tied hash​:

$ perl -lwe '{ package Foo; use Devel​::Peek; sub TIEHASH { bless({}\, $_[0]) } sub STORE { Dump $_[2] } } my %a; tie %a\, "Foo"; $a{x} = ${qr/foo/}' SV = PVLV(0xe8ae40) at 0xe7d560   REFCNT = 2   FLAGS = (POK\,pPOK)   IV = 0   NV = 0   PV = 0xe8e980 "(?^​:foo)"\0   CUR = 8   LEN = 16   MAGIC = 0xe8ed20   MG_VIRTUAL = &PL_vtbl_packelem   MG_TYPE = PERL_MAGIC_tiedelem(p)   MG_FLAGS = 0x02   REFCOUNTED   MG_OBJ = 0xe7cd38   SV = IV(0xe7cd28) at 0xe7cd38   REFCNT = 2   FLAGS = (ROK)   RV = 0xe61c18   SV = PVHV(0xe66890) at 0xe61c18   REFCNT = 1   FLAGS = (OBJECT\,SHAREKEYS)   STASH = 0xe7d5d8 "Foo"   ARRAY = 0x0   KEYS = 0   FILL = 0   MAX = 7   RITER = -1   EITER = 0x0   MG_LEN = -2   MG_PTR = 0xe7cea0 => HEf_SVKEY   SV = PV(0xe5fd90) at 0xe7cea0   REFCNT = 2   FLAGS = (POK\,pPOK)   PV = 0xe87b60 "x"\0   CUR = 1   LEN = 16   TYPE = T   TARGOFF = 0   TARGLEN = 0   TARG = 0xe731e8   FLAGS = 0

Note that the value in the scalar received by the STORE method is a plain string\, the stringification of the regexp. The basic problem seems to be a collision between the use of the SvTYPE field to denote a special type of scalar value\, SVt_REGEXP\, and the the use of the same field to denote a special type of scalar container\, SVt_PVLV. Although they're horribly interleaved\, the container and value aspects of scalars are for the most part orthogonal. The failure of orthogonality here is jarring.

Perl Info ``` Flags: category=core severity=low Site configuration information for perl 5.16.3: Configured by zefram at Sat Jul 27 18:04:27 BST 2013. Summary of my perl5 (revision 5 version 16 subversion 3) configuration: Platform: osname=linux, osvers=3.2.0-4-amd64, archname=x86_64-linux-thread-multi uname='linux barba.rous.org 3.2.0-4-amd64 #1 smp debian 3.2.46-1 x86_64 gnulinux ' config_args='-des -Dprefix=/home/zefram/usr/perl/perl_install/perl-5.16.3-i64-f52 -Duselargefiles -Dusethreads -Uafs -Ud_csh -Uusesfio -Uusenm -Duseshrplib -Dusedevel -Uversiononly -Ui_db' hint=recommended, useposix=true, d_sigaction=define useithreads=define, usemultiplicity=define useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef use64bitint=define, use64bitall=define, uselongdouble=undef usemymalloc=n, bincompat5005=undef Compiler: cc='cc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64', optimize='-O2', cppflags='-D_REENTRANT -D_GNU_SOURCE -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include' ccversion='', gccversion='4.7.2', gccosandvers='' intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=12345678 d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16 ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8 alignbytes=8, prototype=define Linker and Libraries: ld='cc', ldflags =' -fstack-protector -L/usr/local/lib' libpth=/usr/local/lib /lib/x86_64-linux-gnu /lib/../lib /usr/lib/x86_64-linux-gnu /usr/lib/../lib /lib /usr/lib libs=-lnsl -ldb -ldl -lm -lcrypt -lutil -lpthread -lc perllibs=-lnsl -ldl -lm -lcrypt -lutil -lpthread -lc libc=, so=so, useshrplib=true, libperl=libperl.so gnulibc_version='2.15' Dynamic Linking: dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E -Wl,-rpath,/home/zefram/usr/perl/perl_install/perl-5.16.3-i64-f52/lib/5.16.3/x86_64-linux-thread-multi/CORE' cccdlflags='-fPIC', lddlflags='-shared -O2 -L/usr/local/lib -fstack-protector' Locally applied patches: @INC for perl 5.16.3: /home/zefram/usr/perl/perl_install/perl-5.16.3-i64-f52/lib/site_perl/5.16.3/x86_64-linux-thread-multi /home/zefram/usr/perl/perl_install/perl-5.16.3-i64-f52/lib/site_perl/5.16.3 /home/zefram/usr/perl/perl_install/perl-5.16.3-i64-f52/lib/5.16.3/x86_64-linux-thread-multi /home/zefram/usr/perl/perl_install/perl-5.16.3-i64-f52/lib/5.16.3 . Environment for perl 5.16.3: HOME=/home/zefram LANG (unset) LANGUAGE (unset) LD_LIBRARY_PATH (unset) LOGDIR (unset) PATH=/home/zefram/usr/perl/perl_install/perl-5.16.3-i64-f52/bin:/home/zefram/usr/perl/util:/home/zefram/pub/x86_64-unknown-linux-gnu/bin:/home/zefram/pub/common/bin:/usr/bin:/bin:/usr/local/bin:/usr/games PERL_BADLANG (unset) SHELL=/usr/bin/zsh ```
p5pRT commented 10 years ago

From @rjbs

This program may more clearly demonstrate the problem for other readers​:

  use 5.19.9;   use Tie​::Hash;

  my $qr = qr/foo/;   say ref \${ $qr };

  {   my %h;   $h{qr} = $$qr;   say ref \$h{qr};   }

  {   tie my %h\, 'Tie​::StdHash';   $h{qr} = $$qr;   say ref \$h{qr};   }

-- rjbs

p5pRT commented 10 years ago

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