Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Strict, my and warnings

by true (Pilgrim)
on Oct 15, 2002 at 21:53 UTC ( #205532=note: print w/ replies, xml ) Need Help??


in reply to Re: Re: Re: Re: Many strings make one variable?
in thread Many strings make one variable?

2 follow-up question if you're up for it:
1.) i turned on strict and was amazed at
all the crap i was doing wrong!
I also tried to incude the library 'warnings'
which failed (my perl is 5.005), is the
shebang -w switch a suitable substitute
for use warnings lib?

Will #!/usr/bin/perl -w work in place of use warnings;
2.) Do you see anything fundamentally flawed with my
use of 'my' instead of local? The strict and -w switch
stopped outputing bad news to my error logs, so am i
okay now? Or is there something else i don't know about
to keep me on the right path to using perl correctly.
#!/usr/bin/perl -w # this script outputs a gif # set color, width, and height use strict; &printGIF("FF0000","33","10"); ###################################### sub printGIF{ my $color = $_[0]; my $WH = sprintf("%lX",$_[1]); my $HH = sprintf("%lX",$_[2]); $color =~ s/(..)(..)(..)/$1\|$2\|$3/; my($RH,$GH,$BH)=split(/\|/,$color); print STDOUT "Content-type:image/gif\n\n"; my @gif=( "47", "49", "46", "38", "37", "61", "$WH", "00", "$HH", "00", "A1", "01", "00", "$RH", "$GH", "$BH", "FF", "FF", "FF", "00", "00", "00", "00", "00", "00", "21", "F9", "04", "05", "00", "00", "01", "00", "2C", "00", "00", "00", "00", "$WH", "00", "$HH", "00", "40", "02", "$HH", "84", "8F", "A9", "CB", "ED", "0F", "A3", "9C", "B4", "DA", "8B", "B3", "DE", "9C", "17", "00", "3B" ); binmode (STDOUT); # if needed foreach my $bit(@gif){ my $bita = hex($bit); $bita = pack("C",$bita); print STDOUT $bita; } }#################################### end printGIF
When i say correct use of perl i mean a clean running
program which could open and close millions of times
with no overflow problems.
jtrue


Comment on Strict, my and warnings
Select or Download Code
Re: Strict, my and warnings
by demerphq (Chancellor) on Oct 16, 2002 at 11:13 UTC
    Will -w work in place of use warnings;

    Yes. But use warnings is more powerful and localized to the module it is used in. If/when you upgrade to 5.6 or later then try to get into the habit of using warnings and not -w.

    As for the code, it looks ok to me. (In principle.) Although I might write

    my $bita=pack("C,hex($bit));
    instead. And
    my($RH,$GH,$BH)=($color =~ /(..)(..)(..)/);
    Also it would be a good idea to get into the habit of indenting properly. You can check out the tool "PerlTidy" hosted on source forge to do have it done automatically for you. perlstyle will have some hints for you with regard to programming style. For instance $RH is not the best var name. To me it implies a filehandle or something else with "mystic" overtones (due to its captialization). I would probably have just used $red instead...

    Ultimately it looks like you are on the right path... Keep it up!

    HTH

    --- demerphq
    my friends call me, usually because I'm late....

      Funny(Horrid) thing to share regarding the lack of -w and strict...
      I've been running a dozen linked perl scripts for
      several websites for 2 years now.
      Very sloppy, but working code with
      global dynamic variables, lack of 'my',
      and a few 'locals' where they should not be.
      I turned -w on...
      my apache error log generated 14,267 warnings.
      This is the total number of warnings issued for
      1 single request to one cgi.

      The main cgi only had 5041 lines of code,
      93 subroutines,
      and 2 to 3 requires.

      jtrue

      P.S. running strict was an impossibility.
      Apache said "unsafe" at line four and sacked the cgi.
      Guess what i'll be doing for the next couple of days.

      (See i don't mind showing my butt cheeks).
      In two weeks i'll be stronger.

      thanks for your help demerphq
        running strict was an impossibility. Apache said "unsafe" at line four and sacked the cgi.

        Im sure that if you post the circumstances for this that one of the Apache aware monks will help you get scripts working under strict

        In two weeks i'll be stronger.

        It looks like you are well along on the path to enlightenment. It may be a touch painful (14267! yikes!) but by the end of it you'll probably say it was worth it. And hanging out here and listening to the monks and participating will ease the pain a lot im sure. Incidentally dont be afraid to post answers and your ideas, you might get shot down a few times but if you take it well then nobody will mind helping out a bit....

        :-)

        --- demerphq
        my friends call me, usually because I'm late....

Re: Strict, my and warnings
by Aristotle (Chancellor) on Oct 16, 2002 at 11:39 UTC

    Yes, the -w switch is acceptable - there was in fact no warnings pragma before Perl 5.6, so it's a fairly recent thing. I still prefer -w for the time being as I never need to switch off warnings anywhere, anyway. (The warnings pragma allows for a more finegrained control if you do: you don't have to switch warnings off entirely anymore, you can selectively disable only specific ones.)

    The script is fine, "clean" wise, but could be written quite a lot more succintly. The only technical mistake is writing "$WH" etc where $WH (without the quotes) would do. If you're only using a single variable, you almost never want to put it in quotes. (There are a rare few cases, but you'll know those when you see them.)

    One thing I strongly urge you to, though, is to properly indent your code. It is barely acceptable in that short script, but would make a more complex one completely unreadable.

    Another note is that you're outputting a CGI header in your printGIF routine: if you go to the trouble of writing a function, then make it do exactly one thing. In this case, generate a GIF file. Nothing else. The header, in this case, is the main program's job. That way, you get functions you can reuse in other scripts later. For the same reason I would have the function return the GIF file, rather than printing it to STDOUT directly.

    Which brings up another point you might want to know about: the $|++; I dumped in there. See Suffering from Buffering about it.

    Here's how I'd write that:

    #!/usr/bin/perl -w # this script outputs a gif # set color, width, and height use strict; $|++; binmode STDOUT; print "Content-Type: image/gif\n\n"; print gif_file "FF0000", 0x33, 0x10; ###################################### sub gif_file{ my ($hexrgb, $wid, $hgh) = @_; my %c; @c{qw(r g b)} = map hex, unpack "A2"x3, $hexrgb; return pack "C*", ( 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, $wid, 0x00, $hgh, 0x00, 0xA1, 0x01, 0x00, $c{r}, $c{g}, $c{b}, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0xF9, 0x04, 0x05, 0x00, 0x00, 0x01, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, $wid, 0x00, $hgh, 0x00, 0x40, 0x02, $hgh, 0x84, 0x8F, 0xA9, 0xCB, 0xED, 0x0F, 0xA3, 0x9C, 0xB4, 0xDA, 0x8B, 0xB3, 0xDE, 0x9C, 0x17, 0x00, 0x3B, ); }
    I would normally have used $width and $height instead of the shorter forms, but wanted to keep the hexdump aligned and somewhat compact.

    Makeshifts last the longest.

      Didn't see anything in the
      link about $|++
      Do you mean $|=1

        Yes, $|++ and $| = 1 are equivalent. (Because Perl only ever lets $| be 0 or 1. So if you increment it, it can only become 1. perldoc perlvar should have info on this.)

        Makeshifts last the longest.

      I never need to switch off warnings anywhere,

      Interesting. There a bunch of modules that I routinely use that produce warnings under -w. In fact I spent a day trying to figure out what I was doing wrong, only to realize that my colleague had slipped a -w into the script and that nothing was wrong. (Win32::Eventlog is the latter mentioned module)

      Personally I think that the late introduction the warnings modules and its consequent slow takeup and usage was one of the few serious language errors that was made in the earlier versions of perl. (Strong words I know, but im entitled to an opinion :-)

      I suspect that perl6 will have a quite powerful warning and strictures structure from the get-go as a lesson learned from this.

      --- demerphq
      my friends call me, usually because I'm late....

        Slap the module author(s) on the wrist. Lexically disabling warnings will work under -w and of course you can localize $^W as we used to do. So I don't see any excuse not to take the proper action to disable them as needed if you're intentionally doing something that may raise warnings. My take on this is fairly rigid. There are times that call for not complying with warnings or strict, but you should at least have the decency not to force that choice unto whoever is using your code as well.

        File a bug report or complain to the author by email.

        Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://205532]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2014-07-31 06:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (245 votes), past polls