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

Hello,
I accidently gained a warning in my script.
Code:
use warnings; @_ = (undef, 5, 8, undef); print ( (sort {$b <=> $a} grep defined, @_)[ 0 ], " - was a max defined value\n" );
STDOUT: 8 STDERR: print (...) interpreted as function at prog.pl line 5.
How can I avoid such warning? And what does it mean? I can't understand.

Replies are listed 'Best First'.
Re: false warning: print (...) interpreted as function
by choroba (Cardinal) on Nov 06, 2015 at 11:48 UTC
    The easiest way to avoid the warning is to remove the space between print and (.

    splain explains the warning in this way:

    (W syntax) You've run afoul of the rule that says that any list operator followed by parentheses turns into a function, with all the list operators arguments found inside the parentheses. See "Terms and List Operators (Leftward)" in perlop.

    Updated:The warning would be useful if you closed the outer parentheses earlier:

    print ((sort { $b <=> $a } grep defined, @_)[0]), " - was a max define +d value\n";

    The common solution is not to use another parentheses, but to insert a +:

    print +(sort { $b <=> $a } grep defined, @_)[0], " - was a max defined + value\n";
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: false warning: print (...) interpreted as function
by hippo (Archbishop) on Nov 06, 2015 at 11:41 UTC
        If this warning sounded like: "print ([^)]*) interpreted as function" or "print (...) interpreted as function, where '...' is qr/[^)]*/" - it would be understandable. Because I don't know what do three points ('...') mean and am thinking that a warning is unnecessary.
Re: false warning: print (...) interpreted as function
by Discipulus (Canon) on Nov 06, 2015 at 12:51 UTC
    And if you use really AS function?
    perl -Mstrict -wE "my $x = print ('string'); say ''; say $x" string 1 perl -Mstrict -wE "print('string')" string perl -Mstrict -wE "print ('string')" print (...) interpreted as function at -e line 1. string
    No warning are issued in first and second examples.

    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      It depends on what you print, too:
      my $x = print ( (sort {$b <=> $a} grep defined, @_)[ 0 ], " - was a max defined value\n" );
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        According to the Camel Book (4th Edition, 2012, p. 918), parentheses around all the arguments should be ok, even with a space between the print and the opening left parenthesis:

        Also, remembering the if-it-looks-like-a-function-it-is-a-function rule, be careful not to follow the print keyword with a left parenthesis unless you want the corresponding right parenthesis to terminate the arguments to the print. Instead, interpose a + or put parens around all the arguments:
        print (1+2)*3, "\n"; # WRONG print +(1+2)*3, "\n"; # ok print ((1+2)*3, "\n"); # ok

        I’m guessing that the warnings pragma is made suspicious when the closing right parenthesis either (1) is followed by other expressions, or (2) doesn’t appear on the same line as the opening left parenthesis — as in the example cited.

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,