Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Any differences between method and method()?

by sophate (Beadle)
on Aug 18, 2012 at 10:52 UTC ( #988195=perlquestion: print w/ replies, xml ) Need Help??
sophate has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

Are there any differences between method calls of no arguments with and without ()? For example:

# Are they the same? $object->method; $object->method(); # Are they the same? open my $FileHandle1, "/tmp/file1"; $FileHandle1->close; open my $FileHandle2, "/tmp/file2"; $FileHandle2->close();

Thanks in advance.

Comment on Any differences between method and method()?
Download Code
Re: Any differences between method and method()?
by influx (Beadle) on Aug 18, 2012 at 11:06 UTC
    If you've predeclared the subroutine you can leave the '()' off. I generally use it anyway so any maintainers of my code KNOW its a subroutine without even thinking.
      If you've predeclared the subroutine you can leave the '()' off.
      That's only relevant for function calls; for method calls, predeclaring makes no difference. And to the OP, there's no difference whether you have the () or not.

      Dave.

Re: Any differences between method and method()?
by tobyink (Abbot) on Aug 18, 2012 at 11:23 UTC

    For function calls (i.e. non-OO) the parentheses are sometimes necessary. I can never remember the exact formulations - depends very much on whether strict subs are enabled, and on prototypes. For function calls, best to always include the parentheses, unless it's a constant sub, or you are sure that the sub has been defined with an empty prototype.

    For method calls on the other hand, if there are no arguments being passed, the parentheses are entirely unnecessary. Use whatever you think looks nicest.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      For function calls (i.e. non-OO) the parentheses are sometimes necessary. I can never remember the exact formulations - depends very much on whether strict subs are enabled, and on prototypes. For function calls, best to always include the parentheses, unless it's a constant sub, or you are sure that the sub has been defined with an empty prototype.     [emphases added]

      Unfortunately, there is a happy little cluster of corner cases associated with the  => 'fat comma' operator (see perlop) that are not affected by (or perhaps I should say, that interact in a complex way with) prototyping or strictures.

      In the example below,  FOO() and  'FOO' (quoted string) are never surprising, but try commenting out successive hash elements from right to left. Try replacing  +FOO with  !FOO or  -FOO instead. Replacing the
          use constant FOO => 'foo';
      statement with the prototyped function
          sub FOO () { 'foo' }
      (which is essentially what constant creates for you) makes no difference.

      Happy debugging!

      >perl -wMstrict -le "use constant FOO => 'foo'; ;; my %hash = ( FOO() => 'ok', FOO => 'oops', +FOO => 'huh?', 'FOO' => 'yes', ); ;; use Data::Dump; dd \%hash; " { foo => "ok", FOO => "yes" }

        Yes, as you can see from this demonstration code:

        #!/usr/bin/perl -w use strict; my $list = ' FUD() => "()", "FUD" => "str", FUD => "bare", -FUD => "-", +FUD => + "+", '; sub FUD { 'fud' } for( 0 .. 1 ) { my @a = eval $list; print "@a\n"; last if $_; undef &FUD; eval 'sub FUD() { "dud" }'; } __END__ fud () FUD str FUD bare -FUD - FUD + dud () FUD str FUD bare -FUD - FUD +

        While the FUD() case always calls the function (and thus gives 'fud' then 'dud') and the "FUD" case always doesn't call the function (and thus always gives 'FUD'), the other bareword cases are affected by whether or not a prototype is used and so give the follow inconsistent results:

        FUD +FUD -FUD ----- ----- ------ w/ prototype: FUD FUD -FUD w/o prototype: FUD FUD -FUD
        Happy debugging!

        Like anybody could possibly debug such wily inconsistency! So funny.

        Update: Sorry, I accidentally reversed the "w/" vs "w/o" labels in the above table. Sorry for the confusion that must have caused.

        - tye        

        You may be interested in the "winking fat comma"...

        use constant KEY => 'foobar'; use Data::Dumper; print Dumper({ KEY ,=> 'value' });

        Looks like a fat comma, works like a regular comma.

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Any differences between method and method()?
by toolic (Chancellor) on Aug 18, 2012 at 16:42 UTC

    According to B::Deparse, they seem to be treated the same (Tip #6 from the Basic debugging checklist):

    $ perl -MO=Deparse -MDateTime -e 'DateTime->now' use DateTime; 'DateTime'->now; -e syntax OK $ perl -MO=Deparse -MDateTime -e 'DateTime->now()' use DateTime; 'DateTime'->now; -e syntax OK

      Thank you all for your reply.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2014-11-26 23:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (177 votes), past polls