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

Hello monks,

First of all, I must say that I am new to the idea of programming in Perl.
I have written the program shown below which tries to call Win32::TaskScheduler in order to schedule a specific task:

use Win32::TaskScheduler;<br> sub create_schedule($$$$$$$$$$$)<br> <br> { <br> my ($mins, $hrs, $mon,$tue,$wed,$thu,$fri, <br> $sat,$sun,$target_machine,$task_name) = @_; <br> my $days;<br> print my $scheduler = Win32::TaskScheduler->New();<br> my %trig=(<br> 'BeginYear' => 2005,<br> 'BeginMonth' => 03,<br> 'BeginDay' => 20,<br> 'StartHour' => $hrs,<br> 'StartMinute' => $mins,<br> 'TriggerType' => $scheduler -> <br> TASK_TIME_TRIGGER_MONTHLYDOW,<br> 'Type'=>{<br> 'WhichWeek' => <br> $scheduler->TASK_FIRST_WEEK | $scheduler->TASK_SECOND_WEEK <br> | $scheduler->TASK_THIRD_WEEK | $scheduler->T +ASK_LAST_WEEK, 'DaysOfTheWeek' => <br> $scheduler->TASK_TUESDAY,<br> 'Months' => $scheduler->TASK_JANUARY | $scheduler->TASK_FEBRUARY <br> |$scheduler->TASK_MARCH |<br> $scheduler->TASK_APRIL | $scheduler->TASK_MAY | <br> $scheduler->TASK_JUNE | $scheduler->TASK_JULY |$scheduler->TASK_AUGUST | <br> $scheduler->TASK_SEPTEMBER | <br> $scheduler->TASK_OCTOBER | $scheduler->TASK_NOVEMBER | <br> $scheduler->TASK_DECEMBER<br> },<br> );<br> print $scheduler->SetTargetComputer("\\\\MYHOST");<br> my $tsk = $task_name;<br> my $k;<br> foreach $k (keys %trig)<br> { <br> print "$k=" . $trig{$k} . "\n";<br> } print $scheduler->NewWorkItem($tsk,\%trig); <br> print $scheduler->SetApplicationName <br> ("timezone.pm");<br> print $scheduler->Save(); <br> $scheduler = Win32::TaskScheduler->New();<br> $scheduler->Activate($task_name); <br> }

When i execute it i get the following output:

Win32 : : TaskScheduler : fatal error : null pointer, call NEW()
Win32 : : TaskScheduler : fatal error : null pointer, call NEW()

I suspect that this has to do something with the NewWorkItem function of Win32:: TaskScheduler; however the 'trig' array does not seem to have any null pointers. Also the 'timezone.pm' file is in the same folder with the code shown above.I have searched for extra documentation on CPAN and google concerning this function, but it did not help. I would be very grateful to anyone who could tell what the reason of this problem could be.

Thank you in advance,

Harry

Replies are listed 'Best First'.
Re: Win32::TaskScheduler error
by BrowserUk (Patriarch) on Mar 16, 2005 at 02:03 UTC

    Your question would get more response if it was readable. (Please read Writeup Formatting Tips).

    The first thing you should add to you code is some error checking. Using print $obj->someCall() isn't very useful. The Perlish idiom is

    $obj->someCall() or die "Error message: $! ($^E)";

    Note: The $^E. This sometimes gives you a little extra information about what went wrong. The main benefit of this idiom is that the program stops at the point that something fails (preventing loads of spurios errors that occur as a knock-on effect of the first failure--and the message will tell you which line failed and where!

    Below is your code cleaned up with those additions. The error messages could be made better, but that's your job.

    As for the cause of your failure. Once you know what is failing and where, and what if any error message are being produced, you may then be able to work that out for yourself.

    use Win32::TaskScheduler; sub create_schedule($$$$$$$$$$$) { my( $mins, $hrs, $mon, $tue, $wed, $thu, $fri, $sat, $sun, $target_machine, $task_name ) = @_; my $days; my $scheduler = Win32::TaskScheduler->New() or die "Win32::TaskScheduler->New() failed: $! $^E"; my %trig=( 'BeginYear' => 2005, 'BeginMonth' => 03, 'BeginDay' => 20, 'StartHour' => $hrs, 'StartMinute' => $mins, 'TriggerType' => $scheduler -> TASK_TIME_TRIGGER_MONTHLYDOW, 'Type'=>{ 'WhichWeek' => $scheduler->TASK_FIRST_WEEK | $scheduler->TASK_SECOND_WEEK | $scheduler->TASK_THIRD_WEEK | $scheduler->TASK_LAST_WEEK, 'DaysOfTheWeek' => $scheduler->TASK_TUESDAY, 'Months' => $scheduler->TASK_JANUARY | $scheduler->TASK_FEBRUARY | $scheduler->TASK_MARCH | $scheduler->TASK_APRIL | $scheduler->TASK_MAY | $scheduler->TASK_JUNE | $scheduler->TASK_JULY | $scheduler->TASK_AUGUST | $scheduler->TASK_SEPTEMBER | $scheduler->TASK_OCTOBER | $scheduler->TASK_NOVEMBER | $scheduler->TASK_DECEMBER }, ); $scheduler->SetTargetComputer("\\\\MYHOST") or die "Error: $! $^E"; foreach my $k (keys %trig) { print "$k=" . $trig{$k} . "\n"; } $scheduler->NewWorkItem( $task_name,\%trig ); $scheduler->SetApplicationName ("timezone.pm") or die "Error: $! $^E"; $scheduler->Save() or die "Error: $! $^E"; $scheduler = Win32::TaskScheduler->New() or die "Error: $! $^E"; $scheduler->Activate($task_name) or die "Error: $! $^E";; }

    Examine what is said, not who speaks.
    Silence betokens consent.
    Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco.