temp file creation in perl 5.0

K

KP

Hi,

I have this perl code that is written in 5.8 but want to run in 5.0.
The issue is that 5.0 don't have File::Temp module so, it fails. Here
is the code.. Please let me know what to comment and what to change so
that the script can be run smoothly. Instead of using standard Perl
Temp module, I want to specify a path to create a temp file and then
cleanup later in the process.

#!/usr/bin/perl -w
use File::Temp qw/ tempdir /;
use Time::HiRes qw/tv_interval gettimeofday/;
use POSIX qw(strftime);

#---------------------------------------
# wget related Variable declarations
#---------------------------------------
# number of seconds to wait for before each wget request times out
use constant WGET_TIMEOUT => 1;

# number of retries to wait for before wget give up
use constant WGET_RETRIES => 1;

# current run timestamp
use constant TIMESTAMP => strftime('%Y-%m-%d %H:%M:%S', localtime());

# subject for the warning mail
use constant SUBJECT => "open file failed ";

# recipient of the warning mail
use constant RECIPIENT => '(e-mail address removed)';

#---------------------------------------
# trims a string
#---------------------------------------
sub trim
{
$_ =~ s/^\s+//;
$_ =~ s/\s+$//;
return $_;
}

#---------------------------------------
# open a file
#---------------------------------------
sub open_or_die
{
# if @_ contains 1 element only then $#_ == 0
# in that case, ($fh, $fn) is initialized to be (undef, @_)
# in the first case,
# open_or_die opens $fn in the specified handled $fh
# in the second case, $fh is not specified, but will be returned
through the return value of the function
my($fh, $fn) = $#_ ? @_ : (undef, @_);
open($fh, $fn) or (`mail -s "${SUBJECT}: ${fn}" ${RECIPIENT} < /dev/
null` and die "cannot open: $fn\n");
return $fh;
}

#------------------------------------------------------------------------------
# make a set of urls given the input file handle $fh and
# the transformer $to_url which transforms each line of the input file
into a url
#------------------------------------------------------------------------------
sub make_urls
{
my($fh, $to_url) = @_;
my(@definitions) = <$fh>; #@definitions now contains the lines from
fh
# each line of definition is trimed (remove starting/trailing
spaces) then processed by the custom to_url function
my(@reduced) = ();
for (@definitions)
{
$_ = trim($_);
if ($_)
{
push @reduced, ($_);
}
}

return map {$to_url->($_)} @reduced;
}

#----------------------------------------------------
# processes the given url $url with $id, and
# record the result using the transformer $to_record
#----------------------------------------------------
sub process_url
{
my($id, $url, $to_record) = @_;

# time a url fetch, returns NULL if no files were downloaded
sub time_url
{
my($url) = @_;
my($cmdline) = "wget -H -p -t" . WGET_RETRIES . " -T" .
WGET_TIMEOUT . " -P\"" . $TEMPDIR . "\" \"$url\" 2>&1";

# @start time accurate to milliseconds
my(@start) = gettimeofday();
my($results) = "" . `$cmdline`;
# @end time accurate to millisecodns
my(@finish) = gettimeofday();

# if $results contains the text Downloaded: 0 Bytes...
# or $results does not contain the text Downloaded
# then download must have failed somehow.
# "NULL" value is returned
# otherwise, assume download to have succeeded,
# the time interval between @start and @finish is returned using
tv_interval
return (($results =~ /Downloaded: 0 bytes in 0 files/) ||
($results !~ /Downloaded/)) ? "0" : sprintf("%0.2f", tv_inter$
}

# prints the result transformed by the to_record transformer to the
$RESULT handle
print $RESULT $to_record->($id, $url, time_url($url)) . "\n";
}
#-----------------------------------------------------------------------------------
# process all urls, given the input file name $fn, and
# transformers $to_url which converts each input line of the input
file to a url and
# $to_record which converts the output data to a valid line of csv
record
#-----------------------------------------------------------------------------------
sub process_urls
{
my($fn, $to_url, $to_record) = @_;

my %urls = make_urls(open_or_die($fn), $to_url);

while (($id, $url) = each(%urls))
{
process_url($id, $url, $to_record);
}
close($fn);
}

{

#------------------------------------------------------------------------------------
# the script can take two or three paramters depending
# the first two params are the filename for the surveys and static
urls respectively
# if only two parameters are passed to the script, then stdout is
used for output,
# otherwise the third parameter specifies the result file

#------------------------------------------------------------------------------------
$#ARGV >= 1 or print("usage: urltimer.pl survey.txt static.txt
[result.txt]") and exit();

my($survey_fn, $static_fn) = @ARGV[0..1];

# context sensitive constants
#$RESULT = ($#ARGV == 1 ? STDOUT : open_or_die($ARGV[2]));
$RESULT = STDOUT;
$TEMPDIR = tempdir(CLEANUP => 1);

# process the server urls
process_urls($survey_fn,
# to_url for survey urls
sub {
return $_ => "http://www.website.com/sitereport/servlet/web.Reg?
websiteid=${_}";
},
# to_record for survey urls
sub {
my($id, $url, $time) = @_;
join(",", $id, '', $time, TIMESTAMP);
});

# process static urls
process_urls($static_fn,
# to_url for static urls
sub {
my($id, $url) = split(/\s+/, $_, 2);
return $id => $url;
},
# to_record for static urls
sub {
my($id, $url, $time) = @_;
join(",",'' , $id, $time, TIMESTAMP);
});
}
 
J

John W. Krahn

KP said:
I have this perl code that is written in 5.8 but want to run in 5.0.
The issue is that 5.0 don't have File::Temp module so, it fails.

perldoc -q "How do I make a temporary file name"



John
 
T

Tad McClellan

KP said:
I have this perl code that is written in 5.8 but want to run in 5.0.


Why do you want to run it on a 1994 release of perl?

Life is too short for such things...

The issue is that 5.0 don't have File::Temp module so, it fails. Here
is the code.. Please let me know what to comment and what to change so
that the script can be run smoothly.
use File::Temp qw/ tempdir /;


Delete that line, and define a subroutine named "tempdir"
that makes a safe temporary directory.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top