Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re: Strict, my and warnings by Aristotle
in thread Many strings make one variable? by heezy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2024-04-23 08:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found