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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |