http://www.perlmonks.org?node_id=930599


in reply to Stopping a package from infesting my namespace

Seems you can tell the Parse::Token what package to create the symbols in by calling  $parse_token->inpkg($package_name).

I thought the best way would be to specify the tokenClass option for Parse and subclass Parse::Token and do something in the constructor, but it seems there are problems with this ... the instance method tokenClass() of Parse::Lex (inherited from Parse::ALex) sets a "static" variable shared by all instances of any subclass of Parse::ALex so once you tell one instance to use a subclass for its tokens, all will. ...

Seems to me the best you can do is to build and eval a string and hope for the best:

my $rand_pkg = 'AnneliTokens::T' . rand(); my $parser = eval "package $rand_pkg; Parse::Lex->new(\@token);";
I do hope this will infest the rand package, not your package.

Jenda
Enoch was right!
Enjoy the last years of Rome.

Replies are listed 'Best First'.
Re^2: Stopping a package from infesting my namespace
by anneli (Pilgrim) on Oct 10, 2011 at 21:31 UTC

    Hey! Thanks for looking into this so much! I really appreciate it.

    Your findings with regards to tokenClass seem to be echoed throughout the design of Parse::Lex; it doesn't appear very friendly to the notion of more than one being instantiated (which is to say it can work, but you have to tiptoe around it!).

    OTOH, your eval trick looks pretty much perfect! I imagine it'll go into the new package this way (as it'll appear to be the callee), though it's a slightly hacky way of doing it.

    In the end, I just made my lexer non-reentrant, and it hides the unsightliness under the interface. But if I end up going about this another way, it'll likely be as you suggest!

    Thanks again!

    Anne