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

I've been working with perl for only a short period of time, however I am a quick study. I've been creating a build script which will basically do everything I need save one minor problem as of late, I've been trying to read a log file ie $fh = "$buildarea/$logfile"; and find a phase, either BUILD FAILED or BUILD SUCCESSFUL, if failed make $buildmsg the contents of the logfile or if the late, simply enter BUILD SUCCESSFUL. To no avail. I'm wondering if anyone would mind helpen a bit or pointing me in the right direction please.

Edit: 2001-03-03 neshura

  • Comment on How to read a logfile and create a build script

Replies are listed 'Best First'.
Re: New user to Perl Monks, and Looken for a bit-o-help please.
by repson (Chaplain) on Jan 25, 2001 at 17:21 UTC
Re: New user to Perl Monks, and Looken for a bit-o-help please.
by TStanley (Canon) on Jan 25, 2001 at 19:42 UTC
    #!/usr/bin/perl -w use strict; my $logfile = "/buildarea/logfile.log"; my $buildmsg = ""; open(FH,"$logifle") or die "Can't open $logfile"; $_=<FH>; if(/BUILD SUCCESSFUL/i){ $buildmsg = "BUILD SUCCESSFUL"; close FH; } else{ $buildmsg = $_; while(<FH>){ $buildmsg .= $_; } close FH; }
    The above is untested, and is assuming that if a successful build occurs
    that the message "SUCCESSFUL BUILD" will be the only thing inside the logfile

    UPDATE:Thanks to AgentM for pointing out that by inserting last in the while loop, the script will end much more quickly.
    UPDATE 2:Thanks also to chipmunk for noticing that the loop would have worked incorrectly by not even reading in the 1st line of the file. I made changes to the way the file is read, and it should work now.

    TStanley
    In the end, there can be only one!
Re: New user to Perl Monks, and Looken for a bit-o-help please.
by dsb (Chaplain) on Jan 25, 2001 at 20:21 UTC
    Try putting the entire contents of the file into a scalar variable and checking that variable for a match on SUCCESS or FAILED. Then assign values to $buildmsg accordingly. Example:
    #!/usr/bin/perl open( FH, "filename" ) || die "No way man\n"; while ( <FH> ) { $data .= $_; } if ( $data =~ m/failed/i ) { $buildmsg = $data; } else { $buildmsg = "BUILD SUCCESSFUL"; } # continue
    I didn't test this but the idea should work. - kel -