Perl / perl5

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

f(n - 1) oddness #1960

Closed p5pRT closed 20 years ago

p5pRT commented 24 years ago

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

Searchable as RT3233$

p5pRT commented 24 years ago

From @schwern

Created by @schwern

$ perl-devel -wle 'sub n { print @​_; 3 } $foo = n - 1; print $foo' -1 3

$ perl-devel -wle 'sub n { print @​_; 3 } $foo = n() - 1; print $foo'

2

Somebody convince me that this is the way the precedence rules are supposed to work out\, because right now it appears horribly wrong\, but its late.

Perl Info ``` Site configuration information for perl v5.6.0: Configured by schwern at Sat Mar 18 21:03:34 EST 2000. Summary of my perl5 (revision 5.0 version 6 subversion 0) configuration: Platform: osname=linux, osvers=2.2.15pre3, archname=ppc-linux-64all uname='linux blackrider 2.2.15pre3 #3 sat jan 29 18:02:45 cet 2000 ppc unknown ' config_args='' hint=recommended, useposix=true, d_sigaction=define usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef useperlio=undef d_sfio=undef uselargefiles=define use64bitint=define use64bitall=define uselongdouble=undef usesocks=undef Compiler: cc='cc', optimize='-g', gccversion=2.95.2 20000220 (Debian GNU/Linux) cppflags='-DDEBUGGING -fno-strict-aliasing -I/usr/local/include -DDEBUGGING_MSTATS -DDEBUGGING_OPS' ccflags ='-DDEBUGGING -fno-strict-aliasing -I/usr/local/include -DDEBUGGING_MSTATS -DDEBUGGING_OPS -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64' stdchar='char', d_stdstdio=define, usevfork=false intsize=4, longsize=4, ptrsize=4, doublesize=8 d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=8 ivtype='long long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8 alignbytes=8, usemymalloc=y, prototype=define Linker and Libraries: ld='cc', ldflags =' -L/usr/local/lib' libpth=/usr/local/lib /lib /usr/lib libs=-lnsl -lndbm -ldb -ldl -lm -lc -lposix -lcrypt libc=/lib/libc-2.1.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: v5.6.0-RC2 @INC for perl v5.6.0: /usr/local/perl5.6/lib/ppc-linux-64all /usr/local/perl5.6/lib /usr/local/perl5.6/lib/site_perl/ppc-linux-64all /usr/local/perl5.6/lib/site_perl . Environment for perl v5.6.0: HOME=/home/schwern LANG (unset) LANGUAGE (unset) LD_LIBRARY_PATH (unset) LOGDIR (unset) PATH=/home/schwern/bin:/home/schwern/sbin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/usr/local/X11/bin:/usr/bin/X11:/usr/games PERL_BADLANG (unset) SHELL=/bin/bash ```
p5pRT commented 24 years ago

From @simoncozens

$ perl-devel -wle 'sub n { print @​_; 3 } $foo = n - 1; print $foo' -1 3

$ perl-devel -wle 'sub n { print @​_; 3 } $foo = n() - 1; print $foo'

2

Somebody convince me that this is the way the precedence rules are supposed to work out\, because right now it appears horribly wrong\, but its late.

(Coincidentally\, I'm currently writing about subroutines.)

If "supposed to" means "are documented to"\, then try this from perlsub​:

  sub mygrep (&@​) mygrep { /foo/ } $a\, $b\, $c   sub myrand ($) myrand 42   sub mytime () mytime

Note how the last three examples in the table above are treated specially by the parser. C\<mygrep()> is parsed as a true list operator\, C\<myrand()> is parsed as a true unary operator with unary precedence the same as C\<rand()>\, and C\<mytime()> is truly without arguments\, just like C\<time()>. That is\, if you say

  mytime +2;

you'll get C\<mytime() + 2>\, not C\<mytime(2)>\, which is how it would be parsed without a prototype.

If "supposed to work out" means "the Good\, Right and Intuitive Thing"\, then\, well\, who knows?

Simon

__END__


The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review\, retransmission\, dissemination or other use of\, or taking of any action in reliance upon\, this information by persons or entities other than the intended recipient is prohibited. If you received this in error\, please contact the sender and delete the material from any computer.

p5pRT commented 24 years ago

From [Unknown Contact. See original ticket]

Ooops. Finger trouble. Here it is again with the correct Subject​:\, for the benefit of the perlbugtron.

schwern@​blackrider.arena-i.com (Michael G Schwern) wrote

Somebody convince me that this is the way the precedence rules are supposed to work out\, because right now it appears horribly wrong\, but its late.

Normally a subroutine is syntactically the same as a list operator (rightwards). But if it has a prototype of ($)\, it's treated as a named unary operator. And a prototype of () is treated as a list operator (rightwards).

I can't find this said in so many words in the pods\, but an example in the Prototypes section of perlsub implies it​:

  Note how the last three examples above are treated specially   by the parser. mygrep() is parsed as a true list operator\,   myrand() is parsed as a true unary operator with unary   precedence the same as rand()\, and mytime() is truly without   arguments\, just like time(). That is\, if you say

  mytime +2;

  you'll get mytime() + 2\, not mytime(2)\, which is how it   would be parsed without the prototype.

Mike Guy

p5pRT commented 24 years ago

From [Unknown Contact. See original ticket]

On May 11\, Michael G Schwern said​:

$ perl-devel -wle 'sub n { print @​_; 3 } $foo = n - 1; print $foo' -1 3

$ perl-devel -wle 'sub n { print @​_; 3 } $foo = n() - 1; print $foo'

2

This reminds me of an interesting precedence issue brought up on DALnet #perl.

  $foo = 13;   print $foo - 1; # prints 12   print $foo-1; # prints 12   print $foo -1; # prints -1 to the filehandle 13

The guy's problem was his use of whitespace.

p5pRT commented 24 years ago

From [Unknown Contact. See original ticket]

"Michael" == Michael G Schwern \schwern@&#8203;blackrider\.arena\-i\.com writes​:   Michael> $ perl-devel -wle 'sub n { print @​_; 3 } $foo = n - 1;   Michael> print $foo' -1 3

This is parsed as '$foo = n (-1)'

  Michael> $ perl-devel -wle 'sub n { print @​_; 3 } $foo = n() - 1;   Michael> print $foo'

This isn't.

  Michael> Somebody convince me that this is the way the precedence   Michael> rules are supposed to work out\, because right now it   Michael> appears horribly wrong\, but its late.

It's always been this way. If you don't like the behaviour\, use an explicit prototype. Unprototyped subs default to a 'sub (@​)' prototype.

p5pRT commented 20 years ago

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