Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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!


In reply to Unexpected difference in whitespace parsing in subroutine calls. by Lexicon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-24 15:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found