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.


In reply to Re: Exporter failing - the case of the creeping 1s by Ovid
in thread Exporter failing - the case of the creeping 1s by jargon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.