Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Bug? Use of uninitialized value $1 in substitution iterator

by clinton (Priest)
on Mar 04, 2009 at 12:50 UTC ( #748116=perlquestion: print w/replies, xml ) Need Help??
clinton has asked for the wisdom of the Perl Monks concerning the following question:

Hi all

Is this a bug, or just a bad regex?

sub match { no warnings; my $value = shift @_; $value =~ s{^(?:aa(bb))?.*}{$1}i; return $value; } sub match_warn { use warnings; my $value = shift @_; $value =~ s{^(?:aa(bb))?.*}{$1}i; return $value; } print match('aabbcc')."\n"; # 'bb' print match('abbcc')."\n"; # '' print match_warn('aabbcc')."\n"; # 'bb' print match_warn('abbcc')."\n"; # Use of uninitialized value $1 +in substitution iterator

The way I would have read that is that the capture would contain the empty string, rather than undef and so wouldn't have thrown an unintialized value warning. Is my assumption correct, which would mean that this is a bug (in 5.8.8 and 5.10), or is there a better way to write this regex.

(Note: this regex is a simplified example of a substitution which is intended to retain a part of a string (if the surrounding text matches), and if not, to set the string to the empty string).

Replies are listed 'Best First'.
Re: Bug? Use of uninitialized value $1 in substitution iterator
by moritz (Cardinal) on Mar 04, 2009 at 12:55 UTC
    No, the regex engine has it right. Since the capturing group didn't match, $1 is undef. If $1 contained the empty string, it would mean that the associated capturing group matched a zero-length string (not possible in your example, but in general it's possible).

    I don't see a reason to change the regex though, if it does what you want. Just switch off the warning.

      OK, fair enough. This same code started throwing up errors today which it wasn't throwing before - I'd refactored some other code around it - so it had me worried.

      My solution was, as you suggest, to turn of those warnings.

      thanks Moritz

Re: Bug? Use of uninitialized value $1 in substitution iterator
by bellaire (Hermit) on Mar 04, 2009 at 13:01 UTC
    $1 is undefined, and any use of undef will trigger the warning. You can use the following regex if you don't want to turn the warnings off:
    The e switch lets you use code for the replacement, so you can test whether $1 is defined before using it, and if it's not defined, use the empty string instead.
Re: Bug? Use of uninitialized value $1 in substitution iterator
by ikegami (Pope) on Mar 04, 2009 at 15:28 UTC
    Perl distinguishes between a capture that wasn't encountered in the match and a capture that matched zero characters.
    use Data::Dumper qw( Dumper ); '' =~ / ^ ( (?: ( bb ) # $2 )? ) # $1 $ /x; local $Data::Dumper::Useqq = 1; local $Data::Dumper::Terse = 1; local $Data::Dumper::Indent = 0; print('$1 = ', Dumper($1), "\n"); print('$2 = ', Dumper($2), "\n");
    $1 = "" $2 = undef
      nicely explained

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://748116]
Approved by sundialsvc4
[perldigious]: Snowshoeing time stevieb? I need to buy a pair so I can walk my German Shepherd properly through the country.
[stevieb]: yeah, I snowshoe a fair amount in the mountains. I also use spikes similar to these very often as well
[perldigious]: Discipulus: alternate link
[stevieb]: LOL you can easily replace "Helsinki" with "Alberta"
[perldigious]: I used to live at the top of a very steep hill stevieb, I can relate to the video. Literally used to have to build up momentum and try to force my vehicle to make it up the hill to park in my driveway properly... didn't always make it.
[choroba]: Why don't they use tire chains?
[perldigious]: First snow, probably not prepared right away choroba.
[stevieb]: it's Quebec... they do weird stuff there :) In the mountains, it is *mandatory* for trucks to use tire chains. Many passenger vehicles do as well (but it's not mandatory for them)
[perldigious]: That stuff can hit fast... like what we just got where I'm at. Saw a few people who had slid OUT of the roundabout I have to drive through to get to work, not used to the ice yet, take awhile to get the hang of it again and make vehicle adjustments.
[stevieb]: man, when the roads are glare ice, I don't even bother going to work or out... unless I absolutely have to, or was already out in the first place

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2016-12-06 16:34 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (112 votes). Check out past polls.