What I did was a bit different. If you used the other examples and got a not match of the first match then $1 would still hold that value. If your goal is have $1 be 'undef' or to have an 'undef' value to play with, you may want to try this:
use strict; my $i = "mmmm9"; my $a = match_rtn( $i ); print $a, "\n"; $i = "mmm"; $a = match_rtn( $i ); print $a, "\n"; sub match_rtn { my $str = shift; $str =~ m/(\d+)/; return $1; }
So there is a method now doing the checking and returning the value of $1. What is creating the 'undef' value though, is the use of 'strict'. From what I understand, using strict causes variables to be isolated to the block of code they are declared in, and when that block is finished the variable is destroyed. So $1 would be destroyed at the end of the routine after the value is returned. It works though, this way you definitely have an 'undef' value to play with if that is what you are after.

I looked up exactly what 'strict' is supposed to do, and the Camel book says its supposed to disallow "unsafe" code. My question to anyone else is what is considered "unsafe"?

Amel - f.k.a. - kel


In reply to Re: Regular Expression Question by dsb
in thread What happens with empty $1 in regular expressions? (was: Regular Expression Question) by marcblecher

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.