CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Looking for assistance with a simple script



Dec 6, 2012, 12:57 PM

Post #1 of 4 (1927 views)
Looking for assistance with a simple script Can't Post

Hello, I'm looking for assistance / help on the following. I have a script reading from a file handler, which outputs logs for a program. It then parses these to get the exact folder location. I then have another file handler opened which stores it's contents into an array. It then greps the folder variable from the first while loop, and compares to the array of items from from the 2nd file handler. This is done so there is no duplicate of the folders. It then has an if / elsif statement that compares this for me, and finally writes to the file handler. I've written a few scripts with this / fifo pipes and worked fine, but never had to do it like this with multiple files.

1. The script works, it will not write the folder if already there, and DOES write it if not found in the array. My problem is that the 2nd bit of code seemingly does not keep running. I'm not sure if this is because it's all one block of code, or you can't have two file handlers being continously read from in the same block of code. I know the first part keeps working, because it writes the complete logs to another file, and that file keeps changing with the uploads. Any guidance would be much appreciated. I know some of the code is choppy, using a a few bash strings "not recommended" but that is fine for now. Code below, thank you!!!

#!/usr/bin/perl -w

use strict;
use File::Basename qw(basename);
use Sys::Syslog qw(:DEFAULT setlogsock);

my $fifo_file = "/var/log/proftpd/xferlog.fifo";
my $program = "xfer_ftp";
my $markerfile = "/oc1/store001/smh/sync/marker.kody.test";
my $fullogs = "/var/log/proftpd/fullxfer.log";

my $fullogs_fh;
open($fullogs_fh, ">> $fullogs") or die "The file \"$fullogs\" is not accessible.:$!";

my $marker_fh;
open($marker_fh, "+>> $markerfile") or die "The file \"$markerfile\" is not accessible, and this will fail.:$!";

my $fifo_fh;
open($fifo_fh, "+< $fifo_file") or die "The FIFO file \"$fifo_file\" is missing, and this program can't run without it.:$!";

setlogsock 'unix';

while (my $lines = <$fifo_fh>) {
print $fullogs_fh "$lines\n"; # prints complete logs to the file
my @values = split(/ +/, $lines); # cuts for the folder I want in the logs
my $folder = `printf "$values[8]" | cut -d/ -f6 | cut -d - -f1`; # cuts again eliminating unwanted characters
while (my @output = <$marker_fh>) { # stores the current folder names into an array
my $element = $folder;
if (grep {$_ eq $element} @output) { # compares the folder to the array
print "do nothing\n"; } # doesn't matter really, will choose error file later
elsif (grep {$_ ne $element} @output) {
print $marker_fh "$element\n"; } # this prints the new folder to the file
else { print "There is something wrong?\n" }

close $fullogs_fh;
close $fifo_fh;
close $marker_fh;


My goal is for it to run all the time, pretty sure the 2nd loop is causing the error.

In Reply To


Dec 7, 2012, 8:49 AM

Post #2 of 4 (1913 views)
Re: [IlovePERL!] Looking for assistance with a simple script [In reply to] Can't Post

You basically have the following structure:

while (my $lines = <$fifo_fh>) { 
while (my @output = <$marker_fh>) {

Here is what happens:

1. The outer loop is entered, and one line is read via $fifo_fh and stored on $lines

2. The inner loop is entered, and all of the lines are read via $marker_fh. Hence, $marker_fh is at end-of-file now.

3. The inner loop terminates, because all lines have already.

4. The outer loop enters the next execution, i.e. $lines gets the next line from the file.

5. The inner loop is skipped again, since the file handle is still at EOF



Dec 7, 2012, 9:23 AM

Post #3 of 4 (1909 views)
Re: [rovf] Looking for assistance with a simple script [In reply to] Can't Post

I see, interesting. How would you advise I go about creating the double loop so it never ends? I am currently trying to write it differently, using "defined" loops in the beginning.

Veteran / Moderator

Dec 7, 2012, 10:42 AM

Post #4 of 4 (1906 views)
Re: [IlovePERL!] Looking for assistance with a simple script [In reply to] Can't Post

I do not fully understand what you are doing, but if your inner loop reached the end of your file the first time you executed your outerloop, then, if you need to read the same file again, you have to "rewind" it back to the beginning, either by closing this file and reopening it, or using a function such as seek to position the filehandler at the beginning of the file.

Now, if you plan to read this whole file for every line of the other input file, you might have to consider some other better ways of doing it, such as storing the aforesaid file into memory one way or another (assuming it is not too large to fit in memory). It could be stored as an array of lines, or possibly a hash enabling you to access directly to the data of interest for your processing.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives