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

Hi,
I have a script that reads thru a load of files in a directory, takes the title, and date then save the title, and the date converted to Unix time format in a database. The trouble is some of the dates are throwing my script. How do I carry on running the script and just send the error to standard error?

My troublesome function is thus....

@time_arr = split(/-/,$components[1]); $time_arr[1] = $time_arr[1] -1; $unix_time = timelocal(0,0,0,$time_arr[2],$time_arr[1],$time_ar +r[0]);


It dies when one of the date components is out of range.

I run the script like this and would like execution to continue.

perl ./files.pl > files.txt 2&>1 &

Hope someone can help


./stew :}

Replies are listed 'Best First'.
Re: Continuing with program execution even when function gives error
by Kanji (Parson) on Jun 20, 2002 at 11:22 UTC

    You'll want to take a look at eval...

    eval { @time_arr = split(/-/,$components[1]); $time_arr[1] = $time_arr[1] -1; $unix_time = timelocal(0,0,0,@time_arr[2,1,0]); }; if ( $@ ) { # propagate unknown errors die $@ unless $@ =~ /out of range/; } # ... script continues

        --k.


Re: Continuing with program execution even when function gives error
by particle (Vicar) on Jun 20, 2002 at 11:50 UTC
    i think Kanji has just what you asked for. might i make a few code suggestions?

    subtracting one from an array element can be handled with the -- operator. also, when passing array values in a mixed order, i like to take an array slice, to make my code easier to read and make my lines shorter. my replacements are below, with a little more whitespace to make it a bit more clear.

    @time_arr = split( /-/, $components[1] ); --$time_arr[1]; $unix_time = timelocal( 0, 0, 0, @time_arr[2,1,0] );
    put that in an eval statement and you're on your way.

    ~Particle *accelerates*

Re: Continuing with program execution even when function gives error
by Aristotle (Chancellor) on Jun 20, 2002 at 11:56 UTC
    We need more info. From that snippet, it isn't apparent what format your input data takes so it may simply be you not supplying the values correctly converted. Also, if I understand correctly, you're using the files' timestamps? In that case you can just stat() the file and get, among others, it's 32bit Unix timestamp.

    Makeshifts last the longest.