@main::fields = split(/\|/, $_);
I cannot imagine why you are doing this. Accessing the variable from the main scope goes against good programming ideologies. The point of being able to pass arguments to a subroutine, whether scalar, hash, array, or reference, is to avoid directly accessing data from that exists outside of the subroutine.
Also, I'm wondering whether or not you looked at the code I provided. From that code, it is quite simple to call a seperate subroutine based on whether or not the part number appears in the flat text file:
#!/usr/bin/perl -w use strict; sub get_part_num { my $part; while (!defined $part || $part =~ /[^0-9]/) { print "Part Number? (None to exit) "; chomp( $part = <STDIN> ); } die "Program terminated.\n" if $part eq ""; return $part; } sub search { my ($file, $part) = @_; my $found_match = 0; open my $db, "<$file" or die "Could not read file: $!"; while (<$db>) { chomp; my @fields = split(/\|/, $_); if ($fields[0] eq $part) { ++$found_match; found_match(@fields); last; } } no_match($part) if $found_match == 0; } sub found_match { my ($part_num, @data) = @_; print "Part $part_num found: \n\t", join("\n\t", @data), "\n"; } sub no_match { my $part_num = shift; print "Part #$part_num was not found in the database.\n"; } search( 'fai.txt', get_part_num() );
In reply to Re: Missing the if statement and going directly to else
by Coruscate
in thread Missing the if statement and going directly to else
by Bismark
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |