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

Hi, I am having some trouble using web templates that web designers at my company are creating. The management here has told me that my Perl programs must use the header and footer that the web designers have created. Since the perl programs are in a different directory than the templates, the web designers have put in references to images using the full URL (ex: http://blah.com/img/navleft1.jpg). I am told that this is ineffecient as it causes more web connections to the server as opposed to using a relative link. Is this true? If so, is there a way that I can read the header and footer files and make my program output relative image links as if the script is in the HTML directory? Thanks, I would appreciate any help, or even links to where I can learn more about this.

Replies are listed 'Best First'.
Re: Using HTML templates
by grinder (Bishop) on May 21, 2002 at 15:21 UTC
    Have a look at the URI module. It will let you rewrite an URL in terms of its relation to another URL. You create a URI object of the URL you want to rewrite, and then you simply ask the object to print out how to get to it, relative to where you are now.

    Playing with code makes it easy to understand. Have a look what this prints out.

    #! /usr/bin/perl -w use strict; use URI; my $image = 'http://www.example.com/img/navleft1.jpg'; my $i = URI->new($image); print 'relative ', $i->rel('http://www.example.com/story/page4.html'), + "\n"; print 'relative ', $i->rel('http://www.example.com/story/down/here/pag +e6.html'), "\n";

    That said, I believe (although I could be wrong) that the most efficient way to reference an URL it to give it the absolute path relative to the current host. Which means in your case, you want to emit '/img/navleft1.jpg'. In which case all you have to do is use the path method. The result of this is that you minimise the number of directories you have to stat.

    update: I didn't remember to say this earlier, but another approach would be to build an image server: a web server that is tuned specifically to pumping out static objects (thus, images) on a separate host.


    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
Re: Using HTML templates
by thraxil (Prior) on May 21, 2002 at 15:32 UTC

    linking to images using the full url won't have any effect on how many connections are open unless the hostname in the url is different from the hostname of the page (eg, if the page is http://www.example.com/index.html and it links to http://example.com/index.html that might make for an extra connection). even if that's the case, that's pretty minor. the main problem with using the full url is that it makes it hard to change things. say you've got a development version on a staging machine and a production version on another machine. if the designers want to change the look of the site on the dev version, they can't just swap out new images because it would affect the production version too.

    what can you do about it?

    without knowing more about your specific setup, it's hard to tell. some cleverness with relative paths (eg, 'img/navleft.jpg') and symlinks might do it. or you could slurp in the files and use a regexp to fix things however you want them. if the scripts are at least on the same server as the images and templates (eg, in /cgi-bin/) you can probably get away with just specifying the images using syntax like '/img/navleft.jpg' giving the full path from the document root. that would link to http://www.example.com/img/navleft.jpg no matter where on the server the page is.

    anders pearson

      Thanks all for helping me. We had server slowdowns and it was blamed on the templates opening too many http connections. I did change the images to pull using the relative server address, but there was no speed gain, and it ended up being caused by something else. Using the relative image links does make design updates a lot easier though, so thank you guys!
Re: Using HTML templates
by cfreak (Chaplain) on May 21, 2002 at 15:37 UTC

    I manage a couple of designers and I do the programming where I work. I normally have the design guys use absolute paths starting with '/'. In your example I would have used /img/navleft1.jpg.

    Then I tell my designers for our projects that all the images should be in one place. Such as a top level directory 'img'. This setup seems to work pretty well

    Hope that helps
    Chris

    Some clever or funny quote here.