Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: CGI script bringing its own data

by liz (Monsignor)
on Sep 03, 2003 at 09:47 UTC ( [id://288568]=note: print w/replies, xml ) Need Help??


in reply to CGI script bringing its own data

What I gather from your question is that you want to make the result of:
my $image = GD::Image->newFromPngData($data, 1);
be available automagically when the script starts.

There are several ways to do that:

  • If you can use mod_perl, make sure that $image gets initialized at server startup time.
  • other ways of unfreezing data

I haven't tried it myself, but I fear that you'll not be able to freeze the GD::Image object (because it is most likely a complex object, not a simple hash ref), so you would have to freeze $data. But that's just the contents of the file, so that doesn't make much sense.

You can however speed up the reading of the original PNG file:

open (my $in_fh, $infile) or die; local $/; # enable slurping mode my $data = <$in_fh>; close $in_fh;

Hope this helps.

Liz

Replies are listed 'Best First'.
Re: Re: CGI script bringing its own data
by hacmac (Novice) on Sep 03, 2003 at 10:31 UTC
    Thanks for your advice, Liz. Freezing $data would indeed make sense to me, as having the data at hand without reading it from a file is what I'm trying to do. I initially thought about useing Storable, but that would still leave a file for loading and really wouldn't make any sense. Basically, I'd like to do the following: Instead of
    open (my $in_fh, $infile) or die; local $/; # enable slurping mode my $data = <$in_fh>; close $in_fh;
    I'd like to do
    my $data = handoverthedata(); (...) sub handoverthedata{ # Inlined data starts here # and gets stored in my $foo... # Data # Data # Data # Data # Data # Data # Data # Data # Data # Data # Data # Data # Data return $foo; }
    Any ideas on how to do it? I think I've seen similar constructs in C (though I don't know any C), and I'd be surprised if there wasn't a "canonical" way to do it in Perl. -hacmac
      But why would you want it to do that? It's not going to help you much.

      But anyway, maybe the following will do the trick if you only have one file:

      1. add a line with __DATA__ at the end of your script.
      2. save the script and make sure the editor doesn't keep it open
      3. then in the shell, do a "cat pngfilename >>yourscript" (assuming you have a *nix here. If you have Win32, it might work in a command window by replacing "cat" with "type")
      4. open the script again, you should see a lot of garbage at the end, don't touch that.
      5. Remove the open() and close() calls.
      6. replace my $data = <$in_fh>; with my $data = <DATA>;

      I haven't tried it myself. I hope your editor doesn't mangle the data. If it does, then keep a clean copy without the data at the end and create a final copy each time you made changes.

      Hope this helps.

      Liz

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2025-04-30 00:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.