Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Concurrent lexical scopes?

by broquaint (Abbot)
on Jun 11, 2002 at 12:00 UTC ( [id://173437]=perlquestion: print w/replies, xml ) Need Help??

broquaint has asked for the wisdom of the Perl Monks concerning the following question:

During the development of the Sub::Lexical module I have come across what I guess could be best described as a limitation of the nature of the module. The problem is that there is no slot in the lexical scratchpad for subroutines so for example the following code dies
my sub foo; __output__ "my sub" not yet implemented at - line 1.
And my &foo is an outright syntax error. So the the lexical subs cannot truly be lexical subs but merely scalar references to CODErefs. But this introduces the problem of namespace issues e.g
use Sub::Lexical; my $foo = "a string"; # ack, $foo = sub {}; will run over previous $foo :-/ my sub foo { print "I'm in foo()\n"; }
Currently the workaround is distinctly horrid and involves inserting a double underscore around the variable name so $foo becomes $__foo__. I'm possibly missing some blatently obvious idea or maybe this is an inherent limitation of perl (and with good reason?).
Thanks

_________
broquaint

Replies are listed 'Best First'.
Re: Concurrent lexical scopes?
by Abigail-II (Bishop) on Jun 11, 2002 at 12:16 UTC
    Perl does not have lexical subs. Nor does it have lexical filehandles, dirhandles, typeglobs or formats. The reason is probably historical: C doesn't have lexical functions either.

    Lexical subs have been on the wish list for some time though, but it never has had a high priority. I don't see much use of lexical functions; you can't easily pass them around, you would need a reference to them before you can, and then one could start off with a coderef in the first place.

    Abigail

Re: Concurrent lexical scopes?
by broquaint (Abbot) on Jun 11, 2002 at 14:30 UTC
    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

      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

Re: Concurrent lexical scopes?
by Joost (Canon) on Jun 11, 2002 at 13:31 UTC
    So the the lexical subs cannot truly be lexical subs but merely scalar references to CODErefs. But this introduces the problem of namespace issues e.g

    The namespace issue has nothing to do with the coderefs; the problem is that lexical variables (and by abstraction lexical subs too) are not in any namespace at all! Lexial variables exist in their lexical scope, and lexical scope doesn't give a d*mn about packages.

    Anyway I don't really see that this is much of a problem - the only use for lexical subs I can see is for 'temporary' subs - you would put those somewhere where they go out of scope quickly - or 'private' subroutines, which you would put in a module anyway (and lexical scope is at most file scope)

    -- Joost downtime n. The period during which a system is error-free and immune from user input.
      The namespace issue has nothing to do with the coderefs
      It does because the 'lexical subs' are stored in coderefs which are just scalars and you can't have 2 scalars of the same name in the same lexical scope e.g
      my $foo = "I'm a string"; # this will replace previous $foo my $foo = sub { "I'm a coderef" };
      the problem is that lexical variables (and by abstraction lexical subs too) are not in any namespace at all!
      Yes they are and it is the current lexical scope's namespace (which is anonymous and inaccessible through straight perl).

      _________
      broquaint

        You're right. I guess I'm a little more tired than I thought :-)

        Anyway, I guess you're sort of stuck then. The only thing I can think of, is to use something a little more descriptive than $__name__ - how about $__lex_sub__name ?

        I think it would be quite reasonable to disallow the use of scalars called $__lex_sub__* in combination with your module.

        -- Joost downtime n. The period during which a system is error-free and immune from user input.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://173437]
Front-paged by Dog and Pony
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2024-03-28 11:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found