Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

'function' calls without parentheses

by g0n (Priest)
on Mar 21, 2006 at 16:36 UTC ( [id://538221]=perlquestion: print w/replies, xml ) Need Help??

g0n has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I have some code that uses Term::ReadKey to get some user input, but 99 times out of 100 when this script is run, it picks the data up from a file instead, and Term::Readkey isn't needed. So I figure why not do away with the dependency, and require Term::ReadKey in when needed.

But the sub that uses it contains a couple of statements like this:

ReadMode 1; # some stuff ReadMode 2;

Intrigued by this - apparently a function call without parentheses - I asked in the CB, and was told that the parentheses are optional for a single param function call. But this test case:

testcall 1; testcall(2); sub testcall { print $_[0]; print "\n"; }

fails to compile with:

Number found where operator expected at fcall.pl line 1, near "testcal +l 1" (Do you need to predeclare testcall?) syntax error at fcall.pl line 1, near "testcall 1" Execution of fcall.pl aborted due to compilation errors.

(On AS perl, 5.6.1). Term::ReadKey doesn't appear to use prototypes incidentally.

I'm intrigued - is there a functional difference between ReadMode 1; and ReadMode(1); and if so, what? Or am I being really, really dense?

Thanks.

Update: thanks to duff. I'd sort of always assumed that

callsub(); sub callsub{#dostuff}
and
sub callsub(){#dostuff} callsub();
were equivalent, and never encountered anything that proved me wrong. I know better now :-)

--------------------------------------------------------------

"If there is such a phenomenon as absolute evil, it consists in treating another human being as a thing."
John Brunner, "The Shockwave Rider".

Can you spare 2 minutes to help with my research? If so, please click here

Replies are listed 'Best First'.
Re: 'function' calls without parentheses
by duff (Parson) on Mar 21, 2006 at 16:43 UTC

    Put the sub definition before the call and it'll work just fine. If perl doesn't recognize the name, it treats it as a bareword.

Re: 'function' calls without parentheses
by McDarren (Abbot) on Mar 21, 2006 at 16:50 UTC
    Quoting directly from the Camel (3rd Ed, Page 218):
    NAME(LIST) # & is optional with parentheses. NAME LIST # Parens optional if sub predeclared/imported. &NAME # Exposes current @_ to that subroutine, # (and circumvents prototypes).
    Cheers,
    Darren :)
Re: 'function' calls without parentheses (link)
by tye (Sage) on Mar 21, 2006 at 16:47 UTC
Re: 'function' calls without parentheses
by Errto (Vicar) on Mar 21, 2006 at 21:45 UTC
    One other thing not mentioned so far. What you were told in the CB isn't quite right. A subroutine called without parentheses or & is a list operator (except in the special case where you use the () or ($) prototypes), which means that it can take any number of arguments:
    sub foo { # do stuff } foo 1, 2, 3, 4;
    is perfectly valid.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://538221]
Approved by friedo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (10)
As of 2024-04-23 08:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found