Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Exception Error in perl code

by RajNaidu (Novice)
on Jun 21, 2009 at 10:26 UTC ( #773370=perlquestion: print w/replies, xml ) Need Help??

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

When I execute theelow code I am getting below exception error. Line 405 in the program points to "return timelocal(0, 0, 0, @dmy) / (24 * 60 * 60);" Am I missing anything in code. ---------------------------------------------------------------------------------------------------------------- Uncaught exception from user code: Day '0' out of range 1..31 at /CLO/Tests/CTI/Scripts/drivers/lib/ line 405 Time::Local::timegm called at /usr/local/lib/perl5/5.8.0/Time/ line 120 Time::Local::timelocal(0,0,0,0,0) called at /CLO/Tests/CTI/Scripts/drivers/lib/ line 405 ---------------------------------------------
I have the following code in my program. use strict; use warnings; use diagnostics; use Time::Local; my @match_list; my $days1 = ""; my $days2 = ""; my $day = ""; if ($failtag eq "CompileErr") { open my $pipe, "-|", "/usr/atria/bin/cleartool", "desc", "$include_pat +h/$srcfile" or die "could not run cleartool: $!"; while (<$pipe>) { next unless /created/; my @rec = split; push @match_list, substr $rec[1], 0, 9; } my $NOW=`date "+%d-%b-%Y`; m +y $t = @match_list; $days1 = epoch_days("$NOW"); $days2 = epoch_days("$t"); $day = $days1 - $days2; if ($day < 15) { $failtag = "TestcaseDiff"; } BEGIN { my %month_num = do { my $n = 0; map(($_, $n++), qw/jan feb mar apr may jun jul aug sep oct nov dec/); }; sub epoch_days { my @dmy = split /-/, shift; $dmy[1] = $month_num{lc $dmy[1]} || 0; return timelocal(0, 0, 0, @dmy) / (24 * 60 * 60); } } }

Replies are listed 'Best First'.
Re: Exception Error in perl code
by FloydATC (Deacon) on Jun 21, 2009 at 12:08 UTC
    The error message "Day '0' out of range 1..31" is a pretty strong hint, check your @dmy and I'm pretty sure you'll find that the first element is 0 (or something to that effect)

    -- Time flies when you don't know what you're doing
      Hi, Thanks for the reply. I am new to perl programming, could you please let me know how to overcome this error. How should i assign @dmy. -Rajini

        First identify where @dmy gets assigned, and how. Then inspect what data it gets assigned from. Maybe you want to check that the data you assign from matches your expectations. It's not hard and it has little to do with Perl programming and more to do with general debugging.

Re: Exception Error in perl code
by Anonymous Monk on Jun 21, 2009 at 10:46 UTC
Re: Exception error in perl code
by ikegami (Pope) on Jun 25, 2009 at 07:23 UTC
    [ Reparented from later thread ]

    timelocal doesn't like your inputs. It's even nice enough to tell you what they are.

    Apparently, @dmy contains two elements, both zero, when used to populate the args of timelocal.

    That's surely due to my $t = @match_list; setting $t to the number of elements in @match_list.

    This is very straightforward debugging. Your variable doesn't contain what it should, so you backtrack until you find where the program's state differs from your expectations.

    As an aside, I wonder why you chose to ignore the warning lc $dmy[1] must have issued.

    Update: The above debugging would have taken less time than posting the question in at least three different places. Over the span of 9 days, no less! And you already got an answer 8 days ago! *boggled*

      Why would  lc $dmy[1] issue a warning?

        oops, I thought lc(undef) would give a warning, but testing shows otherwise.
      Hi ikegami, Thanks for the reply. I am new to perl programming, could you please let me know how to overcome this error. How should i assign @dmy. -Rajini

        You should start by reading my post again, which explains @dmy contains junk because $t contains junk. Fix the latter.

Re: Exception Error in perl code
by Corion (Pope) on Jun 25, 2009 at 08:16 UTC

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://773370]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2021-10-18 22:37 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (76 votes). Check out past polls.