error in check_time function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Manogna
    New Member
    • Jan 2008
    • 10

    error in check_time function

    if pass the values to check_time like this its getting different values:
    [code=perl]
    use Date::Calc qw(:all);

    $hh="jjjjjjjjjj jjjjjjj";

    $mm="bnsdgs";

    $ss="kdhhgfdfdh ";

    if(check_time($ hh,$mm,$ss))
    {
    print "valid time:";

    }
    else

    {
    print "Inva;id time";

    }
    [/code]

    here i am getting the valid time as output..


    please tell me the reason..

    thank u,

    Manogna........ ..
    Last edited by numberwhun; Feb 8 '08, 10:45 AM. Reason: add code tags
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by Manogna
    if pass the values to check_time like this its getting different values:
    [code=perl]
    use Date::Calc qw(:all);

    $hh="jjjjjjjjjj jjjjjjj";

    $mm="bnsdgs";

    $ss="kdhhgfdfdh ";

    if(check_time($ hh,$mm,$ss))
    {
    print "valid time:";

    }
    else

    {
    print "Inva;id time";

    }
    [/code]

    here i am getting the valid time as output..


    please tell me the reason..

    thank u,

    Manogna........ ..
    I am not really sure why. What version of Perl are you using and what OS are you working on?

    Here is the code I used (only slight modification for using the two pragmas you should have already been using, plus their issues resolution(s)):

    [code=perl]
    use strict;
    use warnings;

    use Date::Calc qw(:all);

    my $hh="jjjjjjjjjj jjjjjjj";

    my $mm="bnsdgs";

    my $ss="kdhhgfdfdh ";

    if(check_time($ hh,$mm,$ss))
    {
    print "valid time:";
    }
    else
    {
    print "Inva;id time";

    }
    [/code]

    And here is what was output when I ran the script:

    Code:
    C:\coding\perl>perl Perl-2.pl
    Argument "jjjjjjjjjjjjjjjjj" isn't numeric in subroutine entry at Perl-2.pl line
     12.
    Argument "bnsdgs" isn't numeric in subroutine entry at Perl-2.pl line 12.
    Argument "kdhhgfdfdh" isn't numeric in subroutine entry at Perl-2.pl line 12.
    valid time:
    To me, this is more like what should have been output. You should have not gotten as far as you say you did.

    Regards,

    Jeff

    Comment

    • Manogna
      New Member
      • Jan 2008
      • 10

      #3
      In this if I pass a valid parameters the outcome should be valid time.
      Otherwise the outcome should be an Invalid time..

      But I am sending Invalid parameters but I am getting the out come as valid time .why?

      I am doing this to do some functionalities if hh,mm,ss exactly valid.otherwise the functionalities should not work..

      Please help me..as I am new to Perl.

      thank U,
      Manogna....

      Comment

      • nithinpes
        Recognized Expert Contributor
        • Dec 2007
        • 410

        #4
        check_time() function works only for numerical values. It returns "true" ("1") if the given three numerical values "$hour", "$min" and "$sec" constitute a valid time.
        If you want to catch exception due to non-numeric values, put a separate test statement. Like:

        [CODE=perl]
        use Date::Calc qw(:all);

        $hh="jjjjjjjjjj jjjjjjj";

        $mm="bnsdgs";

        $ss="kdhhgfdfdh ";

        foreach($hh,$mm ,$ss)
        {
        if($_ =~ /\D/) ##check for non-digit characters
        {
        print "Invalid Time.Enter only numerical values" ;
        exit;
        }
        }


        if(check_time($ hh,$mm,$ss))
        {
        print "valid time:";

        }
        else

        {
        print "Inva;id time";

        }

        [/CODE]

        Comment

        Working...