Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

private sub routine

by saranperl (Initiate)
on Sep 02, 2009 at 11:13 UTC ( [id://792875]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: private sub routine
by moritz (Cardinal) on Sep 02, 2009 at 11:16 UTC

    See perlintro, section "Writing subroutines"

    You should read the rest of that document too.

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: private sub routine
by rovf (Priest) on Sep 02, 2009 at 11:39 UTC

    I guess you mean "not visible outside current scope"? You could create a reference to an anonymous subroutine and assign it to a lexical variable.

    -- 
    Ronald Fischer <ynnor@mm.st>
Re: private sub routine
by cdarke (Prior) on Sep 02, 2009 at 15:00 UTC
    my $private_subroutine = sub { print "Hollow World\n" }; &$private_subroutine();
      &$private_subroutine();

      I'm a bit fuzzy on this, but doesn't &$private_subroutine() have subtle issues with @_ that $private_subroutine->() doesn't?

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

        No, not if you include the parens.
        $ perl -le'sub foo { print 0+@_ } @_=1..4; &foo; &foo();' 4 0
Re: private sub routine
by Marshall (Canon) on Sep 02, 2009 at 22:52 UTC
    This is an "anti-Perl" idea -> a wrong one. In Perl a sub name has "package" scope, usually, but not always one package "name space" per file.

    In a function oriented package, the subs that are meant to be called by others are specified in the @EXPORT (), @EXPORT_OK() lines. In an OO module, you will see subs with names that begin with "_". Those are often names of methods (subroutines) that shouldn't be called by outsiders.

    Basically, I suggest "play by the rules and get along with others". I think you are doomed to a life of unhappiness and pain if you insist that Perl work like your other language, which I am guessing is Java. On the plus side, a Perl program is often 1/10th or less than the size of a Java program, so there is less to keep track of!

    Perl does allow you to "shoot yourself in the foot". If you provide a "fully qualified name", you can call that sub X in spite of naming and export conventions. I would say simply: "don't do that".

      Funny, saranperl asks one of his overly condensed interview questions without any further explanation and he gets answers with desperate interpretations like in a interview.

      Is perlmonks an interview? Do we apply for a job?

      You're thinking of "private" like in Java and most of the others of "private" like in lexical variables.

      BTW: Googling for "private function" and "perl" quickly leads to perlmod

      # here's a file-private function as a closure, # callable as &$priv_func; it cannot be prototyped. my $priv_func = sub { # stuff goes here. };

      So why do we answer a question which can be easily resolved with googling???

      Cheers Rolf

        I didn't investigate saranperl's previous posts.
        Yes, there is a way to make a Perl "private sub".
        The part that wouldn't be resolved via Google is that this is a bad idea in Perl. Now that's my personal opinion, but I'm sticking to it and for good reasons.
Re: private sub routine
by Anonymous Monk on Sep 02, 2009 at 11:24 UTC
    Can you define what that is?

    Also read perlsub

Re: private sub routine
by astroboy (Chaplain) on Sep 03, 2009 at 02:15 UTC
    Attribute::Protected should do exactly what you want - assuming what you want is to distinguish between private, protected and public methods....

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 17:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found