This is my first reply, so if something isn't formatted just right, I will apologize in advance. But maybe the meaning will show through in any event :)
I am by no means a monk, but being a full time Perl programmer, I know a lot of shortcuts. (Correct short cuts, that is, not hacks.) So, I kinda ramble over most of the code, I don't mean to step on toes or say the way you have it isn't right.. it's just a shorter way of doing the same thing. Like your argument code:
my $num_of_params;
$num_of_params = @ARGV;
if ($num_of_params < 2)
{
die ("\n You haven't entered enough parameters !!\n\n");
}
I would write it like this:
die "\nYou haven't entered enough parameters !!\n\n" unless $ARGV[1];
Does the same thing, just easier to type.
Next, you have:
open (INFILE, $ARGV[0]) or die "unable to open file";
open (OUTFILE, ">$ARGV[1]");
If this is for end-users, you are right in not being very descriptive with your error messages. However, especially during debugging, I would be a lot more loud and graphic with your errors, something like:
open (INFILE, $ARGV[0]) or die "unable to open file $ARGV[0]: $!\n";
open (OUTFILE, ">$ARGV[1]") or warn "unable to open file $ARGV[1]: $!\
+n";
should work fine. Again, if it's for end-user use, turn off the errors once it's working if you need to.
Next you have:
-snip-
@array = ();
@array = split (/\s+/, $line);
-snip-
# if i print $self here it prints out all of the results from that cat
+ergory
I think the post above me covered this, and probably already answered your main question. The reason that $self is is valid here is because the array is still valid. Each time you run the loop you are clearing the array, thus the one entry that you are displaying is probably the last entry in the file you read in, because the array isn't cleared at the end.
Next you have:
$choice = <STDIN>;
# remove the newline character from the chosen option.
chomp $choice;
if ($choice eq 1)
Again, what you have works just fine, but since you are only reading in one character, why not just use 'getc'? Something like this:
if (getc(STDIN) eq "1")
Since getc only gets one character, no need for a chomp, and you save yourself a variable use.
So, I hope that I got across the help I wanted to give without seeming to pick nits. Your code worked just fine (excepting the reason for the post of course), it's just that personally, I am always looking for a better way to do things, and in this case, I saw a few that I thought I would share.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.