vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

print_argu "Hello function!\n"; sub print_argu{ print "@_\n"; }

Above code is not working for me, Then I put parentheses its working fine.But

sub print_argu{ print "@_\n";} print_argu "Hello function!\n";

In the second code I called this function after the definition of the function, without parentheses, Is it the problem with calling the function?

Replies are listed 'Best First'.
Re: function calling
by Bloodnok (Vicar) on Apr 04, 2009 at 10:24 UTC
    The problem is, as you appear to have noticed, down to the order of calling and declaring the function.

    If the declaration precedes any call, perl already knows that print_argu arg,...; is a call to a sub, whereas if the order is reversed i.e. the call precedes the declaration, perl needs a hint as to what sort of symbol print_argu represents. In this case, the hint can be either...

    • The addition of the parens in the call or...
    • The declaration of the sub prototype (sub print_argu;) before the call
    i.e. either of the following will work
    print_argu("Hello function!\n"); sub print_argu{ print "@_\n"; }
    or
    sub print_argu; print_argu "Hello function!\n"; sub print_argu{ print "@_\n"; }

    That being said, it is recommended, in PBP, that the parens are omitted only for calls to builtins.

    A user level that continues to overstate my experience :-))
Re: function calling
by nagalenoj (Friar) on Apr 04, 2009 at 11:24 UTC
    For a subroutine call, Parentheses are optional if predeclared/imported.

    So, in the second case it is optional and in the first case it is not optional.

    Resource: man perlsub

Re: function calling
by Anonymous Monk on Apr 04, 2009 at 08:59 UTC
    Above code is not working for me,
    It should be complaining along the lines of String found where operator expected at - line 1, near "print_argu "Hello function!\n"", and if you add diagnostics
    String found where operator expected at - line 1, near "print_argu "He +llo function!\n"" (#1) (S syntax) The Perl lexer knows whether to expect a term or an ope +rator. If it sees what it knows to be a term when it was expecting to see + an operator, it gives you this warning. Usually it indicates that an operator or delimiter was omitted, such as a semicolon. (Do you need to predeclare print_argu?) syntax error at - line 1, near "print_argu "Hello function!\n"" Execution of - aborted due to compilation errors (#2) (F) Probably means you had a syntax error. Common reasons include +: A keyword is misspelled. A semicolon is missing. A comma is missing. An opening or closing parenthesis is missing. An opening or closing brace is missing. A closing quote is missing. Often there will be another error message associated with the synt +ax error giving more information. (Sometimes it helps to turn on -w. +) The error message itself often tells you where it was in the line +when it decided to give up. Sometimes the actual error is several toke +ns before this, because Perl is good at understanding random input. Occasionally the line number may be misleading, and once in a blue + moon the only way to figure out what's triggering the error is to call perl -c repeatedly, chopping away half the program each time to se +e if the error went away. Sort of the cybernetic version of S<20 questions>. Uncaught exception from user code: syntax error at - line 1, near "print_argu "Hello function!\n"" Execution of - aborted due to compilation errors. at - line 4