Quote:
Originally posted by ivanatora
Code:
push @$list, "$dir/$file"
This is some kind of reference.. a variable that points to a memory where other
variable is stored.. that's all I know of it. Why do you use it, instead of just
@list? And why is it @$ ? There are so many types of references :( ..
|
You're exactly right that
$list is a reference. Earlier on in my code, you'll
see that I created
$list as:
This simply creates an empty array reference. I don't know your programming history,
but just for the sake of explanation, it is much faster to pass an array reference
into a function (or out of for that matter), than it is to pass all the items of an
array. And, since the recursive function in my original code passes the dir listing
into itself, we use the reference to speed it up.
@$ is a perlism that you just get used it. (= It's not visually self-explanatory,
that's for certain. Since
$list is an array reference, we have to convert it, for
the sake of the
push command, into a regular array. The
@$ simply
dereferences the reference.
I think your code looks fine! I have a couple of suggestions for you to consider, too.
1. In reference to the
opendir command, consider the final product you're trying to
achieve, in regards to the
die command. If for some reason the script
encounters an error trying to open a dir, this will terminate the script. Perhaps this is
what you want. Typically, though, a better way to handle it is to generate a warning,
or simply silently ignore the directory on which the error was triggered (or write the
error to a log file).
2. This one is only my opinion, but I feel strongly enough about it to share it with you.
I feel that it is a bad idea to get into the habit of using
& when calling functions. The
reason for this is that
& allows the user to ignore any prototyping done with the function
declaration/definition. IMHO prototyping is a good idea, because it not only forces the
coder to pay attention to what needs to be passed into a function, but it also allows
the coder to reference the function w/o the need for parenthesis (mostly). If you're not
familiar with prototyping, it's similar to how C declares/defines functions. Consider this
code:
Code:
#!/usr/bin/perl
use strict;
use warnings;
sub getMyInfo($$$) {
my($filename) = shift @_;
my($arrayRef) = shift @_;
my($regexp) = shift @_;
print $filename, scalar(@$arrayRef), $regexp, "\n";
}
#MAIN
{
# This produces an error
#
getMyInfo "Bacon.txt";
}
The user of this code would be forced to provide three items to this function, otherwise,
the perl interpreter would complain:
Code:
~> ./prototype.pl
Not enough arguments for main::getMyInfo at ./prototype.pl line 17, near ""Bacon.txt";"
Execution of ./prototype.pl aborted due to compilation errors.
If the code calls the function using the
& operator, here is the output:
Code:
~> ./prototype.pl
Can't use an undefined value as an ARRAY reference at ./prototype.pl line 10.
The second error will more than likely leave a coder scratching their heads, saying
"This sub looks right, why is it giving me an error?" when the error lies elsewhere.
Anyway, as I said, this is just my opinion, and I'm sure that there are those who would
disagree with me.. (= but, if you learned something, then that is what matters to me.
Hope you had a good weekend!