A few comments about the code:

1) There is no need to put '&' in front of the subroutine call. This is old Perl syntax and now means "ignore prototype". Basically don't use subroutine prototypes for your functions unless you are trying to emulate a built-in function, which I do not recommend except in rare cases. For the Perl built-in functions, you can eliminate parens, the "()" for the parameters in many, but not all cases. It is always ok to put parens in for the parameter list. Bottom line: you don't need & in front of the subroutine "oops".

2) There are many reasons why an open() will fail...wrong permissions, etc. I would strive for a design where the "open" always works even if the file is contains nothing (NULL). File doesn't open would mean "internal program error". The program logic would have to be re-worked at bit, but there in my opinion should be a big difference between file doesn't open and "no inventory".

3) Naming is important. THEFILE is not that descriptive. Of course this is a file! Perhaps a better name would be SUPPLIER or INVENTORY?

4) In a case like this, the 3 argument form of open which specifies the mode explictly is probably better. open(INVENTORY,'<',$path) or die "blah..$!"; This way there is not a possiblity of $path becoming a write operation that destroys the input due to malformed input of the $basename.

5) Perl map{} is a transformative operator and could be used to simplify my ($title, $image, $counter...,@inventory) = map{chomp; $_}<THEFILE> I've seen map{} used in place of large (30 line foreach loops), but I personally prefer this syntax only for short, clear transformations.
Update: map{chomp} changed to map{chomp; $_} This is an important and common mistake. Ooops!

6) In Perl, the $#version is the last "index". There are a lot of tricky Perl variables. @array in a scalar context is the number of elements in @array. for example: if (@array > 5). If I want the number of the last index, I use @array-1 instead of $#array. I've not seen a case yet where this -1 subtraction means anything significant in terms of execution time and its one less special variable to worry about.


In reply to Re: Need to count the number of entries in a array by Marshall
in thread Need to count the number of entries in a array by Baffled

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.