Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Stopping a package from infesting my namespace

by Jenda (Abbot)
on Oct 10, 2011 at 11:59 UTC ( #930599=note: print w/replies, xml ) Need Help??

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.

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!


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://930599]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2018-02-19 00:11 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (257 votes). Check out past polls.