# prototype the sub (i.e. tell strict that it expects one variable)
logger($) {
# take in $message as the passed variable.
my ($message) = @_;
# generate a time stamp
my $now = localtime;
# set the return status. By default we succeed.
my $status = 0;
# remove training \n's in the passed message.
chomp $message;
# open the log file. If we can't set the status to failure.
# We're assuming $logFile is a global variable make sure you 'use vars'
# or strict will complain.
open (LOG, ">> $logFile") or $status = 1;;
# print everything. " "'d variables are dangerous so don't use them.
print LOG $now . " [ " . $message . " ] \n" or $status = 1;
# close the log.
close LOG or $status = 1;
# return our staus to the caller.
return $status;
}
On Friday, February 1, 2002, at 11:11 AM, Young, Bill wrote:
> Chris's solution works but not for the reason you might think. The
> message
> is printed to the logfile from the sub because $detail is defined in the
> main not because it was passed as an argument to the sub. To check
> this,
> try the following:
>
> $date=gmtime();
> print "\nScript started at $date\n";
> #logger("Script started at $date\n") || die "Can't log\n";
> $detail = "Script started at $date\n"; logger("This doesn't get
> printed") ||
> die "Can't log\n";
> sub logger($detail)
> {
> print "Details are: $detail\n";
> open (LOGFILE, ">AV_audit.log") || die "Couldn't open log file : $!\n";
> print LOGFILE "$detail" || die "Print failed : $!\n";
> close(LOGFILE);
> }
>
> The argument "This doesn't get printed" does not get passed to the sub
> in
> $detail. And does not get written to the logfile.
>
> On the original question. You need to read up on subroutines and how to
> pass arguments to them. In particular, arguments are found in the sub
> using
> the @_ variable. In the subroutine, @_ is populated with the arguments
> passed to the subroutine. Don't specify any argument list in the
> definition
> of the sub. Try the following to fix the original code: (Editorial
> note:
> I change the >> to > in the open statement to overwrite instead of
> append
> the logfile during testing.)
>
> $date=gmtime();
> print "\nScript started at $date\n";
> logger("Script started at $date\n") || die "Cant log\n";
>
> sub logger
> {
> $detail=$_[0];
> open (LOGFILE, ">AV_audit.log") || die "Countnt open log file : $!\n";
> printf LOGFILE "$detail" || die "Print failed : $!\n";
> close(LOGFILE);
> }
>
> Bill Young
> PC/Network Manager
> Globe Metallurgical Inc.
> Phone: 740-984-8612
> Fax: 740-984-8595
> Email: [EMAIL PROTECTED]
>
> -----Original Message-----
> From: Chris Anderson [mailto:[EMAIL PROTECTED]]
> Sent: Friday, February 01, 2002 10:46 AM
> To: Conor Lillis; [EMAIL PROTECTED]
> Subject: Re: Newbie logging question
>
> Here is what I did, and it worked:
>
> $date=gmtime();
> print "\nScript started at $date\n";
> #logger("Script started at $date\n") || die "Can't log\n";
> $detail = "Script started at $date\n";
> logger($detail) || die "Can't log\n";
>
> sub logger($detail)
> {
> print "Details are: $detail\n";
> open (LOGFILE, ">>AV_audit.log") || die "Couldn't open log file :
> $!\n";
> print LOGFILE "$detail" || die "Print failed : $!\n";
> close(LOGFILE);
> }
>
>
> ----- Original Message -----
> From: "Conor Lillis" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Friday, February 01, 2002 9:16 AM
> Subject: Newbie logging question
>
>
>> I am trying to write a subroutuine I can use for logging, more to get
>> familiar with PERL concepts than anything else, but don't understand
>> why
> it
>> is failing. I am sure it is blatantly obvious, but I might as well be
>> staring into a hole in the ground.
>> It fails on the printf statement in the subroutine.
>>
>> TIA
>>
>> Conor
>>
>>
>> $date=gmtime();
>> print "\nScript started at $date\n";
>> logger("Script started at $date\n") || die "Cant log\n";
>>
>> sub logger($detail)
>> {
>> open (LOGFILE, ">>AV_audit.log") || die "Countnt open log file : $!\n";
>> printf LOGFILE "$detail" || die "Print failed : $!\n";
>> close(LOGFILE);
>> }
>>
>>
>> The information in this email is confidential and may be legally
> privileged. It is intended solely for the addressee. Access to this
> email by
> anyone else is unauthorised.if you are not the intended recipient, any
> disclosure, copying, distribution, or any action taken or omitted to be
> taken in reliance on it is prohibited and may be unlawful. Please note
> that
> any views, opinions or advice contained in this email are those of the
> sending individual and not necessarily those of the firm. It is
> possible for
> data transmitted by e-mail to be deliberately or accidentally corrupted
> or
> intercepted. For this reason, where the communication is by e-mail, J&E
> Davy
> does not accept any responsibility for any breach of confidence which
> may
> arise from the use of this medium. If you have received this e-mail in
> error
> please notify us immediately at mailto:[EMAIL PROTECTED] and delete this
> e-mail from your system.
>>
>> _______________________________________________
>> Perl-Win32-Admin mailing list
>> [EMAIL PROTECTED]
>> To unsubscribe:
> http://listserv.ActiveState.com/mailman/listinfo/perl-win32-admin
>>
>
>
> _______________________________________________
> Perl-Win32-Admin mailing list
> [EMAIL PROTECTED]
> To unsubscribe:
> http://listserv.ActiveState.com/mailman/listinfo/perl-win32-admin
> _______________________________________________
> Perl-Win32-Admin mailing list
> [EMAIL PROTECTED]
> To unsubscribe:
> http://listserv.ActiveState.com/mailman/listinfo/perl-win32-admin
_______________________________________________
Perl-Win32-Admin mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/listinfo/perl-win32-admin