Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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.

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


Comment on Re: Stopping a package from infesting my namespace
Select or Download Code
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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2014-09-21 03:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (166 votes), past polls