[Pdx-pm] question: moving script from unix to windows

Ovid curtis_ovid_poe at yahoo.com
Mon Jun 7 17:18:12 CDT 2004


Hi Annett,

I'm not sure if the following will help your problem :/

I could quite possibly be missing something rather obvious on this, but the first thing that
stands out to me is that your error message is not complete:

    open (PNG, ">$png_file") || die "unable to open $png_file";

Change that to:

     open (PNG, ">$png_file") || die "unable to open $png_file: $!";

That $! (pronounced "dollar bang") and the end of the die statement will give you *perl's* error
message.  This might give you some more insight as to why it's not opening the script.

I would also localize the filehandle as this is a bit safer.  Actually, here's something closer to
how I would write the function:

01:   sub PrintPNG {
02:    my($graph_num) = @_;
03:    die "Bad graph_num: ($graph_num)" unless $graph_num =~/^\d+$/;
04:
05:    my$png_file = "$graph_num.png";
06:    local *PNG; # just in case it's being used somewhere else
07:    open PNG, ">" $png_file or die "unable to open ($png_file): $!";
08:    binmode PNG;
09:    print PNG $image->png; # where does $image come from?
10:    close PNG or warn "unable to close ($png_file): $!";
11:  }

Relevant notes:

Line 02:  Ensures that your $graph_num is really a number.

Line 05: localizes the typeglob.  This is similar to localizing any other global variable (e.g.,
"local $_"), but filehandles don't have sigils so you have to localize the typeglob that holds the
filehandle.

Line 06:  this uses the 3 argument form of open.  It's generally safer than using the two argument
form because you don't have to worry about special characters in the filename variable being
interpreted as special characters in the shell.  Also, we add the "$!" variable to the die
statement for better diagnostics.

Line 09:  where does $image come from?  It's probably better to pass this into the subroutine
rather than rely on a global variable.

Line 10:  used the $! variable in the message again.

Cheers,
Ovid

=====
Silence is Evil            http://users.easystreet.com/ovid/philosophy/indexdecency.htm
Ovid                       http://www.perlmonks.org/index.pl?node_id=17000
Web Programming with Perl  http://users.easystreet.com/ovid/cgi_course/



More information about the Pdx-pm-list mailing list