Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Showing why a Regexp didn't match

by pc88mxer (Vicar)
on Apr 12, 2008 at 00:52 UTC ( [id://679905]=note: print w/replies, xml ) Need Help??


in reply to Showing why a Regexp didn't match

Generally you should avoid using $', $& and $'. Once they appear in your program they slow down all regular expression calls. You can achieve the same functionality by specifying your own captures, i.e.
$input->{$key} =~ /[\<\>\:\"\|\?\*]/; die "Error, found $&";
may be re-written as:
$input->{$key} =~ /([\<\>\:\"\|\?\*])/; die "Error, found $1";
Am I doing it right? The ^a-zA-Z?:? is at the beginning as paths my be full windows paths. Although I get the feeling I problem mean ^(a-zA-Z:)?
I think you'll want ^([a-zA-Z]:)?. It depends on if :\some\path is a valid path (i.e. a leading colon without a drive letter.)

The final method you've come up with is about the best you can do. For each error message that you can emit you have to develop a test for it. The only problem I see is that another way your validation test can fail is if there are no valid characters after the "drive:" prefix, e.g. "C:". In that case you'll want a different error message.

Honestly, I don't see what's wrong with just saying:

die "Invalid path: $input->{$key}\n";
By including the offending path in the output it should be easy for the user to tell what the problem is.

Replies are listed 'Best First'.
Re^2: Showing why a Regexp didn't match
by hipowls (Curate) on Apr 12, 2008 at 02:05 UTC

    Generally you should avoid using $', $& and $'. Once they appear in your program they slow down all regular expression calls.
    This is true and good advice but if you are using perl 5.10.0 or later there is the option of using /P and the variables ${^PREMATCH}, {$^MATCH}, and ${^POSTMATCH} where the cost is confined to only the regular expressions that use them.

    In this case, though, there doesn't appear to be any advantage to using /P.

Log In?
Username:
Password:

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

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

    No recent polls found