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

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

by ihb (Deacon)
on Sep 19, 2004 at 10:40 UTC ( [id://392156]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Is it possible to create a sub exclusive to a sub?
in thread Is it possible to create a sub exclusive to a sub?

*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!

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

    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.

      Which strictures?

      #! 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 WRONG!! *test->{lacolsub}(); *tset->{localhost}(); } print test; __END__ P:\test>test Name "main::tset" used only once: possible typo at P:\test\test.pl lin +e 14. Use of uninitialized value in subroutine entry at P:\test\test.pl line + 13. Can't use string ("") as a subroutine ref while "strict refs" in use a +t P:\test\test.pl line 13.

      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

        #!/usr/bin/perl -l use strict; use warnings; use vars qw( %test ); sub test{ $test{localsub} = sub{ print 'localsub1'; return 12345; } unless exists $test{localsub}; $tset{localsub}->(); } print test; __END__ Global symbol "%tset" requires explicit package name at t.pl line 12. Execution of t.pl aborted due to compilation errors.

        Makeshifts last the longest.

        janitored by ybiC: Prepend "(Duplicate)" to nodetitle to indicate that this node is exact duplicate of Re^6: Is it possible to create a sub exclusive to a sub? (id://392165) above

        #!/usr/bin/perl -l use strict; use warnings; use vars qw( %test ); sub test{ $test{localsub} = sub{ print 'localsub1'; return 12345; } unless exists $test{localsub}; $tset{localsub}->(); } print test; __END__ Global symbol "%tset" requires explicit package name at t.pl line 12. Execution of t.pl aborted due to compilation errors.

        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://392156]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-24 20:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found