Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

totally lost

by sweepy838 (Acolyte)
on Apr 29, 2012 at 16:13 UTC ( #967946=perlquestion: print w/replies, xml ) Need Help??
sweepy838 has asked for the wisdom of the Perl Monks concerning the following question:

such a simple thing, from my tutorials there is nothing wrong with this code.
$input = "-lalala:lalala- hello"; #if "badinput" isnt in the string then print the string. if (!$input =~ m/badinput/) { print "$input!"; }
ripping my hair out :/

Replies are listed 'Best First'.
Re: totally lost
by JavaFan (Canon) on Apr 29, 2012 at 16:22 UTC
    Precedence. You're matching badinput against the negation of $input. Use:
    if ($input !~ /badinput/) { ... }


    if (!($input =~ /badinput/)) { ... }
    but that's more typing, and less idiomatic.
Re: totally lost
by moritz (Cardinal) on Apr 29, 2012 at 16:33 UTC

    Another way to write it:

    unless ($input =~ /badinput/) { print $input; }

    Or even

    print $input unless $input =~ /badinput/;

    With perl 5.10 or newer you can also write

    use 5.010; given ($input) { print unless /badinput/; }

    Though usually it is a better idea to check for valid input (whitelisting) than checking for bad input (blacklisting).

      You do not need given, for/foreach is enough and works even in ancient perls.

      for ($input) { print unless /badinput/; }

      Enoch was right!
      Enjoy the last years of Rome.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://967946]
Approved by Corion
[Lady_Aleena]: Your Mother, that's the command line grep, not perl's grep.
[Your Mother]: …how on Earth could Perl’s grep recurse?
[Tanktalus]: perldigious: I'm not looking to get rich. I'm looking for interesting work, with good continuity of employment, and maintenance of our current lifestyle. (An improvement wouldn't be bad, either, but that's not a requirement ;) )
erix enjoys the confusion++
[Your Mother]: :P
[Tanktalus]: Without confusion, I'd be out of a job ;)
[Tanktalus]: Already. :D
[Lady_Aleena]: Your Mother, I wish it did have a way to put in a directory to search.
[Your Mother]: That’s silly. It’s a string op tool.
[Lady_Aleena]: So I guess I'll go argue with File::Find and grep.

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (11)
As of 2017-05-23 19:33 GMT
Find Nodes?
    Voting Booth?