in reply to Re: Any differences between method and method()?
in thread Any differences between method and method()?

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" }

Replies are listed 'Best First'.
Re^3: Any differences between method and method()? (=>)
by tye (Sage) on Aug 18, 2012 at 18:15 UTC

    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        

      ... the other bareword cases are affected by whether or not a prototype is used ...

      I don't get this point. While certainly inconsistent, the inconsistencies do not seem to be affected at all by whether prototyping is or is not used.

      thanks for your detailed reply. I have one more question. What do the syntax +FUD and -FUD mean here?

        -FUD is intended (by the authors of Perl) to emulate the  -whatever syntax of Tcl/Tk options. The leading '-' is appended to the string into which the  FUD bareword is converted in the presence of the fat comma, any stricture against such conversion being locally nullified.

        +FUD is intended (by various monks replying) to illustrate the effect of the unary  + operator on the string into which the  FUD bareword is converted in the presence of fat comma: none at all. Contrast this with the effect of the  ! logical negation operator in the  !FUD case.

        To answer your question more generally, the syntax +FUD and -FUD (and !FUD and ~FUD and maybe some others) means that someone had a bright (and well intended) idea and far too much time on their hands, and now we're stuck with it.

        :)

        + means + and - means -

        + stringifies to "" while - stringifies to "-"

Re^3: Any differences between method and method()?
by tobyink (Canon) on Aug 18, 2012 at 21:13 UTC

    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'

      And then there's also the one they call the "weeping programmer":

      >perl -wMstrict -le "use constant KEY => 'foobar'; use Data::Dumper; print Dumper({ KEY ,,,,,, 'value' }); " $VAR1 = { 'foobar' => 'value' };