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

Hi Monks,

What is the scope of the standard RegEx variables like $_, $' etc?

I'm trying to loop through a RegEx with the following code and replace all occurances of 'exhibit' with different verbs so that exhibit is not repeated (eg exhibit, show, reveal, display etc...). I'm using this code, but it crashes with 'variable not initialised' on the line $loc_end_pos = pos($_) - length($1);. I suspect $_ is being reset by __exhibit_verb_function. What do you think?

if (/(\{exhibit\})/) { $loc_exhibit_txt = $loc_observation_model->__exhibit_verb_function +($k-1+$gl_exhibit_verb_marker_idx, $gl_language_country, "G"); $loc_end_pos = pos($_) - length($1); $loc_string_out = $loc_string_out . substr($_,$loc_start_pos,$loc_ +end_pos-$loc_start_pos); # Add variable $loc_string_out = $loc_string_out . $loc_exhibit_txt ; # Ad +d variable $loc_start_pos=pos($_); }

Thanks for your help.

regards

Steve

Replies are listed 'Best First'.
Re: Scope of RegEx vars like $_
by JavaFan (Canon) on Mar 12, 2010 at 13:59 UTC
    $_ is not a "regexp var". As for $`, $&, $', $1, etc, the variables are global, they will never go out of scope. The values however get either a new value (after a new succesful match), or revert to a previous value (on scope exit), whichever comes first:
    "foo" =~ /([aeiou])/; say $1; { "xyz" =~ /([aeiou])/; say $1; "bar" =~ /([aeiou])/; say $1; } say $1; __END__ o o a o

      You're right, sorry sloppy construction.

Re: Scope of RegEx vars like $_
by almut (Canon) on Mar 12, 2010 at 12:27 UTC
    I suspect $_ is being reset by __exhibit_verb_function.

    ...very likely, as there isn't much else happening in between $_ being valid (in /(\{exhibit\})/) and it having become undefined in pos($_). But unless we get a chance to look at __exhibit_verb_function(), it's hard to tell what exactly is causing it...

    Note that many variables like $_ are global unless you localize them, i.e.

    !/usr/bin/perl -l use strict; use warnings; sub foo { # ... $_ = undef; } while (<DATA>) { chomp; if (/^FOO/) { foo(); print "found $_"; } if (/^BAR/) { # ... print "found $_"; } # ... } __DATA__ FOO BAR

    prints

    Use of uninitialized value in concatenation (.) or string at ./828268. +pl line 14, <DATA> line 1. found Use of uninitialized value in pattern match (m//) at ./828268.pl line +16, <DATA> line 1. found BAR

    While if you write

    sub foo { # ... local $_ = "whatever"; $_ = undef; }

    you'll get

    found FOO found BAR

      local $_

      Perfect.

      Thanks

      Steve

      Well actually, not quite, I'm still getting the same error.

      I've given up and used a local named variable, $loc_diagnoses_text, since you ask. And this works perfectly. Not sure why the other one didn't work, but I'm happy with this solution.

      Steve

Re: Scope of RegEx vars like $_
by Anonymous Monk on Mar 12, 2010 at 11:59 UTC
    I suspect $_ is being reset by __exhibit_verb_function. What do you think?

    I think you should check and see