phizymonk has asked for the wisdom of the Perl Monks concerning the following question:

So I am just getting into CGI using perl, and the last time I posted I had a lot of help to understand hashes and things, so I figured I would ask here.

print $q->header("text/html"), $q->start_html("Images in $imagedir"), $q->print("These are all the images in the directory");

with this code snippet $q is set to "new CGI" is this just laying the ground work for the html part of the script that is running? the goal is to make a script that prints all the images in a directory.

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

 grep { /\.(?:gif|jpg)$/i }

Replies are listed 'Best First'.
Re: CGI Help
by toolic (Bishop) on Feb 10, 2016 at 18:51 UTC
    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 ----------------------------------------------------------------------

      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.

Re: CGI Help
by kennethk (Abbot) on Feb 10, 2016 at 20:20 UTC
    with this code snippet $q is set to "new CGI" is this just laying the ground work for the html part of the script that is running?
    my $q = new CGI; is syntactically equivalent to my $q = CGI->new; It creates a CGI object and stores it in $q, and all methods are called on that object. See perlobj for more details.

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

      The OP should note that the new CGI (indirect syntax) variant is discouraged because it can be confusing to read by both humans and the interpreter.

      Premature optimization is the root of all job security