develooper Front page | perl.perl5.porters | Postings from June 2008

Fwd: [PATCH] perlsyn: equivalent code for HASH ~~ ARRAY

June 26, 2008 23:21
Fwd: [PATCH] perlsyn: equivalent code for HASH ~~ ARRAY
Message ID:
I believe the following patch brian d foy have posted to is wrong.

> I've adjusted the example in the smart matching table in perlsyn. The
> equivalent Perl for smart matching an array and a hash checks that all
> of the array elements are keys in the hash, so you have to check that
> grep returns the number of input elements, not just at least one
> element.
> --- perlsyn.pod 2007-12-18 10:47:08.000000000 +0000
> +++ perlsyn.pod-new     2008-06-07 10:19:51.000000000 +0100
> @@ -674,7 +674,7 @@
>      Any     Code[+]   scalar sub truth         $b->($a)
>      Hash    Hash      hash keys identical      [sort keys %$a]~~[sort
> keys %$b]
> -    Hash    Array     hash slice existence     grep {exists $a->{$_}}
> @$b
> +    Hash    Array     hash slice existence     @$b == grep {exists
> $a->{$_}} @$b
>      Hash    Regex     hash key grep            grep /$b/, keys %$a
>      Hash    Any       hash entry existence     exists $a->{$b}

I think the original command describes the current behaiviour of smart
match better.

Indeed, I've got the following results from both the latest head perl
and perl 5.10.0 release.  (This is on a debian linux system, but with
both perls built from vanilla source.)  You can see how the result of
the smart match has the same boolean value as the first expression,
but different from the new expression you gave in the patch.

$ ~/local/bleadperl/bin/perl -we 'use 5.010; $a = {"k", "v"}; $b =
["k", "l"]; say !!($a ~~ $b);'
$ ~/local/bleadperl/bin/perl -we 'use 5.010; $a = {"k", "v"}; $b =
["k", "l"]; say !!(grep {exists $a->{$_}} @$b);'
$ ~/local/bleadperl/bin/perl -we 'use 5.010; $a = {"k", "v"}; $b =
["k", "l"]; say !!(@$b == grep {exists $a->{$_}} @$b);'

$ ~/local/bleadperl/bin/perl -V
Summary of my perl5 (revision 5 version 11 subversion 0 patch 34089)
  osname=linux, osvers=2.6.25, archname=i686-linux
  uname='linux king 2.6.25 #3 smp mon may 12 23:06:00 cest 2008 i686
gnulinux '
  hint=recommended, useposix=true, d_sigaction=define
  useithreads=undef, usemultiplicity=undef
  useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef
  use64bitint=undef, use64bitall=undef, uselongdouble=undef
  usemymalloc=n, bincompat5005=undef
  cc='cc', ccflags ='-fno-strict-aliasing -pipe -fstack-protector
-I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
  cppflags='-fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include'
  ccversion='', gccversion='4.1.2 20061115 (prerelease) (Debian
4.1.1-21)', 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',
  alignbytes=4, prototype=define
 Linker and Libraries:
  ld='cc', ldflags =' -fstack-protector -L/usr/local/lib'
  libpth=/usr/local/lib /lib /usr/lib /usr/lib64
  libs=-lnsl -ldl -lm -lcrypt -lutil -lc
  perllibs=-lnsl -ldl -lm -lcrypt -lutil -lc
  libc=/lib/, so=so, useshrplib=false, libperl=libperl.a
 Dynamic Linking:
  dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
  cccdlflags='-fPIC', lddlflags='-shared -O2 -L/usr/local/lib

Characteristics of this binary (from libperl):
                      USE_LARGE_FILES USE_PERLIO
 Locally applied patches:
 Built under linux
 Compiled at Jun 27 2008 00:36:39

Thanks for your time,

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