in reply to Concurrent lexical scopes?

A solution has prompted itself to me in the form of localizing a hash value from a package hash. Here's some example code *before* filtering
use Sub::Lexical; my sub foo { print "I'm in a lexically scoped sub, hurrah!\n"; } foo();
After filtering ...
local $Sub::Lexical::LEXICALS{foo}; $Sub::Lexical::LEXICALS{foo} = sub { print "I'm in a lexically scoped sub, hurrah!\n"; }; $Sub::Lexical::LEXICALS{foo}->();
This seems to have work exactly as I had it before as it passes the module's test suite. My thanks go to Joost who's node inspired me to think of this solution. Now who ever said local() was mis-used ...

_________
broquaint

Replies are listed 'Best First'.
Re: Re: Concurrent lexical scopes?
by Joost (Canon) on Jun 12, 2002 at 11:12 UTC
    Hi broquaint, I've just downloaded the code from Sub::Lexical and done some tests of my own, and it seems to me that your subs now have local scope:

    #!/usr/bin/perl -w use strict; use lib '.'; use Sub::Lexical; my sub testsub { print "testsub 1 called\n"; } my sub outersub { print "outersub called\n"; testsub(); } for (1) { my sub testsub { print "testsub 2 called\n"; } testsub(); outersub(); }

    This outputs:

    testsub 2 called outersub called testsub 2 called

    While it probably SHOULD give:

    testsub 2 called outersub called testsub 1 called
    Because the first testsub() declaration is in lexical scope for outersub()

    This could be considered a feature, if you just rename your module to Sub::Local... :-)

    -- Joost downtime n. The period during which a system is error-free and immune from user input.
      Ack, you're completely right. In my haste to get a working solution I somehow overlooked the fact that local() != my(), I think the happy part of my brain was muffling the common sense part with a chloroform soaked cloth ;-) So I shall be going back to munging the variable names and also including a hopefully unique enough id within the name. Thanks for the input Joost!
      HTH

      _________
      broquaint