Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Symbol ? Re: Sub::Lexical

by Corion (Patriarch)
on May 15, 2002 at 20:52 UTC ( [id://166845]=note: print w/replies, xml ) Need Help??


in reply to Sub::Lexical

I haven't really played with it, but maybe Symbol (even in the core distribution) could be of help here - it generates anonymous globs, although not in the namespace you'd propably like (which ruins the 'Lexical' part of the module I guess):

perl -MSymbol -e "print ${gensym()} for 1 .. 10"

Most likely, you'll have to come up with a good identifier scheme, maybe by mucking around with variable names with spaces in them or simply dieing, if a variable with your chosen prefix is used.

Also, I'm not really sure if all that the module promises is true, as I believe that things get nasty if you nest subs two levels deep (or deeper) - that's one thing I miss from Pascal.

perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web

Replies are listed 'Best First'.
Re: Symbol ? Re: Sub::Lexical
by broquaint (Abbot) on May 15, 2002 at 23:01 UTC
    maybe Symbol (even in the core distribution) could be of help here
    I'll try incorporating that at some point to see how it fares, it will certainly save on the variable munging trickery.
    Update: on second thoughts, this won't save variable name munging at all as I still have to create a lexical variable to assign to the glob etc etc If only I could create my own temporary lexical scopes to declare them in, hmmm ...
    I believe that things get nasty if you nest subs two levels deep (or deeper) - that's one thing I miss from Pascal.
    Nope, that's the beauty of lexical subs - you can nest to your hearts content!
    use Sub::Lexical; ## can't nest package level subs sub foo { my $foovar = "in foo here\n"; my sub bar { my $barvar = "in bar here\n"; print "\$foovar = $foovar"; my sub baz { my $bazvar = "in baz here\n"; print "\$barvar = $barvar"; my sub quux { my $quuxvar = "in quux here\n"; print "\$bazvar = $bazvar"; my sub ooh_this_is_deep { my $ooh_this_is_deepvar = "in deep here\n"; print "\$otidv = $ooh_this_is_deepvar"; } ooh_this_is_deep(); } # ooh_this_is_deep falls outta scope here quux(); } # quux falls outta scope here baz(); } # baz falls outta scope here bar(); } # bar falls outta scope here foo(); __output__ $foovar = in foo here $barvar = in bar here $bazvar = in baz here $otidv = in deep here
    The problem with nesting subs is explored here and this module fixes that issue to a degree by hiding by the nested subs as anonymous subs tied to lexicals variables, so you're not really nesting subs at all (but who's the wiser ? ;-)
    HTH

    _________
    broquaint

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://166845]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found