$code or die has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have a couple of questions. I will ask them first and explain why I'm asking afterwards...

__QUESTIONS__

1. Can I create multiple tokens in one file that I can be read with seperate filehandles? i.e. : __DATA1__ , __DATA2__ etc?

If so, how do I terminate each one?

2. Can I load modules using a variable? i.e. "use $module_name;"?

3. How do I output a processed html page with a dynamic image in it? If I create an image with GD for example, how can I display it in some html generated by perl that doesn't require me to save the image each time?

__EXPLANATION__

$short_answer =
"I am curious and think that these could help me";

$long_answer = "
If I can use multiple tokens I can store lots of things in packages instead of seperate files, it would be useful to be able to store all html output or email templates for example in one file rather than having lots.

If I can load a module based upon a variable ... umm, not sure - I will think of use for it.

I am writing a script that will output an html page with statistics and a graph. I suppose that I could output the html, link the <IMG SRC> to a script that will send back an image mime header with the image?";

__END__
  • Comment on This might not be in the spirit of iTIMTOWTDI/i, but...

Replies are listed 'Best First'.
Re: This might not be in the spirit of iTIMTOWTDI/i
by merlyn (Sage) on Oct 07, 2000 at 01:23 UTC
    For number one:
    @datum = split /\n__BREAK__\n/, join "", <DATA>; ... ... __END__ one item goes here and then we break __BREAK__ second item goes here blah blah __BREAK__ and then there's the third item! Yeay! hoho ho ho

    For #2, a bit tougher. use is compile-time, but your variable there is runtime. Just how early do you know the module name? At compile time, or do you need to wait until some of the "real" code has run?

    And for #3, you need to output an <img src = "..."> tag, with a call back to the same, or a different, CGI program. There are examples in the mouse2 book.

    And as for your explanation, look at Template toolkit in the CPAN.

    -- Randal L. Schwartz, Perl hacker

Re: This might not be in the spirit of iTIMTOWTDI/i
by lhoward (Vicar) on Oct 07, 2000 at 01:23 UTC
    I can help you with #3. You can handle this one of two ways:
    • Have your script write the image to a temp file (somewhere within the webserver's heirarchy), and pass that filename to the browser. Be sure you are done saving the file before you return the IMG tag to the bowser. Be sure to delete the file later.
    • Have a separate .cgi that generates the image (it must return an apropriate ContentType header). Call that .cgi from the IMG tag in your main script that is generating the "text" of the page:
      <img src="genimage.cgi">
      See this earlier node Answer: How can I use a CGI script to return an image?.
Re: This might not be in the spirit of iTIMTOWTDI/i
by AgentM (Curate) on Oct 07, 2000 at 01:26 UTC
    1. no, only one __DATA__ is allowed. however, different perlmods are allowed different __DATA__s.
    2. you cannot load them with a $var because they are compiled in in the BEGINning. I had the same problem. use this require $module; import $module; which is what use does except that its precompiled.
    3. I don't think you can pipe pics embedded in HTML, if that's what you mean. But you can cache the pics (if the mod allows that) and send through when the browser asks for it. For this, you need an elaborate cacheing scheme. perhaps fastcgi?

    AgentM Systems or Nasca Enterprises is not responsible for the comments made by AgentM- anywhere.
      The sequence require $module doesn't work if $module contains colons. I think there's another node here somewhere that I wrote to show how to do this with the right BEGIN{} block around it. But super search isn't super enough for me to find it quickly.

      -- Randal L. Schwartz, Perl hacker


        eval "require $module";

        if $module contains colons

        except that (at least in my program) my module name was determined after reading in a file at runtime. It's easy enough to do if you push the path onto the @INC. Anyway, a quick split for colons would take care to push yet another path on the poor @INC :-O so the end justifies the means.
        AgentM Systems or Nasca Enterprises is not responsible for the comments made by AgentM- anywhere.
RE: This might not be in the spirit of iTIMTOWTDI/i, but...
by acid06 (Friar) on Oct 07, 2000 at 02:15 UTC
    For question number two you could use this:
    eval("use $module");
    I tried it here, and it works. But maybe there's a better way to do it.
Re: This might not be in the spirit of iTIMTOWTDI/i
by Trimbach (Curate) on Oct 07, 2000 at 01:28 UTC
    1. Don't think you can. _DATA_ is a special token, and it's terminated automatically at the end of the file. Not really even sure why you'd want to do this though. Keeping (for example) email and html templates in one file may sound good at first, but really it's much easier (and more portable) to keep all your data pieces organized by file/folder combinations, and then have your programs use just the parts they need.

    2. Again, don't think you can. "use" is processed WAY at the beginning by the Perl Interpreter, long before pesky things like variable interpolation occur.

    3. And lastly, no can do. HTML is output to a browser using a particular Content-type HTTP header (i.e., text/html). Graphics must be output to the browser using the appropriate Content-type header (image/gif, image/jpg, whatever.) You can't mix the two, so you can't send both kinds of data to the browser at the same time. Your solution of linking the graph to a Perl program that generates your image (and then outputs it to the browser with the right header) will work just fine.

    Hope this helps.

    Gary Blackburn Trained Killer

Re: This might not be in the spirit of iTIMTOWTDI/i
by cianoz (Friar) on Oct 07, 2000 at 15:34 UTC
    for the last question:
    the first script (html) writes:
    print q{<img src="/cgi-bin/imgserv.pl">}; ...
    the second script (imgserv.pl) writes:
    # suppose you have read() an immage into $image_data # and image type is jpeg... print "Content-Type: image/jpeg\n\n"; binmode STDOUT; #just if you are unde windows #not needed on more serious platforms :-) print $image_data;