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

Can anyone explain why a subroutine in perl cookbook section 7.23 has the form

sub sysreadline(*;$) {
in its parameters what is *;$ is it a special glob. thank you

Replies are listed 'Best First'.
Re: strange form in sub parameters
by ikegami (Patriarch) on Mar 31, 2015 at 21:17 UTC

    The * prototype forces the argument expression to be evaluated in scalar context. Furthermore, if the argument expression is an identifier but not an operator, the identifier will be passed as a string ("auto-quoted"). This allows one one to create subs with the same interface as open and read.

    The $ prototype forces the argument expression to be evaluated in scalar context.

    The semi-colon separates the required arguments (on the left) from the optional ones (on the right).

    sysreadline($foo) Passes $foo sysreadline(FOO) Passes "FOO" sysreadline($foo, $bar) Passes $foo and $bar sysreadline(FOO, $bar) Passes "FOO" and $bar
Re: strange form in sub parameters
by CountZero (Bishop) on Apr 01, 2015 at 06:11 UTC
    *;$ is the prototype definition of this subroutine and you can read all about it through the links already quoted above.

    However, don't start using prototypes everywhere. They are rarely needed and in many places unneccessary or even wrongly used.

    So the main rule is: don't use prototypes unless you certainly know what you are doing. And its use is NOT about checking the type of the sub's parameters.

    In all honesty I can say that in all the years I have been programming Perl, I may have used prototypes once or twice only and I'm not entirely sure they were really necessary.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: strange form in sub parameters
by Anonymous Monk on Mar 31, 2015 at 22:12 UTC