Lexicon has asked for the wisdom of the Perl Monks concerning the following question:
There seems to be a difference in the parsing if you put whitespace in between a subroutine name and the subroutine parameters. I imagine this is known, and probably even documented somewhere, but I haven't been able to find it. Can anyone explain the difference?
In case it's a bug: I've run the following code on two machines:
This is perl, v5.8.5 built for i386-linux-thread-multi
This is perl, v5.8.6 built for darwin-thread-multi-2level
#!/usr/bin/perl print (sort ( f( 0 ) )); print (sort ( f ( 0 ) )); print (sort ( &f ( 0 ) )); print (sort f( 0 ) ); print (sort f ( 0 ) ); print (sort &f ( 0 ) ); sub f { 1 }
And get the following results
Chromium:~ lexicon$ perl -l little.pl 1 0 1 0 0 1
Update: No offense guys, but you should read the existing comments before replying to a thread. broquaint had already subtly clued me in that f gets called as the sort subroutine in some cases and noth others. I still find the difference surprising, but etcshadow addressed it as just one of those "crazy rules" perl has for ambiguous situations. etcshadow also pointed out the no op unary + operator which I had been using for years for subroutine calls as my %hash = %{+shift};. Although, really, sort &f(0) does the job and has the bonus of looking correct as well.
Update 2: I thought I should also mention, to those who aren't aware, that this will apply to a lot of situations with built in functions. print will try to call your function to get a file handle, for instance.
All the help is much appreciated, of course!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Unexpected difference in whitespace parsing in subroutine calls.
by broquaint (Abbot) on Jul 10, 2005 at 10:24 UTC | |
by Lexicon (Chaplain) on Jul 10, 2005 at 11:20 UTC | |
Re: Unexpected difference in whitespace parsing in subroutine calls.
by revdiablo (Prior) on Jul 10, 2005 at 15:36 UTC | |
Re: Unexpected difference in whitespace parsing in subroutine calls.
by etcshadow (Priest) on Jul 10, 2005 at 16:27 UTC | |
Re: Unexpected difference in whitespace parsing in subroutine calls.
by pg (Canon) on Jul 10, 2005 at 16:19 UTC | |
Re: Unexpected difference in whitespace parsing in subroutine calls.
by holli (Abbot) on Jul 10, 2005 at 09:45 UTC | |
Re: Unexpected difference in whitespace parsing in subroutine calls.
by jhourcle (Prior) on Jul 10, 2005 at 23:18 UTC | |
by Lexicon (Chaplain) on Jul 10, 2005 at 23:52 UTC |