Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Getting a Regex Not to Match Again

by mdillon (Priest)
on Oct 10, 2002 at 20:43 UTC ( #204318=note: print w/ replies, xml ) Need Help??


in reply to Getting a Regex Not to Match Again

I don't think that this:

my $cmd1 = "/bin/mv /etc/$i.d/$name /etc/$i.d/.NO.$name" or print "/etc/$i.d/$name not found\n"; `$cmd1` if $name =~ /\b(K|S)\d{2}?$expression\b/;
Does what you are expecting... It looks like you think $cmd1 will be some sort of "command" that when used in the backticks will either move the file or print your message if it fails. To achieve this, you're better of using Perl's builtin rename command:
if ($name =~ /^[KS]\d{2}$expression$/) { rename "/etc/$i.d/$name", "/etc/$i.d/.NO.$name" or print "/etc/$i.d/$name not found\n"; }
Notice that I used the modified regex as suggested by sauoq.

In case you're wondering, the first line of your code that I quoted above does this:

  1. Interpolate the variables $i and $name into the string "/bin/mv /etc/$i.d/$name /etc/$i.d/.NO.$name"
  2. Assign the result of the interpolation to the variable $cmd1
  3. If the return value of the assignment is false, print "/etc/$i.d/$name not found\n"

The return value of an assignment expression is the value assigned (isn't that a mouthful). Since the interpolation you're doing will never have a false value (i.e. it will never interpolate into "" or 0), the return value of the assignment will always be true and the print statement will never be executed.


Comment on Re: Getting a Regex Not to Match Again
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2014-12-29 01:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (184 votes), past polls