When you depend on $1 having a value without checking to ensure that a match occurred, and you make up syntaxes without consulting the Perl documentation, you have to expect "strange regexp problems".

$1 will only contain a value if your regular expression match succeeds. In other words, if the match fails, $1 will be undef.

Therefore, when your regexp fails to match, and you still assign the value of $1 to $cwd, you're just assigning undef to $cwd. Then you go and print it. Perl is warning you that you're using an uninitialized value in print.

What this should be telling you, if you read between the lines, is that basename $0 isn't returning a value that matches what your regular expression is set to look for.

Also, as others have mentioned, qw "......" is not the right way to construct a regexp. In fact, this was mentioned to you previously in what function of this Regular Expression?. It only "works" because, as Abigail-II eloquently put it , "...if you use a thingybob as a fnord, Perl will treat the thingybob as a fnord." That doesn't make it right though, and it's subject to breaking, since you're getting into undefined behavior. Use, instead, the actual regexp operator: m//.

You will find perlrequick and perlretut helpful (but only if you read them). Unfortunately the Perl documentation doesn't do a good enough job of advertising what will happen if you depend upon a value being in $1 in cases where a match may fail. It seems you have to look at perlre to get a description of that issue.


Dave


In reply to Re: strange regexp problem by davido
in thread strange regexp problem by iwanthome

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.