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

Re: Is it possible to create a sub exclusive to a sub?

by BrowserUk (Patriarch)
on Sep 19, 2004 at 09:58 UTC ( [id://392150]=note: print w/replies, xml ) Need Help??


in reply to Is it possible to create a sub exclusive to a sub?

Here's a way that avoids re-creating the local sub every invocation of the main, and avoids namespace pollution in main. It also allows as many (named) "local" subs as required.

#! perl -slw use strict; sub test{ ## create a 'local' sub the first time we're called ## As many as you like each with it's own name *test->{localsub} = sub{ print 'localsub1'; return 12345; } unless exists *test->{localsub}; ## use it *test->{localsub}(); } print test; ## It's not truly local, but no namespace pollution. ## Anyone doing this, is doing it deliberately *test->{localsub}(); __END__ P:\test>test localsub1 12345 localsub1

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

Replies are listed 'Best First'.
Re^2: Is it possible to create a sub exclusive to a sub?
by Aristotle (Chancellor) on Sep 19, 2004 at 10:19 UTC

    That's just obfuscated syntax for

    sub test { $::test{localsub} = sub { print 'localsub1'; return 12345; } unless exists $::test{localsub}; $::test{localsub}->(); }

    and implies all the usual ramifications of global variable use.

    Makeshifts last the longest.

      *test->{localsub} isn't equivalent to $::test{localsub} in the general case. It just happens to be that in the current package in the code BrowserUk wrote. You probably know that, but I just want to point out that. It's important to realize that *foo doesn't by any means imply the main namespace.

      ihb

      Read argumentation in its context!

        Yes, I know that. I guess I should have added a remark to the effect that I'm not implying that *test->{localsub} is shared across packages. I didn't want to formulate the code using vars or our because that wouldn't have been equivalent — the difference and key point of my argument is that *test->{localsub} is an unchecked use of a global that elides strictures. So I had to use a fully qualified global variable name instead; obviously, the package would have to change according to the context of the code.

        Makeshifts last the longest.

      The only "global variable use", it *main::test, which is in use anyway and has several slots going free. And I guess obfuscated is in the eye of the beholder.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

        You use %main::test. That that's the same as *main::test{HASH} and *main::test{CODE} happens to be defined doesn't make it any less of a global variable. At least if you're going to go that route, do it directly

        use vars qw( %test ); # ... $test{localsub}->();

        so that strict can catch your typos. (Though it still won't catch $test{lcoalsub}, as opposed to a lexical mistyped as $lcoalsub.) The way you wrote it deprives you of protection wholly.

        Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2024-03-29 01:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found