in reply to Local variables assigned to pattern matches

It's not local(), it's the parentheses around $x. A regex in scalar context returns a boolean value; a regex in list context returns the backreferences. And I recently fixed a bug related to regexes in list context.

japhy -- Perl and Regex Hacker
  • Comment on Re: Local variables assigned to pattern matches

Replies are listed 'Best First'.
Re: Re: Local variables assigned to pattern matches
by nysus (Parson) on Mar 30, 2001 at 10:37 UTC
    Thanks for getting back. Yeah, I just figured out it was the parentheses, not local (which I understand is not as good as passing references through @_.

    What about the second part of the question (which obviously doesn't apply to this particular situation now).

      The backreferences ($1, $2, ...) only last as to the end of the current scope, or to the next successful regex. Thus, it's pretty useful to keep them in another variable. Also, those variables are read-only, so if you need to extract something and modify it, you need another variable:
      while (<>) { chomp; my ($name) = /(\w+\s+\w+)/; $name =~ tr/\n\r\t\f / /s; # squish spaces push @names, $name; }


      japhy -- Perl and Regex Hacker