Re: strange regexp problem
by davido (Cardinal) on Apr 06, 2004 at 08:45 UTC
|
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.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] |
Re: strange regexp problem
by tinita (Parson) on Apr 06, 2004 at 08:38 UTC
|
1. never use $1 without checking if a regex succeeded. you have
to provide a default value for this case or do something else.
2. you're printing $cwd after the regex and assigning. use print-debugging by just printing $cwd before the regex.
3. using qw() as a regex is confusing IMHO. you could use qr() or just m//
4. what are you trying to accomplish with that snippet? | [reply] [Watch: Dir/Any] |
Re: strange regexp problem
by snowcrash (Friar) on Apr 06, 2004 at 08:47 UTC
|
As BUU mentioned, the use of qw seems a little, strange, but it works anyway, it deparses to:
$cwd =~ m[(/.*)/];
But if you don't use it for the sake of obfuscation, I suggest to replace it by something more readable. :)
My guess why your code doesn't work is either dirname $0 or $0 is not what you expect. Add some print statements for debugging to see what they contain.
Also, you should check if the regex matches, $1 will only be set if the match is successful:
if ( $cwd =~ m#(/.*)/# ) {
$cwd = $1;
print $cwd;
}
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: strange regexp problem
by BUU (Prior) on Apr 06, 2004 at 08:25 UTC
|
What the hell does this line $cwd =~ qw "(/.*)/" ; do and what do you expect it to do?
The =~ operator is for binding Regular Expressions to a variable, ex: $foo=~m/stuff/;
qw Is an operator that splits a group of words on whitespace. It returns a list. Feeding it's output to the =~ operator probably doesn't do what you want (I'm not really sure what it does do for that matter). | [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] |
Re: strange regexp problem
by iwanthome (Beadle) on Apr 06, 2004 at 09:00 UTC
|
print $cwd;
$cwd = dirname $0;
it print ".",so $1 is undef. So it is the reason
My code want to delete the last chapter of the directory(englist is not my native language,I don't know whether I express clearly).But I don't know why dirname return this.Maybe it has a bug | [reply] [Watch: Dir/Any] [d/l] |
|
Your code, albeit odd, is correct. The "problem" is that
$0 doesn't contain what you expect it to contain. $0 contains
the path of program how you called it. If you called
it as ./program (which can happen if . is
in your PATH, and you called it as program), then
$0 will contain ./program, making that the directory
name is just a dot.
You might to special case this, and stick in a ..,
use FindBin, or call your script in a different way.
Abigail
| [reply] [Watch: Dir/Any] |
|
thanks!I call that program like what you said.I will try to use FindBin to find absolute directory of the program.
| [reply] [Watch: Dir/Any] |