cLive ;-) has asked for the wisdom of the Perl Monks concerning the following question:

Either that, or I'm going nuts.

I've been adding a test suite to our app. Everything's going smoothly. Then I try an install and I get an error. I write a minimal test case for it. Error is:

F/Ast/Action.pm did not return a true value at tmp.pl line 4. BEGIN failed--compilation aborted at tmp.pl line 4.

line 4 is the use statement loading the module. So, I take a look at it. It ends in a "1;" on it's own line.

perl -c F/Ast/Action.pm gives no errors or warnings.

So I start ripping out subs to create a minimal case. I end up with this:

package F::Ast::Action; use Switch; use strict; use warnings; =over 4 =item constants =back =cut sub new { return bless [], shift; } sub doCmd { my $self = shift ; my $cmd = shift ; switch ($cmd) { case 'get_permission' {} } } 1;

Here's where it gets weird. If I remove the pod, the test script works fine. If I put it back and remove the doCmd sub, it works just fine. And if I move the module into the same directory as the test script (renaming and amending use statement), it works fine.

I am very, very confused.

Any ideas as to what my sleepy head could be missing? I'm about ready to start headbutting the keyboard...

Replies are listed 'Best First'.
Re: Extremely weird module issue...
by shmem (Chancellor) on Mar 22, 2007 at 08:35 UTC
    Here's where it gets weird. If I remove the pod, the test script works fine. If I put it back and remove the doCmd sub, it works just fine. And if I move the module into the same directory as the test script (renaming and amending use statement), it works fine.
    hmm... source filter (Switch) screwing things?
    BUGS
    There are undoubtedly serious bugs lurking somewhere in code this funky :-) Bug reports and other feedback are most welcome.
    - just a guess, as you say removing the doCmd sub (the one that uses switch) makes the thingy work.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Extremely weird module issue... (just say no)
by tye (Sage) on Mar 22, 2007 at 10:34 UTC

    Switch.pm is infamous for breaking code in such bizarre ways. Just stop using it. This is exactly why most source filters are a bad idea.

    Although the Switch docs are not nearly alarmist enough, it helps to read Damian's own warnings from another module, Perl6::Rules:

    via a source filter. (And hence suffers from all the usual limitations of a source filter, including the ability to translate complex code spectacularly wrongly).

    Not that I find your code above particularly complex, but I'm not surprised that it is complex enough to cause Switch.pm to bungle things in confusing ways.

    - tye        

Re: Extremely weird module issue...
by rhesa (Vicar) on Mar 22, 2007 at 09:06 UTC
    Try starting your pod with a =pod line. This snippet from the Switch source will explain:
    my $pod_or_DATA = qr/ ^=(?:head[1-4]|item) .*? $CUT | ^=pod .*? $CUT | ^=for .*? $EOP | ^=begin \s* (\S+) .*? \n=end \s* \1 .*? $EOP | ^__(DATA|END)__\n.* /smx;
Re: Extremely weird module issue...
by grinder (Bishop) on Mar 22, 2007 at 13:58 UTC

    It's definitely a bug caused by the Switch module. You could file this as a bug on rt.cpan.org. Otherwise the solution is to not use Switch. It doesn't work, and there are other, more robust, ways of dealing with switches. Even if you get it to work now through some weird contorsions in your code, changes down the track will cause it to begin failing again.

    On the other hand, with minor adjustments, your code works fine with bleadperl, which has a language-level switch keyword:

    package F::Ast::Action; use strict; use warnings; use feature 'switch'; =over 4 =item constants =back =cut sub new { return bless [], shift; } sub doCmd { my $self = shift; my $cmd = shift; given ($cmd) { when ('get_permission') {} } } 1;

    You just have to wait until 5.10 comes out :)

    (gah! I had this in preview mode in another tab and forgot to hit submit for 5 hours. I hate when that happens).

    • another intruder with the mooring in the heart of the Perl

Re: Extremely weird module issue...
by Fletch (Bishop) on Mar 22, 2007 at 12:57 UTC
Re: Extremely weird module issue...
by cLive ;-) (Prior) on Mar 22, 2007 at 16:47 UTC
    ^ thanks all. I never use Switch myself - ahhh the joys of fixing legacy code (as I'm sure my ex-coworkes at my last job would agree :)
Re: Extremely weird module issue...
by rir (Vicar) on Mar 22, 2007 at 19:27 UTC
    The convention of putting pod after the __END__ of code might have prevented this problem.

    A lazy approach is to move use Switch under your pod. More energy is takened to localize the use of Switch. These approaches come from a mental process that is suited to refactoring or enhancing unfamiliar code.

    # use Switch; deleted... sub doCmd { my $self = shift ; my $cmd = shift ; use Switch; switch ($cmd) { case 'get_permission' {} } no Switch; }
    Be well,
    rir