in reply to Re: ifdef in modules
in thread ifdef in modules

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 ;)

Replies are listed 'Best First'.
Re^3: ifdef in modules
by Tanktalus (Canon) on Jun 14, 2005 at 20:12 UTC

    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!

        Ignorance? I'm just figuring this stuff out now, too :-)

        1 - correct. Specifically, at the front of @INC.

        2 - correct. Specifically, at the front of @INC (in front of the handler that ifdef put in)

        2.1 - when the module is loaded, perl looks at @INC, finds perl5lib, finds the file there, loads it, and compiles it. During that compilation, it sees...

        3 - incorrect. It sees the use ifdef, but doesn't reload ifdef (it's already loaded). It does, however, call ifdef->import(). This action turns around, figures out what ifdef parameter was passed on the original commandline (it's stored in an environment variable), and then calls the source filter on your code immediately. Once the source filter is done, perl continues parsing and compiling the rest of your module.

        So, actually, it's not as convoluted as you may have thought. ;-) It does seem very convoluted, though.