Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Newbie question: I have the following script named "c.pl". I am using <img src="http://someserver/c.pl"> in an html document to reference the script. When I load the page, all I get on the page is a broken link. The picture does not come up. Running the script from the commandline outputs the .gif properly. What am I doing wrong?
#!/usr/bin/perl open(PIC, "c.gif"); $size = -s PIC; print "Content-length: $size\n"; print "Content-type: image/gif\n\n"; while(<PIC>) { print; } close(PIC);

Replies are listed 'Best First'.
RE: Using a perl script as a graphic?
by DrManhattan (Chaplain) on Apr 26, 2000 at 00:38 UTC
    What happens when you try to load http://someserver/c.pl in your web browser? Your server may not be configured to execute .pl files. You may need to rename it c.cgi, put it in a ScriptAliased cgi-bin directory, or add "Options ExecCGI" to the directory you're running it from.
Re: Using a perl script as a graphic?
by merlyn (Sage) on Apr 26, 2000 at 00:47 UTC
    You didn't specify your platform, but if you're on a box where "binmode()" makes a difference, use binmode(C) and binmode(STDOUT).
Re: Using a perl script as a graphic?
by btrott (Parson) on Apr 26, 2000 at 00:01 UTC
    You have "use strict" (good job!) but you didn't predeclare $size. I don't know why your script would work from the command line; but I tried it out myself, fixed the $size problem, and it worked for me.
    my $size = -s PIC;
RE: Using a perl script as a graphic?
by SuperCruncher (Pilgrim) on Apr 26, 2000 at 00:45 UTC
    I think you've got the problem sorted, but one bit of advice: use CGI::Carp qw(fatalsToBrowser); You'll get errors sent to the browser.

    This has saved me SO MUCH time. My script ran fine through the shell, and it gave me 500 Internal Server Error when ran over the web. I eventually tracked the problem down to FreeBSD not liking Windows/DOS' text file linebreaks and invalid use of regexp metacharacters in a regexp.

RE: Using a perl script as a graphic?
by The Alien (Sexton) on Apr 26, 2000 at 02:03 UTC
    The script may not be executable by the server (check permissions to see if it's world or group executable as relevant) or the location may not be valid for executables. Some servers only allow /cgi-bin/ or other specific(or specified) directories to contain executables. I see that as the likely problem if it works at the command line. Remember, the web server usually runs as a user of no importance, so unless it shares a group with your script, the script must be world executable.
Re: Using a perl script as a graphic?
by Anonymous Monk on Apr 26, 2000 at 02:48 UTC
    Content-type *must* be the first header printed by the script, followed by any other optional headers (like size).
RE: Using a perl script as a graphic?
by Anonymous Monk on Apr 26, 2000 at 05:23 UTC
    Content type should go first. Content length should go later ( and is optional ). try "binmode(PIC)" before reading from <PIC> and dumping the gif. -
Re: Using a perl script as a graphic?
by turnstep (Parson) on Apr 26, 2000 at 22:06 UTC

    Um, no, "Content-type" does not have to go first. Actually, it usually comes last, if you look at the output from an apache server, for example. Both "content-type" and "content-length" are entity-header fields, and as such are on equal footing. Check out the HTTP RFC and you'll see that the order of the headers is not specified, but is recommended that the entity-header fields go last. Also, the examples in the RFCs all have "content-type" as the *last* header.

    The script may be failing, but it's a protection or a binmode problem, not the headers.