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


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

Replies are listed 'Best First'.
Re^3: defining constant in other package
by Corion (Patriarch) 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