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

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

From:
=?ISO-8859-1?Q?Zsb=E1n_Ambrus?=
Date:
June 26, 2008 23:21
Subject:
Fwd: [PATCH] perlsyn: equivalent code for HASH ~~ ARRAY
Message ID:
25d4951c0806262320r2fe9b6bfofd55527c545727c0@mail.gmail.com
I believe the following patch brian d foy have posted to
perl5-porters@perl.org 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);'
1
$ ~/local/bleadperl/bin/perl -we 'use 5.010; $a = {"k", "v"}; $b =
["k", "l"]; say !!(grep {exists $a->{$_}} @$b);'
1
$ ~/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)
configuration:
 Platform:
  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 '
  config_args=''
  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
 Compiler:
  cc='cc', ccflags ='-fno-strict-aliasing -pipe -fstack-protector
-I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
  optimize='-O2',
  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',
lseeksize=8
  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/libc-2.3.6.so, so=so, useshrplib=false, libperl=libperl.a
  gnulibc_version='2.3.6'
 Dynamic Linking:
  dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
  cccdlflags='-fPIC', lddlflags='-shared -O2 -L/usr/local/lib
-fstack-protector'

Characteristics of this binary (from libperl):
 Compile-time options: PERL_DONT_CREATE_GVSV PERL_MALLOC_WRAP
                      USE_LARGE_FILES USE_PERLIO
 Locally applied patches:
      DEVEL
 Built under linux
 Compiled at Jun 27 2008 00:36:39
 @INC:
  /home/ambrus/local/bleadperl/lib/5.11.0/i686-linux
  /home/ambrus/local/bleadperl/lib/5.11.0
  /home/ambrus/local/bleadperl/lib/site_perl/5.11.0/i686-linux
  /home/ambrus/local/bleadperl/lib/site_perl/5.11.0
  .
$

Thanks for your time,

Ambrus



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