Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Forward slashes around argument

by Marshall (Canon)
on Jul 02, 2019 at 02:15 UTC ( [id://11102286]=note: print w/replies, xml ) Need Help??


in reply to Forward slashes around argument

The forward slashes are signify a "regular expression". Regular Expressions. A regex can match a series of characters within a variable and much, much more than that. Be careful with syntax like "if this variable doesn't match 'not stop'" - that is a double negative and is hard to understand.

Update: italics added to main point. Extraneous \ removed before single quotes.

use strict; use warnings; my @CPC_STP_type =('bus does not stop here','bus stop', 'anything', 'n +ot this stop'); foreach my $type (@CPC_STP_type) { if ($type =~ m/not stop/) #the "m" is optional { print "type=$type......'not stop' detected\n"; } else { print "type=$type......'not stop' was NOT detected\n"; } } print "\n"; =prints type=bus does not stop here......'not stop' detected type=bus stop......'not stop' was NOT detected type=anything......'not stop' was NOT detected type=not this stop......'not stop' was NOT detected =cut foreach my $type (@CPC_STP_type) { if ($type !~ m/not stop/) #the "m" is optional { print "type=$type......We stop here\n"; } else { print "type=$type......We keep going\n"; } } __END__ #watch out for the unexpected! type=bus does not stop here......We keep going type=bus stop......We stop here type=anything......We stop here type=not this stop......We stop here <= Whoa!!

Replies are listed 'Best First'.
Re^2: Forward slashes around argument
by hippo (Bishop) on Jul 02, 2019 at 08:03 UTC
    print "type=$type......\'not stop\' detected\n";

    You might be pleased to hear that those backslashes before the single-quotes are superfluous. If you omit them your code will become shorter to type and simpler to eye-parse:

    print "type=$type......'not stop' detected\n";

    HTH.

      You are quite correct.
      My point was: Be careful with syntax like "if this variable doesn't match 'not stop'" - that is a double negative and is hard to understand..

      Perl quoting can become hard to understand. An extraneous \ in this case makes little difference, but thanks for pointing that out.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2024-04-25 11:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found