Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: qr// and user provided regex patterns...

by misterMatt (Novice)
on Jul 30, 2009 at 06:57 UTC ( #784509=note: print w/ replies, xml ) Need Help??


in reply to Re: qr// and user provided regex patterns...
in thread qr// and user provided regex patterns...

Okay, I decided to go the eval route. Because as you pointed out - I'm only using the match operator - which I thought was a regex pattern (I'm really still not clear on the difference.) anyway, I'm also going that route because as another monk pointed out - the use of qr// messes with the user pattern that's already wrapped in //. (the interpolation issue) right now I'm looking at this:

if($options{c}){ #variable grab my $pattern = eval "qr$options{c}" or die $@; print "Please enter the text you wish to run the pattern on: "; my $text = <STDIN>; chomp $text; if($text =~ $pattern){ print $&; #prints entire match print " " . $text; } else{ print "$pattern on $text Failed. "; } }
It works, as long as what the user provides doesn't have any spaces in it. (ex: /Bill Clinton/i causes the program to fail, but /BillClinton/i doesn't.) How do I fix that?


Comment on Re^2: qr// and user provided regex patterns...
Download Code
Re^3: qr// and user provided regex patterns...
by ikegami (Pope) on Jul 30, 2009 at 07:10 UTC

    I'm really still not clear on the difference.

    A multiplier tells the multiplication operator by how much it should multiply. The multiplier is data, the operator is Perl code.

    A regex pattern tells the match operator what it should be match. The pattern is data to Perl, the operator is Perl code.

    You don't expect $c = '4 * 5'; 3 + $c to execute the contents of $c, so why would you expect differently from $c = '/foo/'; $x =~ $c?

    It works, as long as what the user provides doesn't have any spaces in it. (ex: /Bill Clinton/i causes the program to fail, but /BillClinton/i doesn't.) How do I fix that?

    I'm guessing the problem is that the user did NOT provide an argument with spaces in it, but rather passed two arguments. Quote the argument appropriately for your shell.

      Wrapping the argument in quotes solved it - good catch. Thanks.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2014-12-18 21:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (66 votes), past polls