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

Hello, Please help me investigate what's wrong, I have an Perl script, which check email and post result in IRC. Here is the code:
#! /usr/bin/env perl use strict; use warnings; use IO::Socket; my $true = 1; sub exec_module { #enable formatting options my ( %colors , %format ); # Color Codes $colors{blue} = chr(3) . "02"; $colors{green} = chr(3) . "03"; $colors{red} = chr(3) . "04"; $colors{brown} = chr(3) . "05"; $colors{purple}= chr(3) . "06"; $colors{olive} = chr(3) . "07"; $colors{teal} = chr(3) . "10"; $colors{royal} = chr(3) . "12"; $colors{pink} = chr(3) . "13"; $colors{dgrey} = chr(3) . "14"; #formatting $format{bold} = chr(2); $format{underline} = chr(31); $format{italics} = chr(22); # Logging options: # If debug = 1, all messeges are shown # If debug = 0, only messeges sent with 2 flag are shown my $debug = 1; #logit ( $debug , 1 , "MODULE Generic: error messege" ); my $sock = shift; # io::sockets referenct to socket info in me +mory; my $channel = shift; # channel bot is opperating in my $client = shift; # connection string from IRC (ex: nick!~us +ername@hostname (ident)) my $mailcheck = "/usr/bin/env perl /home/tool/imap-connect.pl| +"; # Pointer to data array use like so: # $data->[0] = name of module called # $data->[1] = fields 1 and above are data after module call sepereat +ed by spaces. # ie (!generic field1 field2) would be $data->[0] = generic, $data->[1 +] = field1 $data->[2] = field2 my $data = shift; while (1) { open (HANDLE, $mailcheck); while ( my $line = <HANDLE>) { if ( $line =~ /(no mail)/i ) { sleep(1); }else{ print $sock "PRIVMSG " . $channel . " +:" . $format{bold} . $colors{red} . "[mail] " . $line . "\n"; } close ( HANDLE ); } sleep(20); } }
It works, but in log I see this error: readline() on closed filehandle HANDLE at modules/alert.pl line 48.

Replies are listed 'Best First'.
Re: Syntax error in scritp or so?
by Ratazong (Monsignor) on Dec 12, 2014 at 11:38 UTC

    while ( my $line = <HANDLE>) { if ( $line =~ /(no mail)/i ) { ... } close ( HANDLE ); }
    You seem to close the handle inside the loop where your read from it. Please be sure that you only close a file after you finished reading it.

    HTH, Rata
Re: Syntax error in script or so?
by hippo (Archbishop) on Dec 12, 2014 at 11:22 UTC

    Unless I've missed something obvious, this script doesn't seem to do anything. Essentially it creates and defines one lexical variable and one subroutine. The subroutine isn't called and the variable isn't used.

    So either this isn't your entire script (in which case the line number in the warning is meaningless to us) or this isn't a script at all, but a module which you use somehow - but that would be odd given the hashbang line and the .pl extension.

    Can you provide some more info about what this code is and how it is executed?

      Hello, This script is a part of IRC Bot. It check a mail box every minute (loop), and post in IRC channel any new messages from Mail box (title).
Re: Syntax error in scritp or so?
by FloydATC (Deacon) on Dec 12, 2014 at 15:34 UTC

    As long as you don't check the result of open(), the most likely explanation is that it fails and therefore does not produce a file handle valid for reading.

    Try adding or die "Open failed '$mailcheck': $!" immediately after open(HANDLE, $mailcheck) and you'll most likely see the root cause of the problem.

    Umm.... a quick update: I just noticed the close() statement inside the read loop. Probably not the best place for it.

    -- FloydATC

    Time flies when you don't know what you're doing

Re: Syntax error in scritp or so?
by Anonymous Monk on Dec 12, 2014 at 11:14 UTC
    What happens in your program after  close ( HANDLE );?