in reply to Re: (jeffa) Re: How to open sub FH when filename is passed to sub?
in thread How to open sub FH when filename is passed to sub?
The reason for the warnings is because your hash probably has keys, but no values. Now, your problem is two fold:
Good luck, and please read this book: Learning Perl.use strict; my @array1 = (1,2,3,4); my @array2 = (5,6,7,8); my %hash = ( key => 'val', foo => 'bar', ); print '=' x 20, "\nwrong way:\n"; wrong1(@array1,@array2); sub wrong1 { my (@a1,@a2) = @_; print "array 1: @a1\n"; print "array 2: @a2\n"; # notice that @a2 is empty } print '=' x 20, "\nright way:\n"; right1(\@array1,\@array2); sub right1 { my @a1 = @{ shift @_ }; my @a2 = @{ shift @_ }; print "array 1: @a1\n"; print "array 2: @a2\n"; # multiple arrays must be passed in as references } print '=' x 20, "\nwrong way:\n"; wrong2(%hash); sub wrong2 { my %hash = %_; while (my($k,$v) = each %hash) { print "$k => $v\n"; } # %_ is not special, use @_ instead - always! } print '=' x 20, "\nright way:\n"; right2(%hash); sub right2 { my %hash = @_; while (my($k,$v) = each %hash) { print "$k => $v\n"; } # a hash is really just a special kind of array # and yes, multiple hashes must be passed as references too } # last - a hash slice my %slice; @slice{@array1} = @array2; print '=' x 20, "\nhash slice:\n"; while (my($k,$v) = each %slice) { print "$k => $v\n"; } # and don't creat hash slices inside an subroutine's # argument list - that is bad bad bad ;)
jeffa
Cargo Cult Programming - Just say 'NO!'
|
|---|