PDA

View Full Version : Trouble running a perl script in the background


Stevewr
10-12-2007, 05:13 PM
Running on Unix...

When I run my Perl script in the background, or when I switch it from the foreground to the background, it seems to freeze (or maybe endlessly loop) on the following line in the code:

@lines = `more test.log`;

I have to go and kill the script.

Can anyone please tell me what is going on and how to fix it? It works fine when I run the program in the foreground.

If you need anymore information for me about the script, please let me know.

Thanks.
Steve.

FishMonger
10-12-2007, 08:07 PM
Why are you using 'more'? The more command is a pager utility that needs to run in the forground because it expects user input, i.e., pressing “Enter” to see the next “page”.

Other's have used the cat command for this purpose, but please don't. Instead you should use the methods that Perl provides.

open(my $file, '<', 'test.log') || die “can't open log file <$!>”;
my @lines = <$file>;
close $file;

However, slurping an entire file into an array is rarely the best approach. Normally you'd want to loop through the file, processing each line as you go.

open(my $file, '<', 'test.log') || die “can't open log file <$!>”;

while(my $line = <$file>) {
# process $line as needed
}
close $file;