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.
| [reply] |
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>
| [reply] [d/l] |
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();
| [reply] [d/l] |
|
&$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
| [reply] [d/l] [select] |
|
No, not if you include the parens.
$ perl -le'sub foo { print 0+@_ } @_=1..4; &foo; &foo();'
4
0
| [reply] [d/l] |
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". | [reply] |
|
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???
| [reply] [d/l] |
|
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.
| [reply] |
|
|
|
Re: private sub routine
by Anonymous Monk on Sep 02, 2009 at 11:24 UTC
|
Can you define what that is?
Also read perlsub | [reply] |
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.... | [reply] |