Front page | perl.beginners |
Postings from October 2021
Re: run cmd, writing output to log file
Thread Previous
From:
Andy Bach
Date:
October 5, 2021 20:22
Subject:
Re: run cmd, writing output to log file
Message ID:
CACV6rWLJSYGdojLGmePqVgSDpbXpa6T8qP=58epwGqa6Dw=quw@mail.gmail.com
What Ken said:
while (<$ch>) {
print $fh "$PaddedDateStr $_";
}
On Tue, Oct 5, 2021 at 2:46 PM Ken Slater <kenslaterpa@gmail.com> wrote:
>
>
> On Tue, Oct 5, 2021 at 3:35 PM reader <hput3@fastmail.fm> wrote:
>
>> Whenever I don't do scripting for longish periods, next time I start
>> writing a perl script, an awful lot of useful info has flew right out
>> of my pea brain.
>>
>> I was pretty sure I have written perl scripts that wrote to log files
>> with out problems but the script below does not. Instead if throws
>> this error:
>>
>> Can't use string ("/home/reader/t/var/log/fetchmail"...) as a symbol
>> ref while "strict refs" in use at ./pfetch line 18, <$ch> line 1.
>>
>>
>> pfetch script
>> ------- ------- ---=--- ------- -------
>>
>> #!/usr/local/bin/perl
>>
>> use strict;
>> use warnings;
>>
>> my $cmd = "fetchmail -vvvc";
>>
>> my $PaddedDateStr = pd();
>>
>> open my $ch, '-|', "$cmd" or die
>> "Can't open $cmd: $!";
>>
>> my $log = "/home/reader/t/var/log/fetchmail.log";
>> open my $fh, '>>', "$log" or die
>> "Can't open $log: $!";
>>
>> while (<$ch>) {
>> print $log "$PaddedDateStr $_";
>> }
>>
>> print $log "\n";
>>
>> close $log;
>>
>> sub pd {
>> my ($mon,$mday,$year,$hour,$min,$sec,$wday) =
>> (localtime(time))[4,3,5,2,1,0,6];
>> $year -= 100; ## gives 2 digit (with %02d)
>> $mon += 1;
>>
>> my $PDS = sprintf "%02d%02d%02d:%02d%02d%02d %d",
>> $year,$mon,$mday,$hour,$min,$sec,$wday;
>> return $PDS;
>> }
>> ------- ------- ---=--- ------- -------
>> At first glace, looks like $log was used instead of instead of $fh.
>>
> Ken
>
--
a
Andy Bach,
afbach@gmail.com
608 658-1890 cell
608 261-5738 wk
Thread Previous