In article <a05101204b8710279a226@[24.70.235.44]>, Harvey Quamen wrote:
> 1) This script doesn't really "translate" from a jpeg to a PNG, does
>    it? The $image_type is simply for the header, right? I got confused
>    because the book says, "So we will provide a high-color,
>    high-detail PNG if possible, or a JPEG otherwise" but it seems like
>    the script is just dumping the data untouched right to STDOUT.

 No, there's no file format conversion done by this script. It just
 check if the user's browser can accept PNG files (or, in my example,
 prefers them over JPEG files.) According to this user preference, the
 script displays the image in the right format (but a version of the
 same image must be provided in the two formats.)
 
> 2) More description: "If a user calls this with
>    http://localhost/cgi/image_fetch.cgi/new_screenshot.png, he or she
>    will actually get new_screenshot.png or new_screenshot.jpeg
>    depending on what the browser supports." I can see that the script
>    is pulling the filename from $ENV{PATH_INFO} but could this data
>    also be retrieved from @ARGV? I tried a version with @ARGV, but it
>    didn't seem to work, and I wasn't sure if it was due to @ARGV or
>    me.

 No, the @ARGV array is useless in this case since there are no
 arguments passed to the script. Two ways of fetching informations from
 outside the script are the CGI parameters (either with POST or GET
 method), and the http server environement variables, which can be
 found in the hash %ENV.
 The modified version below uses CGI.pm to avoid the hassle of
 remembering all the environements variables set by the http server. The
 added benefit of using CGI is that it provides a path_translated
 function that translates the additional path info (in our case, the
 filename) into the full path in the file system.

>    (By the way, it would be smart to use File::Basename for this type
>    of thing, wouldn't it?

 We could, but perl is generaly smart enough to translate '/' to '\' on
 Windows, for example. And the path_translated function should probably
 give back what you are expecting too.

>    And is that why there are extra
>    parentheses around the my $basename declaration? It's not in a list
>    context, is it?)

 Yes it is! the m// operator returns true or false (1/0) if it matches,
 which is handy in for example:
 
   print "matched" if m/Foobar/;

 If you look at the documentation (perldoc perlop), you'll read
 somewhere that the m// operator:
 
        Searches a string for a pattern match, and in scalar context
        returns true if it succeeds, false if it fails.
        [...]
        "m//" in list context returns a list consisting of the
        subexpressions matched by the parentheses in the pattern, i.e.,
        ($1, $2, $3...).

 So you can see that the (\w+), if it is matched, is returned in in the 
 $image_path variable.
 Not that if we wanted to keep track of the file extension the user
 gave, we could have said for example:

   my ($image_path, $image_ext) = path_translated() =~ /(\w+)\.([^.]+)$/;
 
> 3) The Camel and the Pocket Reference both say that binmode needs a
>    filehandle. I'm guessing the Perl in the Guelich et al book is a
>    slightly older version....? So I should expect a few more of these
>    syntax differences as I go through the book? (I actually never got
>    this version of the script to run, even with setting up some
>    directories and files, but managed very well with the one in the
>    Camel, p. 686.)

 I don't have the book right here, but in doubt, check the perldoc with
 up to date information on function use. For example, to get the doc
 about binmode():
   $ perldoc -f binmode
 
> 4) When I test for syntax errors on the command line, I get the following:
> Too late for "-T" option at image_fetch.cgi line 1.
> I'm using Perl 5.6.0 and Apache 1.3.20 on Mac OS 10.1.

 Try adding the -T switch on your command line:
 $ perl -T myscript.pl
 
#!/usr/bin/perl -wT
# Same script, using CGI.pm (untested though)
use strict;
use CGI qw(:standard);

my $image_type = Accept('image/png') >= Accept('image/jpeg') ? 'png' : 'jpeg';
my ($basename) = path_translated() =~ /^(\w+)/;
my $image_path = "$basename.$image_type";

unless ( $basename and -B $image_path and open IMAGE, $image_path ) {
    redirect('http://mysite.com/errors/not_found.html');
    exit;
}

my $buffer;
print header( -type => "image/$image_type", );
binmode STDIN;
while ( read( IMAGE, $buffer, 16_384 ) ) {
    print $buffer;
}

__END__

-- 
briac
 << dynamic .sig on strike, we apologize for the inconvenience >>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to