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 (Archbishop) 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.