in reply to Re^2: Gzip compression issue?
in thread (SOLVED by Anonymous Monk) Gzip compression issue?

I'm not finished :D

the eq and || opearators don't chain like that

here is how I might write this

#!/usr/bin/perl -- use constant DEBUG => !!( 0 || $ENV{PERL_DEBUG_MYAPPNAME} ); use CGI::Carp qw( fatalsToBrowser ); use CGI; # to avoid those pesky 500 errors BEGIN { CGI::Carp::set_message( sub { print "<h1>something broke, we know what it is, thank you, + try again later</h1>\n"; if (DEBUG) { print '<p>', CGI->escapeHTML(@_), '</p>'; } } ); } ## end BEGIN use strict; use warnings; use File::Copy qw' copy '; use LWP::MediaTypes qw( guess_media_type ); #~ use MIME::Types 'by_suffix'; BEGIN { *guess_media_type = *by_suffi +x; } use CGI (); Main( @ARGV ); exit( 0 ); sub Main { #~ return DebugCGI(); # generic, env.cgi return SpitThisImage(); } sub Main { my $document_root = $ENV{'DOCUMENT_ROOT'}; $document_root =~ s{/$}{}; my $file = $document_root."/comingsoon.gif"; #~ $ perl -MRegex::PreSuf -le " print presuf( @ARGV ) " jpeg jpg jpe b +mp gif png #~ (?:bmp|gif|jp(?:eg|[eg])|png) if( $file =~ /(?:bmp|gif|jp(?:eg|[eg])|png)$/i ){ my $size = -s $file; $size or die "No file ($file) : $!" ; my $type = guess_media_type( $file ); print CGI->header( -type => $type, -Content_length => $size, ); copy( $file, \*STDOUT ); } else { die "No image ($file) "; } } ## end sub Main sub DebugCGI { my $cgi = CGI->new; print $cgi->header(); # Write HTTP header print $cgi->start_html, $cgi->b( rand time, ' ', scalar gmtime ), '<table border="1" width="%100"><tr><td>', $cgi->Dump, '</td>', '<td><div style="white-space: pre-wrap; overflow: scroll;">', $cgi->escapeHTML( DD($cgi) ), '</div></td></tr></table>', CGI->new( \%ENV )->Dump, $cgi->end_html; } ## end sub DebugCGI sub DD { require Data::Dumper; scalar Data::Dumper->new( \@_ )->Indent(1)->Useqq(1)->Dump; }

See perlintro, perlop

See also http://learn.perl.org/books/beginning-perl/ and the Modern Perl Book

Replies are listed 'Best First'.
Re^4: Gzip compression issue?
by Inexistence (Acolyte) on Dec 25, 2011 at 16:25 UTC
    EEk! the || operators don't work like that? I've got my languages switched up then. Maybe the perl i'm using is modified as it seems to work correctly. I'll test if further assuming it doesn't work now. The last thing I want is my script serving up ANY file on my servers :O omg! I'm chewing through your code, myself not being proficient in perl. I thought CGI caused undue overhead? Chewing, chewing :) Thank-you so much for your further help. This gives me an example that I haven't seen via google
    There isn't in existence

      Maybe the perl i'm using is modified as it seems to work correctly.

      No. You can see how its broken by giving a different extension, like this

      #!/usr/bin/perl -- use Test::More ; for my $ext ( qw/ jpg gif bmp PNG / ) { my $isItTrue = int( $ext eq "jpg"||"gif"||"bmp"); is( $isItTrue, 1, "$ext" ); } Test::More::done_testing(); __END__ ok 1 - jpg not ok 2 - gif # Failed test 'gif' # at - line 5. # got: '0' # expected: '1' not ok 3 - bmp # Failed test 'bmp' # at - line 5. # got: '0' # expected: '1' not ok 4 - PNG # Failed test 'PNG' # at - line 5. # got: '0' # expected: '1' 1..4 # Looks like you failed 3 tests of 4.

      I thought CGI caused undue overhead?

      If the overhead bothers you, use CGI::Simple, or better yet, get a better webserver, like plackup

      I highly recommend you start with beginning-perl book, or Tutorials

        Saved me the testing time, thanks :) I'd have used real life examples, hitting the webserver to see if things worked properly, taking much more time to debug. I definately am looking into better web servers, but i'm only planning atm. Until i'm going live, I don't need to worry about overhead, but its best to keep it low from the start. Thank-you again, you've answered everything I needed to know and more :) Happy holidays!

        PS: Yes, i've checked out some of those writings and continue to read through them

        There isn't in existence

      See also B::Deparse

      $ cat junk int( $ext eq "jpg"||"gif"||"bmp") $ perl -MO=Deparse,-p junk int(((($ext eq 'jpg') or 'gif') or 'bmp')); junk syntax OK $ perl -le " print int( 'jpg' eq 'jpg' ) " 1 $ perl -le " print int( 'gif' ) " 0 $ perl -le " print int( 'bmp' ) " 0