Perl programmer for hire: download my resume (PDF).
John Bokma MexIT
freelance Perl programmer

Perl: Slurping files and better relative URLs

Wednesday, April 21, 2004 | 0 comments

Today I extended the XML to HTML pages transforming Perl script for the MexIT (blog) part of this site with a nice reporting option. I wanted to have an overview of which HTML files were changed after a run of the Perl program, so I could easier spot mistakes in the XML input file, or the Perl program itself. Later, I also want to use this to only upload new and changed files to the web server using the file transer protocol (ftp).

Before the HTML file is written it is compared to the current file version on disk. I use the File::Slurp module, downloaded from CPAN, to read the current version of the HTML file and it is compared to the new version in memory. The File::Slurp module can also write a scalar as a file in one go. This saved a lot of code, since reading a file into a scalar is as simple as: read_file($filename), and writing: write_file($filename, $data). In case you prefer not to use File::Slurp, see

perldoc -q "entire file"

(How can I read in an entire file all at once?) for alternative ways. However, I really recommend to use File::Slurp whenever possible.

I also added a small piece of Perl code to optimize relative URLs, using the URI module:

sub relative {

    my ( $myuri, $uri ) = @_;

    my $rel = URI->new( "http://localhost$uri" )
        ->rel( URI->new( "http://localhost$myuri" ) )

    return length( $rel ) < length( $uri ) ? $rel : $uri;

Note that the http://localhost part was added to create a valid URI. In the return statement I check on the length of the relative URL. The new one created is only returned if it's actually shorter, otherwise the original relative URL is returned to the caller.

Also today

Please post a comment | read 0 comments | RSS feed