@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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.