Number of variables passed to function may vary
If each input and each output are independent as they appear, it's a bad design. Use a loop on the outside of the function, not inside.
sub getfile { my ($qfn) = @_; open(my $fh, '<', $qfn) or die("Can't open \"$qfn\": $!\n"); <$fh> } my @files = ('file.txt', 'file2.txt' ); my @file_contents = map { [ getfile($_) ] } @files;
The contents and the names and the contents are in parallel arrays, so it's still not optimal. Fix:
sub getfile { my ($qfn) = @_; open(my $fh, '<', $qfn) or die("Can't open \"$qfn\": $!\n"); <$fh> } my @files = ('file.txt', 'file2.txt' ); my %file_contents = map { $_ => [ getfile($_) ] } @files;
You can access the data as follows:
for my $file (keys %file_contents) { print("File $file:\n"); my $content = $file_contents{$file}; for my $line (@$content) { print($line); } print("\n"); }
In reply to Re: Multiple variables sub
by ikegami
in thread Multiple variables sub
by Kirche
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |