in reply to ifdef in modules

The limitation is not ifdef's, but -M's. use is block/file scoped. -M is the same thing as use at the top of the input file, therefore -M won't affect the module.

bla.pl ------ BEGIN { $|=1 } use bla (); $planet = "World\n"; print($planet); bla.pm ------ $greet = "Hello\n"; print($greet); >perl -Mstrict bla.pl Hello Global symbol "$planet" requires explicit package name at bla.pl line +3. Global symbol "$planet" requires explicit package name at bla.pl line +4. Execution of bla.pl aborted due to compilation errors.

As you can see, strict did not affect the module.

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

    The way ifdef seems to be implemented1, it does its thing before the compilation stage, so it's not quite the same as 'use strict'2.

    1 I say _seems_ to be implemented because I don't fully understand how it works. If I did, I'd know why it's not working :)

    2I'm not really sure how 'use strict' works either, so take that for what it's worth ;)

      According to the ifdef docs:

      This version is completely written in Perl. It uses a source filter to provide its magic to the script being run and an @INC handler for all of the modules that are loaded otherwise.
      which says to me that we use an @INC handler to load modules. And the lib says it puts stuff at the front of the @INC. So it sounds to me like the two are not compatable with each other.

      A quick test shows that if you "use ifdef;" at the top of your module, whatever you put on the commandline will propogate... you may want to try more significant tests before relying on it, though.

        Ok, it looks like I'll have to read up on how an @INC handler works (suggestions are welcome).

        In the meantime, as per your test, I put 'use ifdef;' at the top of the module, and the vars I pass on the commandline now propagate. Just to make sure I understand the significance of this, let me see if I've got this right:

        1. -Mifdef=DEBUGGING on the commandline puts a handler into @INC
        2. use lib './perl5lib'; in foo.pl puts perl5lib into @INC
        3. use ifdef; in Bar.pm puts another handler into @INC

        The last handler (added in step 3) can now get triggered because it is at the front of @INC. It also knows about perl5lib because it was added in step 2 before the triggered handler. It is also able to process the pod sections tagged with DEBUGGING because it was set in the original handler in step 1.

        Am I even close? All this playing with @INC is pretty new to me, so forgive my ignorance.

        Thanks!