in reply to Need to count the number of entries in a array

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.

Replies are listed 'Best First'.
Re^2: Need to count the number of entries in a array
by chromatic (Archbishop) on Jan 16, 2012 at 06:01 UTC
      Please tell me what a better formulation is?

      my ($title, $image, $counter...,@inventory) = map{chomp}<THEFILE>

      Ok, I made a simple mistake, and one that is common: map{chomp} should be map{chomp; $_} The return from map is the last statement, here $_ is chomped and that result, the chomped version of $_ should be the last statement.

      Thanks for pointing this out!

        Don't guess. Try it out, or read the documentation for chomp. I've made that mistake before with map and it's a head-scratcher the first two times.

        (It returns—depending on the line endings of the file—a bunch of copies of $/.)


        Improve your skills with Modern Perl: the free book.

        Most of "us", including the OP, don;t need telling.

        *You* could look it up.

        Or even try your fascile suggestions out, *before* advising the use of broken code as a replacement for perfectly fine, working code.