in reply to Re^3: Execute on conditional check
in thread Execute on conditional check

I am sorry.
while () { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime t +ime; $time_val="$hour:$min:$sec"; while ($time_val == "08:28:00") { print "1st loop\n"; } while ($time_val == "08:28:30") { print "2nd loop\n"; } }
What is the problem in this script. I am writing the while loop recursivley, so that even for the next day it will execute checking on the condition.
But now this code doesn't work.
When the first condition is true, then it should print "1st loop" and execute the second while loop

Replies are listed 'Best First'.
Re^5: Execute on conditional check
by Corion (Patriarch) on Jun 20, 2010 at 15:35 UTC

    If you want to write something like cron, see Schedule::Cron.

    Other than that, have you printed out your $time_val to debug it? It does not contain what you think it does. And even if it contains what you think it does, == is not the comparison operator to use for strings. See perlop for the comparison operator for strings.

Re^5: Execute on conditional check
by hexcoder (Curate) on Jun 20, 2010 at 21:04 UTC
    change
    while ($time_val == "08:28:00") {
    to
    if ($time_val eq "08:28:00") {
    Update: (thanks to Perlbotics)

    also change

    $time_val="$hour:$min:$sec";
    to
    $time_val=sprintf("%02d:%02d:%02d", $hour, $min ,$sec);
    Then all values smaller than 10 will be formatted with two digits.