Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: defining constant in other package

by LanX (Canon)
on Apr 24, 2012 at 14:41 UTC ( #966860=note: print w/ replies, xml ) Need Help??


in reply to Re: defining constant in other package
in thread defining constant in other package

Well but even when putting a sub-declaration into a BEGIN block codefoldinmg isn't effected!

> perl -MO=Deparse tst.pl sub BEGIN { sub MyPackg::DBG { 0; } } our $a = 1; package MyPackg; print 'DEBUGGING' if DBG(); # <-- oops print 'huhu'; tst.pl syntax OK

OK forget it this works, thanks!

BEGIN { sub MyPackg::Debug {!!0}; } our $a=1; package MyPackg; use constant DBG => MyPackg::Debug; print "DEBUGGING" if DBG; # won't be compiled print "huhu";

> perl -MO=Deparse tst.pl sub BEGIN { sub MyPackg::Debug { 0; } } our $a = 1; package MyPackg; sub BEGIN { require constant; do { 'constant'->import('DBG', Debug()) }; } '???'; print 'huhu'; tst.pl syntax OK

Cheers Rolf


Comment on Re^2: defining constant in other package
Select or Download Code
Re^3: defining constant in other package
by Corion (Pope) on Apr 24, 2012 at 14:45 UTC

    Perl wants a constant (subroutine) to have an empty prototype before it eliminates code guarded by it:

    BEGIN { sub MyPackg::DBG() { 0; } } our $a = 1; package MyPackg; print 'DEBUGGING' if DBG(); # <-- oops print 'huhu';

    gives this output:

    F:\>perl -MO=Deparse tmp.pl sub MyPackg::DBG () { 0 } sub BEGIN { } our $a = 1; package MyPackg; '???'; print 'huhu'; tmp.pl syntax OK
      Argh , thanks, already forgot about this.

      Cheers Rolf

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2015-07-05 16:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls