By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,847 Members | 1,326 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,847 IT Pros & Developers. It's quick & easy.

error in check_time function

P: 10
if pass the values to check_time like this its getting different values:
Expand|Select|Wrap|Line Numbers
  1. use Date::Calc qw(:all);
  2.  
  3. $hh="jjjjjjjjjjjjjjjjj";
  4.  
  5. $mm="bnsdgs";
  6.  
  7. $ss="kdhhgfdfdh";
  8.  
  9. if(check_time($hh,$mm,$ss))
  10. {
  11.  print "valid time:";
  12.  
  13. }
  14. else
  15.  
  16. {
  17.  print "Inva;id time";
  18.  
  19. }
  20.  
here i am getting the valid time as output..


please tell me the reason..

thank u,

Manogna..........
Feb 8 '08 #1
Share this Question
Share on Google+
3 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
if pass the values to check_time like this its getting different values:
Expand|Select|Wrap|Line Numbers
  1. use Date::Calc qw(:all);
  2.  
  3. $hh="jjjjjjjjjjjjjjjjj";
  4.  
  5. $mm="bnsdgs";
  6.  
  7. $ss="kdhhgfdfdh";
  8.  
  9. if(check_time($hh,$mm,$ss))
  10. {
  11.  print "valid time:";
  12.  
  13. }
  14. else
  15.  
  16. {
  17.  print "Inva;id time";
  18.  
  19. }
  20.  
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)):

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. use Date::Calc qw(:all);
  5.  
  6. my $hh="jjjjjjjjjjjjjjjjj";
  7.  
  8. my $mm="bnsdgs";
  9.  
  10. my $ss="kdhhgfdfdh";
  11.  
  12. if(check_time($hh,$mm,$ss))
  13. {
  14.  print "valid time:";
  15. }
  16. else
  17. {
  18.  print "Inva;id time";
  19.  
  20. }
  21.  
And here is what was output when I ran the script:

Expand|Select|Wrap|Line Numbers
  1. C:\coding\perl>perl Perl-2.pl
  2. Argument "jjjjjjjjjjjjjjjjj" isn't numeric in subroutine entry at Perl-2.pl line
  3.  12.
  4. Argument "bnsdgs" isn't numeric in subroutine entry at Perl-2.pl line 12.
  5. Argument "kdhhgfdfdh" isn't numeric in subroutine entry at Perl-2.pl line 12.
  6. valid time:
  7.  
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
Feb 8 '08 #2

P: 10
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....
Feb 11 '08 #3

nithinpes
Expert 100+
P: 410
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:

Expand|Select|Wrap|Line Numbers
  1. use Date::Calc qw(:all);
  2.  
  3. $hh="jjjjjjjjjjjjjjjjj";
  4.  
  5. $mm="bnsdgs";
  6.  
  7. $ss="kdhhgfdfdh";
  8.  
  9. foreach($hh,$mm,$ss)
  10. {
  11.   if($_ =~ /\D/)  ##check for non-digit characters
  12.   {
  13.    print "Invalid Time.Enter only numerical values" ;
  14.    exit; 
  15. }
  16. }
  17.  
  18.  
  19. if(check_time($hh,$mm,$ss))
  20. {
  21.  print "valid time:";
  22.  
  23. }
  24. else
  25.  
  26. {
  27.  print "Inva;id time";
  28.  
  29. }
  30.  
  31.  
Feb 11 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.