in reply to Perl Object as Hash, Array & Scalar in the same time! ( $O->{k} | $O->[0] | $O )

Don't do it, at least not like that.

Global variables under the guise of being lexicals just so you can have the benefit of globs?

I say get perlxs involved, that way a lexical remains really a lexical, and not *Symbol::GEN0.

update: Hey, that'd make a good module, Lexical::Typeglob, right up there with Lexical::Alias.


MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
** The Third rule of perl club is a statement of fact: pod is sexy.

  • Comment on Re: Perl Object as Hash, Array & Scalar in the same time! ( $O->{k} | $O->[0] | $O )

Replies are listed 'Best First'.
Re: Re: Perl Object as Hash, Array & Scalar in the same time! ( $O->{k} | $O->[0] | $O )
by gmpassos (Priest) on May 10, 2003 at 01:36 UTC
    Well, I know that Symbol::gensym keep a reference to an global GLOB at Symbol::GENx. What is not very good at POO. But for GLOB you need to keep it in some point!

    To fix that the code need a DESTROY that destroy the GLOB too.

    Note that you really need to put the different types inside the GLOB! You can't use a hash ref, since you want to do: $this->{key}, and this will go to get_hash(), and inside it you can't use $this->{key} or $this->[0], or you make an infinity loop of nothing. So, if you bless a hash, but make the access of it using overload you can't access again the hash, specially if you want to use overload for ARRAY access too!!! This is why I use GLOB, since is the only way that I found to access data inside the object wihtout use ways that are overloaded.

    If you can find a way to make the same thing without Symbol::gensym (GLOB), please send! ;-P

    Update:
    From POD: "Symbol::gensym creates an anonymous glob and returns a reference to it"!
    The GLOB is already anonymous! Test to creat multiples GLOB from gensym(), you can see that is always *Symbol::GEN0.

    Graciliano M. P.
    "The creativity is the expression of the liberty".

      No, clearly every gensym object occupies a new GEN# slot in the Symbol package. Also - DESTROY is completely innappropriate here. You cannot call ->DESTROY on an object and have it actually free the object. The ->DESTROY method is called by perl when the object is being destroyed - you can't provoke it by calling it yourself.

      perl -MB -MSymbol -e '$,=$\="\n";push @A,gensym for 0 .. 10;@B=map B::svref_2object($_), @A; print $_->STASH->NAME."::".$_->NAME for @B'
        I said that the DESTROY is to DESTROY the GEN# with the OBJ (in the same time), when Perl make the garbage collection of the block. Not to call it by hand!

        Graciliano M. P.
        "The creativity is the expression of the liberty".