The problem with this solution is that we no longer have a "readdata" routine. Instead, we have a routine that should probably be called "read_and_print_data" (or something like that). Subroutines generally should not have side effects like this without good reason. Admittedly, for such a small program it doesn't matter as much, but when the programmer wants to later create a more generalized routine, side effects will cause problems. For example, I almost changed it to this:
sub read_data { my $file = shift; open FH, '<', $file or die "Cannot open ($file) for reading: $!"; chomp (my @data = <FH>); close FH; return wantarray ? @data : \@data; }
This routine is a nice little black box that returns an array (or reference) to the lines contained in the file. If you wanted to print out the contents, putting this functionality in the subroutine means that this routine is tough to reuse if you want to just read a file and not print the contents (of course, some might question the use of chomp, given that argument).
Cheers,
Ovid
New address of my CGI Course.
In reply to Re: Re: Printing an array
by Ovid
in thread Printing an array
by shemyaza
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |