in reply to strange regexp problem
$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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: strange regexp problem
by iwanthome (Beadle) on Apr 06, 2004 at 09:14 UTC |