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

Hi, i'm a newbie to Perl CGI programing. I'm using Debian system, the cgi files located in /usr/lib/cgi-bin/, and template files located in /var/www/

So, when I use Template in perl cgi, the CGI file path is like this: http://192.168.1.1/cgi-bin/main.cgi The image in it is: http://192.168.1.1/cgi-bin/t.jpg

Of course this would not work.

What should I do to make the image show itself? Thanks!

I'm using Template like this:

/usr/lig/cgi-bin/main.cgi:
#!/usr/bin/perl -w use Template; use CGI; my $file = '/var/www/template.tt'; my $vars={ msg=>"hello" }; my $cgi = CGI->new(); my $template = Template->new(); print $cgi->header(-type=>'text/html',-charset=>'utf-8'); $template->process($file, $vars) || die "Template process failed: ", $template->error(), "\n";
/var/www/template.tt
<img src="sc1.jpg" />
t.jpg located in /var/www/t.jpg

Replies are listed 'Best First'.
Re: Template::Toolkit img path
by kcott (Archbishop) on Oct 14, 2010 at 06:45 UTC

    The most obvious problem that leaps out at me is that you've given the image filename as t.jpg (at the top and bottom of your post) but your markup has <img src="sc1.jpg" /> (i.e. a different filename).

    Another thing to be aware of is relative pathnames. These can be affected by a number of things such as the configuration of your web server, a <base> element in your markup, symbolic links and so on. If http://192.168.1.1/cgi-bin/ is your effective root, you may need to specify the path to your image with a leading slash (i.e. <img src="/image.jpg" />).

    You should add an alt attribute to your <img> element (e.g. <img src="/image.jpg" alt="picture of whatever" />). This will at least give you a placeholder in the rendered page even if the actual image can't be shown.

    Right-clicking on the place where the image should be will often provide an Image Properties (or similar) option which will tell you where it didn't find your image.

    Your web server logs should also give some indication of what's going wrong here.

    Final tip: add use strict; to your code.

    -- Ken

Re: Template::Toolkit img path
by mjscott2702 (Pilgrim) on Oct 14, 2010 at 08:19 UTC
    It's probably not a TT problem, but more related to where the web server serves its files from - try creating a simple static HTML file with an image, and serving that up.

    Pathnames should be relative to the (virtual) server root, not the Unix absolute path. You can check the server log file for files not being found to get a clue to where it is trying to get them from.

Re: Template::Toolkit img path
by Anonymous Monk on Oct 14, 2010 at 06:29 UTC