Why doesn't my Perl program output anything to my file?

I am trying to write a ~ / .log file, but the file remains empty. I don’t know why this is happening, everything seems beautiful.

My system is Ubuntu 9.10amd64, Perl 5.10.

#!/usr/bin/perl
# vim:set filetype=perl:

use strict;
use warnings;

use LWP::Simple qw||;
use Net::SMTP;

# Only because log file
$| = 1;

my $match_string = "... some text to match ..." ;
my $content;

my @mails = ('mejl1@provider1.com',
        'mejl2@provider2.com',
        );
my $data = <<END;

... Subject text ...

END

open(my $log, ">>","$ENV{HOME}/.log")
or die "Couldn't open log file";

sub get( $ ) {
    my $content_ref = shift;

    print {$log} "get: Error: $!\n"
        until ( ${$content_ref}
                = LWP::Simple::get("www.somesite.com/index.html") );
}


my $check_num = 0;
get( \$content );
while ( index($content, $match_string) != -1) {
    print {$log} "Check number $check_num\n"
        or die "Couldn't write in log file";
# This is printed
#    print "Check number $check_num: $ENV{HOME}/.log\n";
    $check_num++;
    get( \$content );
    sleep 60*30;
}


my $server = "smtp.my_provider.org";
my $smtp = Net::SMTP->new($server)
    or print {$log} "smtp: Couldn't connect on $server\n";

$smtp->mail('my_mail@my_provider.org')
    or print {$log} "smtp: Error in mail\n";
$smtp->bcc(@mails)
    or print {$log} "smtp: Error in bcc\n";

$smtp->data();

$smtp->datasend($data)
    or print {$log} "smtp: Error when sending data\n";
$smtp->dataend;

$smtp->quit();
+3
source share
4 answers

Maybe you are not patient enough? I see the script is waiting 3 minutes before exiting.

Note that $|only works for the currently selected file descriptor, which means STDOUThere.

You can set it for any old file descriptor in a complicated way:

{
    my $old = select $log;
    $| = 1;
    select $old;
}

, - , autoflush, , :

$log->autoflush(1);   # untested

, .

. IO:: Handle FileHandle autoflush - , , .

+6

script. :

print {$log} "get: Error: $!\n"
    until ( ${$content_ref}
            = LWP::Simple::get("www.somesite.com/index.html") );

, , URL- , get, , undef. , until. , ? , , .

script :

 while( 1 ) {
      my $data = LWP::Simple::get("http://www.somesite.com/index.html");
      print "got [$data]\n";

      if( substr( ... ) > -1 ) { sleep 1800; next }

      .... do error stuff here ...
      }

, 30 , script 30 cron. :

 my $data = LWP::Simple::get("http://www.somesite.com/index.html");
 print "got [$data]\n";

 exit if( substr( ... ) > -1 );

 .... do error stuff here ...
+7

, - , $match_string $content. , , , - , ?

+2

.

open(my $log, ">>","~/.log")
or die "Couldn't open log file";
print $log "smtp: Error in mail\n";
close  $log 

it works?

+1
source

Source: https://habr.com/ru/post/1732670/


All Articles