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

Re: __PACKAGE__ variable cannot use as replace of package name

by kcott (Abbot)
on Apr 23, 2013 at 08:16 UTC ( #1030066=note: print w/ replies, xml ) Need Help??


in reply to __PACKAGE__ variable cannot use as replace of package name

G'day chinaxing,

I see ++moritz has described why you can't do this.

Here's a way you might emulate the behaviour you're after.

$ perl -Mstrict -Mwarnings -E ' package Testonly; our $VERSION = "1.000"; { no strict "refs"; say "NO REFS: ", ${__PACKAGE__ . "::VERSION"}; } eval { say ${__PACKAGE__ . "::VERSION"}; }; warn "REFS: $@" if $@; ' NO REFS: 1.000 REFS: Can't use string ("Testonly::VERSION") as a SCALAR ref while "st +rict refs" in use at -e line 9.

Note how I've used an anonymous namespace to isolate ${__PACKAGE__ . "::VERSION"} in a scope that doesn't check for "strict refs". The eval that follows, demonstrates that the "no strict "refs";" is no longer in effect outside the anonymous namespace.

-- Ken


Comment on Re: __PACKAGE__ variable cannot use as replace of package name
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2014-10-26 06:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (151 votes), past polls