Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

strange regexp problem

by iwanthome (Beadle)
on Apr 06, 2004 at 08:21 UTC ( [id://342863]=perlquestion: print w/replies, xml ) Need Help??

iwanthome has asked for the wisdom of the Perl Monks concerning the following question:

My code is like this

use File::Basename; my $cwd = dirname $0; $cwd =~ qw "(/.*)/" ; $cwd = $1; print $cwd; require "$cwd/lib/Cisco/MyCopy3.pm";
it will report
Use of uninitialized value in print at bin/cpcfg.pl line 26. Use of uninitialized value in concatenation (.) or string at bin/cpcfg +.pl line 2 7.
But if my code is like this:
my $cwd = "/ddd/862/8u/sss"; $cwd =~ qw "(/.*)/" ; $cwd = $1; print $cwd; require "$cwd/lib/Cisco/MyCopy3.pm";

it will work normal,So what's wrong with it ?

thanks!

Replies are listed 'Best First'.
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.


    Dave

      I'm very sorry for that

      And I will read it more detail

      thanks for your help!

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?
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; }
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).

      Sorry for my stupid error.But it can't work too after I change the "qw" to "m".Perl report same error.

      Can you give more advice ?

      thanks very much!
Re: strange regexp problem
by iwanthome (Beadle) on Apr 06, 2004 at 09:00 UTC

    thanks for everybody.My changed code is like this:

    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

      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

        thanks!I call that program like what you said.I will try to use FindBin to find absolute directory of the program.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://342863]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-03-28 16:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found