jargon has asked for the wisdom of the Perl Monks concerning the following question:

So here is a real stumper.
I am maintaining a perl code base, and I appear to have a case of the creeping 1s.
Somewhere, there is a piece of code that breaks the Exporter. Once that code has loaded, I encounter Exporter failings like this:

Can't export symbol: 1varnameA

where $varnameA is a key in the @EXPORT array. An example array is:

@EXPORT = qw($varnameA %varnameB &varnameC);
I'm using 5.6.0 or 5.6.1 depending on the machine, and the complaint comes from Exporter::Heavy line 174:
foreach $sym (@imports) { # shortcut for the common case of no type character (*{"${callpkg}::$sym"} = \&{"${pkg}::$sym"}, next) unless $sym =~ s/^(\W)//; $type = $1; *{"${callpkg}::$sym"} = $type eq '&' ? \&{"${pkg}::$sym"} : $type eq '$' ? \${"${pkg}::$sym"} : $type eq '@' ? \@{"${pkg}::$sym"} : $type eq '%' ? \%{"${pkg}::$sym"} : $type eq '*' ? *{"${pkg}::$sym"} : do { require Carp; Carp::croak("Can't export symbol: $type +$sym") }; }
This became so irritating on one machine that a fellow coder of mine rewrote the code to not use $1 because that magic variable would, in fact, mysteriously have a value of 1. We would even print $sym so we could see what names it was iterating through - and it was correct. The $sym string would be "$varnameA". The order of the @EXPORT array did not matter. Naturally, if you didn't prefix an identifier with a type (a sub) then it would work fine.

I should make it clear that although we patched our Exporter::Heavy on one machine, that did not solve all of our problems..there were still a few creeping 1s, but at least we could get our code to run. I've been searching through our code trying to find what is breaking it, but there is a lot of code there...
Any ideas? Anyone ever seen this behavior before?

Replies are listed 'Best First'.
Re: Exporter failing - the case of the creeping 1s
by Ovid (Cardinal) on May 12, 2002 at 22:08 UTC

    I have no idea how this is happening, but here's some information for you (including some behavior that is a bit surprising to me). First, from perldoc perlvar:

    $<*digits*> Contains the subpattern from the corresponding set of capturing parentheses from the last pattern match, not counting patterns matched in nested blocks that have been exited already. (Mnemonic: like \digits.) These variables are all read-only and dynamically scoped to the current BLOCK.

    Since the dollar digit variables are auto-localized, I expected that the Exporter code should not have a predefined value of 1. However, I did the following test. First, I created a file called "test.pm":

    package test; foreach ( 1..3 ){ print "Here: '$1'.\n"; } 1;

    Then, I created a "test.pl" in the same directory:

    unshift @INC, '.'; my $var = "abc123def"; $var =~ /(\d+)/; require test;

    And the output of running it:

    Here: '123'. Here: '123'. Here: '123'.

    Apparently, the $1 is not auto-localized in a different package. Further, I thought that the foreach loop would have its own scope and be treated as a BLOCK. In fact, even when I explicitly wrapped the loop in braces to create a naked block, $1 was still set to 123.

    My theory: you have a regex matching prior to calling the import method that triggers Exporter. In fact, you might even be using the regex in a scalar context yet be taking multiple matches. For instance, this will set $1 to 1:

    $var = 'foobar'; $var2 = ( $var =~ /(foo)(bar)/); print $var2;

    So... perhaps it's a construct similar to the one above that it setting $1 to 1? Do you have any BEGIN blocks or something similar that's triggering this? Somewhere, I think, you have a stray regex that should have the dollar digit variables localized.

    Update: Okay, I'm full of it. In rereading what you said:

    We would even print $sym so we could see what names it was iterating through - and it was correct. The $sym string would be "$varnameA".

    But, if we look at the code in question:

    foreach $sym (@imports) { # shortcut for the common case of no type character (*{"${callpkg}::$sym"} = \&{"${pkg}::$sym"}, next) unless $sym =~ s/^(\W)//; $type = $1; *{"${callpkg}::$sym"} = $type eq '&' ? \&{"${pkg}::$sym"} : $type eq '$' ? \${"${pkg}::$sym"} : $type eq '@' ? \@{"${pkg}::$sym"} : $type eq '%' ? \%{"${pkg}::$sym"} : $type eq '*' ? *{"${pkg}::$sym"} : do { require Carp; Carp::croak("Can't export symbol: $type +$sym") }; }

    The only way that printing $sym could result in $varnameA is if the substitution failed to recognize the dollar sign as a non-word character:

    $var = '$varnameA'; $var =~ s/^(\W)//; print "Symbol: $1\tVariable: $var\n";

    Resulting in:

    Symbol: $ Variable: varnameA

    If $sym really does have the dollar sign left on it, you need to check to see what is screwing up your regular expressions.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Exporter failing - the case of the creeping 1s
by samtregar (Abbot) on May 12, 2002 at 22:56 UTC
    Guess: something is funky with the contents of $sym, maybe UTF-8? Perhaps a Devel::Peek::Dump($sym) would be informative?

    But that's just a random guess - you didn't give me enough to do better than that. Can you reproduce the problem with a simple script? If you can, post it here and I bet we'll find the problem.

    -sam

Re: Exporter failing - the case of the creeping 1s
by Juerd (Abbot) on May 13, 2002 at 08:51 UTC

    Is there any locale in effect that defines "$" as a \w character?

    If not, try adding this at the beginning of the block:

    "" =~ /()/;

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

(tye)Re: Exporter failing - the case of the creeping 1s
by tye (Sage) on May 13, 2002 at 17:16 UTC

    Two ideas come to my mind:

    1. A bug in Perl
    2. Someone is replacing Exporter::Heavy::heavy_export() out from under you
    but it sounds like you added debugging print statements directly to heavy_export() which would rule out #2. I'd probably install a different version of Perl (keeping your current version untouched) and see if the problem persists with that version.

            - tye (but my friends call me "Tye")