in reply to Lexical use/no Module?

Of course there is nothing to stop you from just mangling the symbol table at runtime. It is not a specifically compile time practice. (Come on try it,.. all the cooool kids are doing it) And with a little creativity and just the right amount of insanity you could make this work.

I saw this trick in Hook::LexWrap, by TheDamian, its quite simple really and very elegant. The code would look something like this:

use Bling; $x = $$; ## Bling Bling as before { my $no_bling = Bling->off(); $y = $$; ## no Bling Bling } $z = $$; ## Bling Bling as before
The magic (oooohhhh spooky) is that the variable $no_bling is lexically scoped. It is also an object instance. When the object is created in Bling->off() it unties $$. The DESTORY method of the object instance then re-tie's $$. Since DESTROY will get called at the close of the block,... whalla,.. you get lexically scoped insanity.

If you dont like the Bling->off() syntax, then you could try something witty with the indirect object syntax. buh_bye Bling or something like that. (although you can't use "no" cause perl will confuse it with the real "no")

Of course, no one in their right mind would do such a thing. No no,.. never,... never ever,.. no ,.. stop,.. dont make me,.. nooooooooooo......

-stvn

Disclaimer: In no way does this post consitute an endorsement of the practice of mangling perl's global variables. Bad,.. very very Bad.

Replies are listed 'Best First'.
Re: Re: Lexical use/no Module?
by Stevie-O (Friar) on Jan 29, 2004 at 03:52 UTC
    Ahh, a most interesting idea.

    And as for your disclaimer, I'd be surprised to find someone who *would* promote hacking the symbol table. But I like to boldly go where no man has gone before (or do I rush in where angels fear to tread?). I even once replaced UNIVERSAL::can ;) Stevie-O's scratchpad

    --Stevie-O
    $"=$,,$_=q>|\p4<6 8p<M/_|<('=> .q>.<4-KI<l|2$<6%s!<qn#F<>;$, .=pack'N*',"@{[unpack'C*',$_] }"for split/</;$_=$,,y[A-Z a-z] {}cd;print lc
      Stevie-O

      Don't get me wrong, I love hacking the symbol table. Personally, I tend to do it more often than I should as well. What I do not advocate however, is messing around with Perl's global variables ($$ in your example). I assume you used it as an example only, since it's doubtful that having your program print "Bling Bling" everytime you try to find out the process id is really what you are going for.

      Personally I never thought symbol tables were all that complex or magical. Its just a bunch of hashes which represents a heirarchy of namespaces. Different paradigm than "normal" maybe but magical, ... nah not so much :)

      -stvn
Re: Re: Lexical use/no Module?
by QwertyD (Pilgrim) on Jan 29, 2004 at 19:55 UTC

    The problem is, Hook::LexWrap isn't actually lexically scoped. As Dominus says in his proposal, Hook::LexWrap is lying (which isn't to say it's a bad module at all, it's just not *quite* lexically scoped).

    For example,

    { my $lexical = wrap 'some_sub', pre => \&wrapper; some_sub();#wrapper is called before some_sub other_sub();#other_sub's call to some_sub is *still* #wrapped with &wrapper, even though other_sub is #outside of this lexical scope! } sub other_sub { some_sub();#call made outside of lexical scope in which some_sub is +wrapped. }
    Basically, theres' no good way to implement truly lexically-scoped modules right now.

    However:

    Dominus is working to change this. Here's the HTMLified POD from his proposal patch for Proper Lexical Pragma Modules.:



    Once it's Turing complete, everything else is just syntactic sugar.

      Quite true. But then again, one would need to be insane to actually do something like this and expect it to behave nicely. And short of kidnapping Dominus and forcing him to patch and recompile your version of perl, its the next best thing. And I quote myself:

      Of course, no one in their right mind would do such a thing. No no,.. never,... never ever,.. no ,.. stop,.. dont make me,.. nooooooooooo......

      -stvn