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

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

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

    #!/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

Re^6: Is it possible to create a sub exclusive to a sub?
by Aristotle (Chancellor) on Sep 19, 2004 at 11:10 UTC

    #!/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.

      So? I didn't say your way wouldn't work too.

      Though it does require a distant, non-intuative (and unnecessary!) extra declaration of %test and %foo and %bar and %xxx for every other sub that has local subs--which is why I didn't go that route.

      Your way buys me nothing, and requires extra.


      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

        Maybe you missed the fact that the first two of the messages you got were merely non-fatal warnings, and that all three of them did not happen until runtime. The message I got was compile time. Why are you using strict at all, if these arguments don't hold any weight with you?

        If distant declarations bother you, use our.

        #!/usr/bin/perl -l use strict; use warnings; sub test{ our %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.

        Update: I wrote our $test{localsub} = # ... at first, which is clearly wrong.

        Makeshifts last the longest.