Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Regexp question

by hotshot (Prior)
on Mar 30, 2003 at 14:41 UTC ( [id://246732]=perlquestion: print w/replies, xml ) Need Help??

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

hello fellow monks!

I have a regexp of the format:
$tmp =~ /$path/;
$path can hold characters like %, * and other that can "damage" the regexp
I get the following error when running my script:
Use of uninitialized value in regexp compilation at lib/cifsEdit.pl li +ne 162.
and I think it involves what I mentioned above.
I don't remember what i'm supposed to do here, can anyone help?

Hotshot

Replies are listed 'Best First'.
Re: Regexp question
by diotalevi (Canon) on Mar 30, 2003 at 14:44 UTC

    "\Q" and "\E" are your friends

    $tmp =~ /\Q$path\E/

    Added BTW, I wonder if your $path variable is even defined all the time. Perhaps you should check for that first: $result = defined $path and $tmp =~ /\Q$path\E/;

    Added xmath had a great insight that you should be using index() here instead. So that becomes (assuming $tmp is always defined as well) $result = defined $path and -1 != index $tmp, $path;.

      I'd like to add: \Q and \E are a way of telling perl, within quoted text, to protect against meta characters. This is done internally by the quotemeta function. Meta characters are characters with meaning, normally beyond the seven bit ascii range.

      What \Q and \E, or quotemeta do is simply add a backslash before every character which can have a special meaning.

      An example of where this is needed is the at (@) character - it can mean an array, or just '@'. Regexes are very fragile in this sense, because many characters have a meaning.

      -nuffin
      zz zZ Z Z #!perl
Re: Regexp question
by xmath (Hermit) on Mar 30, 2003 at 16:41 UTC
    If you don't want any characters inside $path to be interpreted as regex metachars.. then why don't you use the index function instead of a regex?
Re: Regexp question
by Cody Pendant (Prior) on Mar 30, 2003 at 22:16 UTC
    I think this is a very useful example of something I don't have a name for, other than a "how should I use my hammer on this screw?" question.

    It's a perfectly valid question, but it contains what I think is an erroneous assumption -- that it's special characters inside $path which are causing the problem (and also that "%" is a regex special character, but that's a minor issue). So people are trying to help you with the \Q and \E, but that may just be confusing the issue.

    Special characters in $path can't "damage" your regex at all, Hotshot, they can only enhance it. They might enhance it in ways that give unexpected results, but they shouldn't be causing that error message.

    Your problem is that you get the "Use of uninitialized value in regexp compilation", period.

    Like everyone else, I think you should check what's in $path before you do the regex, but I just wanted to get a bit meta on this question, plus call attention to your misapprehension about the percent sign.
    --

    “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
    M-J D
Re: Regexp question
by Abigail-II (Bishop) on Mar 31, 2003 at 09:23 UTC
    The problem is that $path doesn't contain anything. It's undefined.

    Abigail

Re: Regexp question
by aquarium (Curate) on Mar 31, 2003 at 05:20 UTC
    comment out your regexp line and insert a line above it: print $path; that way your program should run, and print the value of $path which looks like being undef in your case. Chris

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-19 05:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found