I had things working and then decided to break this program into 3 parts. The 1st part had a jump-table, of sorts, where a bunch of anon subs were called depending on what keyword matched. Some were named because they were reused by other routines.

When I turned the 1st part into its own package, the call to the subs via the "local" name, stopped working.

Created 2 call types in a test. asub can only be called if I don't declare it w/local -- Can't I declare it?

bsub is local to a procedure, for it, the local call has no effect (bsub is callable from within "do_internal_sub" either way. Oddly, even though the "local *bsub" would seem to include "stuff" in its lexical scope, instead of the eval telling me that bsub was not set, it says it can't find bsub.

So why does the "local *bsub" have no effect and why does the inclusion of "local *asub" prevent it from being callable in "stuff", below?

#!/usr/bin/perl { package Do::Stuff; use warnings; use strict; use P; sub setup{my $p=shift; my $c=ref $p||$p; bless $p={}, $c unless ref $p; $p; } #local *asub; *asub = sub { P "asub called with %d param%s%s.", 0+@_, -1+@_?"s":" +", @_ ? P(" (%s)", \@_) : ""; 1}; local *bsub; sub do_internal_sub() { my $p=shift; *bsub = sub{ P "Called with %d param%s%s.", 0+@_, @_-1?"s":"", @_ ? P(" (%s)", \@_) : ""}; P "calling bsub: %s", &bsub("one",2,"III"); } sub stuff() { my $p=shift; P "In stuff"; eval {&asub("stuff")}; if ($@) { P "asub call gave: $@"; } eval {&bsub("more", "stuff")}; if ($@) { P "bsub call gave: $@"; } } 1} package main; use warnings; use strict; use P; my $p=Do::Stuff->setup; P "calling stuff..."; $p->stuff; P "calling internal_sub: %s", $p->do_internal_sub;

P.s - Obviously, I can hack together something that will work, but there sure seem to a few inconsistencies which always tend to bug me... Thanks!


In reply to how to declare a local *subname=sub{}? by perl-diddler

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.