Le Blanc wrote:
> Greetings,
>   I have a quick question. In the code below I get an error message
> that says 'Global symbol "@fields" requires explicit package name'
> The line that causes the error is highlighted in red. Obviously it
> has to do with how I declared the variable in my subroutine. I tried
> moving curly brackets and I tried other ways of declaring. Could
> someone please tell me the correct way to do this. The perldoc's are
> still somewhat cryptic to me. I thought that moving the close(FILE)
> would help, but it did not.
>
>
> #this is the subroutine to search the file
> sub search{
> open(FILE, '<fai.txt') ||  die $!;  <-- Also, is this statment
> correct to open a file for read only?

Yes, fine.

>

Put 'my @fields'  here.

>  while (<FILE>) {
>         chomp;                  # remove newline
>         my @fields = split(/\|/, $_);
>

    @fields = split '|';

>         # test whether the search string matches part number
>  if ($fields[0] =~ /$PartNumber/ ) {

    if ($fields[0] eq $PartNumber) {

>                 print "$PartNumber Rev. $Revision has a First Article
>  Report\nWould you like to view it?"; }
>
>
>
>  }}

Your while statement finishes here, and so your original @fields
vanishes.

The second closing brace terminates the subroutine!!

(Assuming the second brace is removed) Your code below would be executed
whether or not the part number was found. Since you can close the file
any time after a matching record is found, I suggest you need something
like:

    my $found = 0;

    while (<FILE>) {
        :
        if (match) {
            $found = 1;
            close FILE;
        }
    }

    if ($found)
    {
        :
    }

>
>
> my $answer = <STDIN>;
>
> chomp($answer);
>
> # If the answer is yes then fill the form with the information
>
> if ($answer eq "yes") {
>    print "\n\n\none second please......\n";
>    print "retrieving file........\n";
>    print "$fields[0]: $fields[1]: $fields[2]: $fields[3]\n";
>
> }
> # If the answer is no then die
>    else {
>     print "ok, well have a nice day.\n";
>    }
>
> close(FILE);

Could be closed earlier - see above.

HTH,

Rob




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to