in reply to pulling information from a delineated text file using a subroutine

Amongst other things, the following line is in error and will cause your script to fail:

open(FILE, '>>fai.txt') || die $!;

The >> cause the file to be opened in "append" mode. You want "read" mode. You can do this to correct this:

sub search { my ($file,$part_number,$revision) = @_; open FILE "< $file" or die "Cannot open ($file) for reading: $ +!"; local $_; while (<FILE>) { # search line for $part_number

Note that I have have made some changes. First, I have explicitly passed the necessary arguments to the subroutine. This makes it easier to reuse or move to a library, if necessary. Further, I've localized $_. This restricts changes to $_ to the current scope, thus ensuring that if another part of your program uses $_, you won't accidentally have a strange value in there. See perldoc -f local for more information on this.

Cheers,
Ovid

New address of my CGI Course.
Silence is Evil (feel free to copy and distribute widely - note copyright text)

Replies are listed 'Best First'.
Re: Re: pulling information from a delineated text file using a subroutine
by Bismark (Scribe) on Jan 16, 2003 at 03:39 UTC
    I see the error for the append option, but your post confused me. If I use your code as you typed it, where does it know to open the fai.txt file? Will the rest of the while block stay the same? Thanks again,
    Kerry credo quia absurdum