in reply to ifdef in modules

It works fine for me:

# Bar.pm: use strict; use warnings; package Bar; =begin DEBUGGING die 'Bar dies'; =cut 1; __END__ # foo.pl use strict; use warnings; use Bar; die 'foo.pl dies'; __END__ % perl foo.pl foo.pl dies at foo.pl line 6. % perl -Mifdef=DEBUGGING foo.pl Bar dies at /loader/0x8128de4/Bar.pm line 7. Compilation failed in require at foo.pl line 4. BEGIN failed--compilation aborted at foo.pl line 4.

the lowliest monk

Replies are listed 'Best First'.
Re^2: ifdef in modules
by clee (Novice) on Jun 14, 2005 at 16:50 UTC

    You're right...it does work! It looks like mine didn't work due to this (which I failed to put in my sample code...sorry):

    use lib './perl5lib';

    That's where I put Bar.pm. Here's the full not-working-as-expected code:

    # ./foo.pl: use lib './perl5lib'; use Bar; die "outer death"; __END__ # ./perl5lib/Bar.pm: package Bar; =begin DEBUGGING die "inner death"; =cut 1; __END__

    That said, I'm still not sure why this doesn't work. I'm guessing it has something to do with 'use lib' being loaded after 'ifdef', and 'ifdef' isn't able to work its magic on anything that wasn't in @INC when it was loaded.

    Given the restriction that the module in question must be put in './perl5lib', can anyone offer any suggestions for workarounds?

    Thanks!

      I think that by the time lib modifies @INC, ifdef has already done its thing. One possible workaround would go something like:

      $ PERL5LIB=perl5lib:$PERL5LIB perl -Mifdef=DEBUGGING foo.pl

      the lowliest monk

        Hmm...that still doesn't work for me. I'm on a Win32 system though, so I tried:
        set PERL5LIB=perl5lib;%PERL5LIB% perl -Mifdef=DEBUGGING foo.pl
        I also tried changing foo.pl like so:
        # ./foo.pl: use lib './perl5lib'; use ifdef qw/DEBUGGING/; use Bar; =begin DEBUGGING die "limbo"; =cut die "outer death"; __END__

        Here, the 'use lib' clearly comes before 'use ifdef', but still no luck.

        I should also note that, unexpectedly, this doesn't output 'limbo' either (when run as 'perl foo.pl'). However, that is another issue that is, I believe, unrelated to 'use lib'. In the thread I originally referenced, it indicates that "this pragma is only intended to be called from the commandline" anyway.

      Try this:

      % perl -Mlib=perl5lib -Mifdef=DEBUGGING foo.pl

      the lowliest monk

        Still no joy. Thanks for the suggestion though.