in reply to Re: Unix 'grep -v' equivalency in Perl (was: Perl Regex Question)
in thread Unix 'grep -v' equivalency in Perl (was: Perl Regex Question)

To be more idiomatic, you should ensure that your @list doesn't contain anything that will get the regex bent out of shape. A simple application of quotemeta will help set things straight:
my $re = join ("|", map {quotemeta} @list); while (...) { next if /$re/; print; }
I'm not a huge fan of the 'next LABEL' command. It's too much like 'goto', which is one of those things that shouldn't be shown in public.
  • Comment on Re^2: Unix 'grep -v' equivalency in Perl (was: Perl Regex Question)
  • Download Code

Replies are listed 'Best First'.
Re (tilly) 3: Unix 'grep -v' equivalency in Perl (was: Perl Regex Question)
by tilly (Archbishop) on Jul 10, 2001 at 01:31 UTC
    I don't think the purported similarity to goto is a good reason to not use Perl's loop control facilities. OK, the construct is abusable (what construct is not?), but when used appropriately I think that it clarifies things immensely.

    FWIW the following article on Loop Exits and Structured Programming helped shaped my thinking on this.

      I agree. Consider the alternative (warning: marginally yucky code ahead):
      while (<INPUT>) { my $skip; for my $match (@list) { $skip = 1, last if /$match/; } next if $skip; print; }
        Of course, you could always use grep in your grep equivalent:
        foreach my $line (<INPUT>) { next if (grep{$line =~ /\Q$_\E/}@list); print $line; }