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

smartmatch example in perlop is broken #13239

Closed
p5pRT opened this issue Sep 8, 2013 · 9 comments
Closed

smartmatch example in perlop is broken #13239

p5pRT opened this issue Sep 8, 2013 · 9 comments

Comments

@p5pRT
Copy link

p5pRT commented Sep 8, 2013

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

Searchable as RT119667$

@p5pRT
Copy link
Author

p5pRT commented Sep 8, 2013

From @mauke

Created by @mauke

perldoc perlop​:

| or, if other non-required fields are allowed, use ARRAY ~~ HASH​:
|
| use v5.10.1;
| sub make_dogtag {
| state $REQUIRED_FIELDS = { name=>1, rank=>1, serial_num=>1 };
|
| my ($class, $init_fields) = @​_;
|
| die "Must supply (at least) name, rank, and serial number"
| unless [keys %{$init_fields}] ~~ $REQUIRED_FIELDS;
|
| ...
| }

But​:

| ARRAY HASH any ARRAY elements exist as HASH keys
| like​: grep { exists HASH->{$_} } ARRAY

So the make_dogtag example above doesn't check whether all of the required
fields (name, rank, serial_num) are present. Instead it checks whether at least
one of them is present (which is of course much less useful).

Perl Info

Flags:
    category=docs
    severity=medium

This perlbug was built using Perl 5.12.1 - Thu Jun  3 20:09:15 CEST 2010
It is being executed now by  Perl 5.18.1 - Tue Aug 13 07:08:47 CEST 2013.

Site configuration information for perl 5.18.1:

Configured by mauke at Tue Aug 13 07:08:47 CEST 2013.

Summary of my perl5 (revision 5 version 18 subversion 1) configuration:
   
  Platform:
    osname=linux, osvers=3.5.7-gentoo, archname=i686-linux
    uname='linux nora 3.5.7-gentoo #5 preempt sat jan 26 16:46:10 cet 2013 i686 amd athlon(tm) 64 processor 3200+ authenticamd 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 -flto',
    cppflags='-fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include'
    ccversion='', gccversion='4.8.1', 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 ='-O2 -flto -fstack-protector -L/usr/local/lib'
    libpth=/usr/local/lib /lib/../lib /usr/lib/../lib /lib /usr/lib
    libs=-lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc -lgdbm_compat
    perllibs=-lnsl -ldl -lm -lcrypt -lutil -lc
    libc=/lib/libc-2.15.so, so=so, useshrplib=false, libperl=libperl.a
    gnulibc_version='2.15'
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
    cccdlflags='-fPIC', lddlflags='-shared -O2 -flto -L/usr/local/lib -fstack-protector'

Locally applied patches:
    SAVEARGV0 - disable magic open in <ARGV>


@INC for perl 5.18.1:
    /home/mauke/usr/local/lib/perl5/site_perl/5.18.1/i686-linux
    /home/mauke/usr/local/lib/perl5/site_perl/5.18.1
    /home/mauke/usr/local/lib/perl5/5.18.1/i686-linux
    /home/mauke/usr/local/lib/perl5/5.18.1
    .


Environment for perl 5.18.1:
    HOME=/home/mauke
    LANG=en_US.UTF-8
    LANGUAGE (unset)
    LC_COLLATE=POSIX
    LD_LIBRARY_PATH=/home/mauke/usr/local/lib
    LOGDIR (unset)
    PATH=/home/mauke/usr/perlbrew/bin:/home/mauke/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/opt/bin:/usr/i686-pc-linux-gnu/gcc-bin/4.6.3:/opt/sun-jdk-1.4.2.13/bin:/opt/sun-jdk-1.4.2.13/jre/bin:/opt/sun-jdk-1.4.2.13/jre/javaws:/opt/dmd/bin:/usr/games/bin
    PERLBREW_BASHRC_VERSION=0.43
    PERLBREW_HOME=/home/mauke/.perlbrew
    PERLBREW_PATH=/home/mauke/usr/perlbrew/bin
    PERLBREW_ROOT=/home/mauke/usr/perlbrew
    PERLBREW_VERSION=0.27
    PERL_BADLANG (unset)
    PERL_UNICODE=SAL
    SHELL=/bin/bash

@p5pRT
Copy link
Author

p5pRT commented Sep 8, 2013

From @cpansprout

On Sun Sep 08 02​:03​:42 2013, mauke- wrote​:

perldoc perlop​:

| or, if other non-required fields are allowed, use ARRAY ~~ HASH​:
|
| use v5.10.1;
| sub make_dogtag {
| state $REQUIRED_FIELDS = { name=>1, rank=>1, serial_num=>1
};
|
| my ($class, $init_fields) = @​_;
|
| die "Must supply (at least) name, rank, and serial number"
| unless [keys %{$init_fields}] ~~ $REQUIRED_FIELDS;
|
| ...
| }

But​:

| ARRAY HASH any ARRAY elements exist as HASH keys
| like​: grep { exists HASH->{$_} } ARRAY

So the make_dogtag example above doesn't check whether all of the
required
fields (name, rank, serial_num) are present. Instead it checks whether
at least
one of them is present (which is of course much less useful).

We’ve always had that problem. Any non-trivial example of smartmatch
usually does something other than the author intended.

--

Father Chrysostomos

@p5pRT
Copy link
Author

p5pRT commented Sep 8, 2013

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

@p5pRT
Copy link
Author

p5pRT commented Sep 8, 2013

From @Smylers

Father Chrysostomos via RT writes​:

Any non-trivial example of smartmatch usually does something other
than the author intended.

I suggest that that quote be added to the smartmatch docs.

Smylers
--
Stop drug companies hiding negative research results.
Sign the AllTrials petition to get all clinical research results published.
Read more​: http​://www.alltrials.net/blog/the-alltrials-campaign/

@p5pRT
Copy link
Author

p5pRT commented Sep 8, 2013

From @Leont

On Sun, Sep 8, 2013 at 3​:57 PM, Father Chrysostomos via RT <
perlbug-followup@​perl.org> wrote​:

On Sun Sep 08 02​:03​:42 2013, mauke- wrote​:

perldoc perlop​:

| or, if other non-required fields are allowed, use ARRAY ~~ HASH​:
|
| use v5.10.1;
| sub make_dogtag {
| state $REQUIRED_FIELDS = { name=>1, rank=>1, serial_num=>1
};
|
| my ($class, $init_fields) = @​_;
|
| die "Must supply (at least) name, rank, and serial number"
| unless [keys %{$init_fields}] ~~ $REQUIRED_FIELDS;
|
| ...
| }

But​:

| ARRAY HASH any ARRAY elements exist as HASH keys
| like​: grep { exists HASH->{$_} } ARRAY

So the make_dogtag example above doesn't check whether all of the
required
fields (name, rank, serial_num) are present. Instead it checks whether
at least
one of them is present (which is of course much less useful).

We’ve always had that problem. Any non-trivial example of smartmatch
usually does something other than the author intended.

<shameless plug>

In Smart​::Match, that would have just been «$init_fields ~~
hash_keys(contains(qw/name rank serial_num/))», which is both intuitive to
read and trivial to write even without knowing the details of smartmatch
semantics.

</shameless plug>

Leon

@p5pRT
Copy link
Author

p5pRT commented Jan 30, 2016

From @mauke

On Sun Sep 08 02​:03​:42 2013, mauke- wrote​:

perldoc perlop​:

...

So the make_dogtag example above doesn't check whether all of the
required
fields (name, rank, serial_num) are present. Instead it checks whether
at least
one of them is present (which is of course much less useful).

Fixed in commit 1b590b3, which simply removes that example.

@p5pRT
Copy link
Author

p5pRT commented Jan 30, 2016

@mauke - Status changed from 'open' to 'pending release'

@p5pRT
Copy link
Author

p5pRT commented May 13, 2016

From @khwilliamson

Thank you for submitting this report. You have helped make Perl better.
 
With the release of Perl 5.24.0 on May 9, 2016, this and 149 other issues have been resolved.

Perl 5.24.0 may be downloaded via https://metacpan.org/release/RJBS/perl-5.24.0

@p5pRT p5pRT closed this as completed May 13, 2016
@p5pRT
Copy link
Author

p5pRT commented May 13, 2016

@khwilliamson - Status changed from 'pending release' 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