in reply to CGI Help

also I am sort of confused by this regex and I think I am confused because we have not used grep before

It probably selects only filenames which end in jpg or gif. Tip #9 from the Basic debugging checklist (YAPE::Regex::Explain)

The regular expression: (?i-msx:\.(?:gif|jpg)$) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?i-msx: group, but do not capture (case-insensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- \. '.' ---------------------------------------------------------------------- (?: group, but do not capture: ---------------------------------------------------------------------- gif 'gif' ---------------------------------------------------------------------- | OR ---------------------------------------------------------------------- jpg 'jpg' ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

Replies are listed 'Best First'.
Re^2: CGI Help
by phizymonk (Novice) on Feb 10, 2016 at 19:20 UTC

    is there a certain situation you shoud use grep in and not to use grep in?

      grep in Perl is essentially a simple method for filtering a list. I tend to recommend avoiding it while you are still familiarizing yourself with Perl's constructs.

      grep allows you to accomplish something like:

      my @second; for my $element (@first) { push @second, $element if condition($element); }
      in a concise syntax. Simple filtering by a regular expression is a classic use case. If you have side effects or complex tests, then grep may be a bad choice.

      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.