Perl / perl5

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

perldata documents dubious hash slice #4162

Closed p5pRT closed 21 years ago

p5pRT commented 23 years ago

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

Searchable as RT7213$

p5pRT commented 23 years ago

From @nwc10

perldata.pod gives this as one of its examples of a hash slice​:

  foreach (@​hash{keys %hash}) {   s/^\s+//; # trim leading whitespace   s/\s+$//; # trim trailing whitespace   s/(\w+)/\u\L$1/g; # "titlecase" words   }

Surely this isn't a good example\, as (I think - what am I missing?)

  foreach (values %hash) {   ...   }

is a more sane way of writing that particular construction\, and I don't feel that adding another paragraph to the pod reminding users of values is a good idea.

Nicholas Clark

Perl Info ``` Flags: category=docs severity=medium Site configuration information for perl v5.7.1: Configured by nclark at Thu Jun 28 09:57:50 BST 2001. Summary of my perl5 (revision 5.0 version 7 subversion 17) configuration: Platform: osname=linux, osvers=2.2.19pre17, archname=i686-linux uname='linux nclark 2.2.19pre17 #2 wed may 2 13:59:30 gmt 2001 i686 unknown ' config_args='-Dusedevel -Dcf_email=nick@talking.bollo.cx -Ubincompat5005 -Uinc_version_list -Uversiononly -Uuselongdouble -Uuse64bitint -de -Dcc=gcc-3.0' hint=recommended, useposix=true, d_sigaction=define usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef useperlio=define d_sfio=undef uselargefiles=define usesocks=undef use64bitint=undef use64bitall=undef uselongdouble=undef Compiler: cc='gcc-3.0', ccflags ='-Wall -fno-strict-aliasing -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64', optimize='-O2', cppflags='-Wall -fno-strict-aliasing -I/usr/local/include' ccversion='', gccversion='3.0 20010402 (Debian prerelease)', 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, usemymalloc=n, prototype=define Linker and Libraries: ld='gcc-3.0', ldflags =' -L/usr/local/lib' libpth=/usr/local/lib /lib /usr/lib libs=-lnsl -lgdbm -ldbm -ldb -ldl -lm -lc -lcrypt -lutil perllibs=-lnsl -ldl -lm -lc -lcrypt -lutil libc=/lib/libc-2.2.3.so, so=so, useshrplib=false, libperl=libperl.a Dynamic Linking: dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynamic' cccdlflags='-fpic', lddlflags='-shared -L/usr/local/lib' Locally applied patches: DEVEL10995 @INC for perl v5.7.1: /usr/local/lib/perl5/5.7.1/i686-linux /usr/local/lib/perl5/5.7.1 /usr/local/lib/perl5/site_perl/5.7.1/i686-linux /usr/local/lib/perl5/site_perl/5.7.1 /usr/local/lib/perl5/site_perl . Environment for perl v5.7.1: HOME=/home/nclark LANG=C LANGUAGE (unset) LD_LIBRARY_PATH (unset) LOGDIR (unset) PATH=/home/nclark/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11/bin:/usr/bin/X11:/usr/contrib/bin:/usr/games:/usr/sbin:/usr/ucb:/sbin:/usr/etc:/data3/src/emacs/bin/i386-unknown-bsdi2.1/ PERL_BADLANG (unset) SHELL=/bin/bash ```
p5pRT commented 23 years ago

From [Unknown Contact. See original ticket]

What you are missing is that you hadn't tried it on an older Perl​:

perl5.005 -lw %h = (a => 1\, b => 2); foreach (values %h) { $_++ }; print %h; __END__ a1b2

perl5.6.0 -lw %h = (a => 1\, b => 2); foreach (values %h) { $_++ }; print %h; __END__ a2b3

So it used to be an essential trick\, now obsolete.

Perhaps the example should be commented "This was necessary in Perl versions \< 5.6" ?

Mike Guy

p5pRT commented 23 years ago

From @nwc10

What you are missing is that you hadn't tried it on an older Perl​:

So it used to be an essential trick\, now obsolete.

Perhaps the example should be commented "This was necessary in Perl versions \< 5.6" ?

Ah. What I had found was that the whole section isn't in the 5.005_03 man page. Hmm. How confusing.

Nicholas Clark

p5pRT commented 21 years ago

From @cwest

http​://rt.perl.org/rt2/Ticket/Display.html?id=7213

Nearly two years later\, Nicholas is right. :-)

  Casey West

-- If you don't double-click me\, I can't do anything.   -- John Aniston\, on how computers have taken over his life

Inline Patch ```diff --- perl-current.orig/pod/perldata.pod Mon Mar 4 04:18:27 2002 +++ perl-current/pod/perldata.pod Thu Apr 24 20:35:56 2003 @@ -644,7 +644,7 @@ foreach (@array[ 4 .. 10 ]) { s/peter/paul/ } - foreach (@hash{keys %hash}) { + foreach (values %hash) { s/^\s+//; # trim leading whitespace s/\s+$//; # trim trailing whitespace s/(\w+)/\u\L$1/g; # "titlecase" words ```
p5pRT commented 21 years ago

From @tonycoz

On Thu\, Apr 24\, 2003 at 08​:42​:32PM -0400\, Casey West wrote​:

http​://rt.perl.org/rt2/Ticket/Display.html?id=7213 --- perl-current.orig/pod/perldata.pod Mon Mar 4 04​:18​:27 2002 +++ perl-current/pod/perldata.pod Thu Apr 24 20​:35​:56 2003 @​@​ -644\,7 +644\,7 @​@​

 foreach \(@&#8203;array\[ 4 \.\. 10 \]\) \{ s/peter/paul/ \} 

- foreach (@​hash{keys %hash}) { + foreach (values %hash) { s/^\s+//; # trim leading whitespace s/\s+$//; # trim trailing whitespace s/(\w+)/\u\L$1/g; # "titlecase" words

The problem is you're no longer demonstrating that the results of a hash slice is an lvalue.

Maybe​:

  foreach (@​hash{qw/title subtitle/}) {

(still not a great example)

Tony

p5pRT commented 21 years ago

From @cwest

It was Friday\, April 25\, 2003 when Tony Cook took the soap box\, saying​: : On Thu\, Apr 24\, 2003 at 08​:42​:32PM -0400\, Casey West wrote​: : > http​://rt.perl.org/rt2/Ticket/Display.html?id=7213 : > --- perl-current.orig/pod/perldata.pod Mon Mar 4 04​:18​:27 2002 : > +++ perl-current/pod/perldata.pod Thu Apr 24 20​:35​:56 2003 : > @​@​ -644\,7 +644\,7 @​@​ : >
: > foreach (@​array[ 4 .. 10 ]) { s/peter/paul/ } : >
: > - foreach (@​hash{keys %hash}) { : > + foreach (values %hash) { : > s/^\s+//; # trim leading whitespace : > s/\s+$//; # trim trailing whitespace : > s/(\w+)/\u\L$1/g; # "titlecase" words : : The problem is you're no longer demonstrating that the results of a hash : slice is an lvalue. : : Maybe​: : : foreach (@​hash{qw/title subtitle/}) {

Indeed\, I was too quick to judge the dubious construct. Here is a better patch.

  Casey West

-- "Louis Pasteur's theory of germs is ridiculous fiction". -- Pierre Pachet\, Professor of Physiology at Toulouse\, 1872

Inline Patch ```diff --- perl-current.orig/pod/perldata.pod Mon Mar 4 04:18:27 2002 +++ perl-current/pod/perldata.pod Thu Apr 24 21:22:01 2003 @@ -644,7 +644,7 @@ foreach (@array[ 4 .. 10 ]) { s/peter/paul/ } - foreach (@hash{keys %hash}) { + foreach (@hash{qw[key1 key2]}) { s/^\s+//; # trim leading whitespace s/\s+$//; # trim trailing whitespace s/(\w+)/\u\L$1/g; # "titlecase" words ```
p5pRT commented 21 years ago

From @rgs

Casey West wrote​:

--- perl-current.orig/pod/perldata.pod Mon Mar 4 04​:18​:27 2002 +++ perl-current/pod/perldata.pod Thu Apr 24 21​:22​:01 2003

Thanks\, applied as #19344\, along with other docs patches (tickets 21785 and 21260)

p5pRT commented 21 years ago

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