Hmm, you get the results you expect if you place an extra pair of braces around the if construct.
#!/usr/bin/perl -w use strict; $|++; my $valid ='realgoodname'; my $invalid ='bad name'; my ($test1,$test2); { if ($valid =~ /^([a-z]+)$/i){ $test1=$1; } } { $invalid =~ /^([a-z]+)$/i; $test2=$1; die "Invalid test2" unless $test2; } print "Valid word\t= $valid\nInvalid word\t= $invalid\n\n"; print "Valid test\t= $test1\nInvalid test\t= $test2\n"; exit();
Apparently, stuff inside of the braces on an if statement is not localized. Therefore, the behaviour of $1 does not fit any of the conditions you specified from the Camel. I learn something new about Perl every day :)

Cheers,
Ovid

Update: Duh! The regex wasn't in the block. It's amazing how simple these things often turn out to be. Thanks, chromatic.

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


In reply to (Ovid) Re: Localized Backreferences by Ovid
in thread Localized Backreferences, If Statements & Blocks by doran

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.