Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Quick question on a Perl Code!

by dudydude (Novice)
on Jul 13, 2011 at 21:36 UTC ( [id://914236]=perlquestion: print w/replies, xml ) Need Help??

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

Hi fellow Monks, I am reading a code that somebody wrote and I am trying to make sense of it.

What does it mean if they pass a ($) or ($$) to a subroutine?

For Example::

sub mysub1($)

sub mysub2($$)

PLEASE NOTE I DIDN'T WRITE THE WHOLE CODE INSIDE, BUT IT IS A SUBROUTINE

WHAT DOES $ STAND FOR?

Replies are listed 'Best First'.
Re: Quick question on a Perl Code!
by AnomalousMonk (Archbishop) on Jul 13, 2011 at 23:30 UTC
Re: Quick question on a Perl Code!
by Anonymous Monk on Jul 13, 2011 at 21:43 UTC

    Those aren't arguments, they're prototypes

    perldoc -f sub

    sub NAME BLOCK
    sub NAME (PROTO) BLOCK
    sub NAME : ATTRS BLOCK
    sub NAME (PROTO) : ATTRS BLOCK
            This is subroutine definition, not a real function *per se*.
            Without a BLOCK it's just a forward declaration. Without a NAME,
            it's an anonymous function declaration, and does actually return
            a value: the CODE ref of the closure you just created.
    
            See perlsub and perlref for details about subroutines and
            references, and attributes and Attribute::Handlers for more
            information about attributes.
    

      They are subroutines, I just didn't bother to copy and paste the whole code

        Reread what he said.

        Arguments are what you pass to a subroutine. They're not being passed to the subroutine as you said. They're part of the subroutine's declaration.

        I'll elaborate
        sub fudge($){ print @_ }
        that is a function declaration -- it doesn't print anything
        fudge(1); fudge 2;
        those are function calls, it prints 1 then prints 2

        If you wrote

        fudge 1,2;
        the prototype ($) ensures fudge only gets 1 argument

        If you try to force the issue with

        fudge(1,2);
        You'll get an error
        $ perl -le " sub fudge($){print @_} fudge(1,2); Too many arguments for main::fudge at -e line 1, near "2)" Execution of -e aborted due to compilation errors.
        Next time, when you get links, follow them, read them, its what we all do :)
Re: Quick question on a Perl Code!
by bart (Canon) on Jul 14, 2011 at 11:24 UTC
    The thing is a "prototype", see the "sub" keyword that gives away the fact that it's not a function call, but a function declaration.

    Sub prototypes were an experiment in Perl, and most experienced Perlers seen to agree we'd better classify it under the "mostly failed experiments".

    See my archived copy of Tom Christiansen's old (but excellent and still very relevant) comprehensive article about prototypes, in FMTEYEWTK about prototypes.

Re: Quick question on a Perl Code!
by sundialsvc4 (Abbot) on Jul 14, 2011 at 01:07 UTC

    (rolling his eyes...)   It is not the most intuitive syntax in the known universe ... but it’s what made the cut, and it does work.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-25 15:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found