dannoura has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm passing a hash and an array to a sub-routine with:

process_article(*WFH, %{$locations{$article}}, @{$lists{$list}});

And the sub-routine accepts them with:

my $fh=shift; my (%locations, @keywords)=@_;

The problem is that they somehow get mixed up in the sub-routine. Any way to fix that (without using references)? (yes, I'm using strict).

-----------------------------------

Any comments about coding style are welcome.

Replies are listed 'Best First'.
Re: problem with passing array and hash to sub-routine
by pzbagel (Chaplain) on Aug 06, 2003 at 07:35 UTC

    Well, one answer/question is "why NOT use references?"

    Another idea may be to pass in the size of the array, then you can slice them apart or something.

    use warnings; use strict; use Data::Dumper; my @a=qw(1 2 3 4 5); my %b=( a => 1, b => 2, c => 3 ); sub printdata { my $size=shift; my @x=@_[0..$size]; my %y=@_[$size+1..$#_]; print Dumper(\@x, \%y); } printdata($#a, @a, %b); #__OUTPUT__ $VAR1 = [ '1', '2', '3', '4', '5' ]; $VAR2 = { 'c' => 3, 'a' => 1, 'b' => 2 };

    I don't usually do this. I usually work with references since you take a performance hit when perl is forced to copy your arrays and hashes into a new memory slot when you pass then into a function. If there is a better way to do this, lemme know! I could swear I read about this in the camel book, but I am too tired to look it up now.

    HTH

Re: problem with passing array and hash to sub-routine
by Skeeve (Parson) on Aug 06, 2003 at 08:08 UTC
    You already have references. So why not pass them?
    process_article(*WFH, $locations{$article}, $lists{$list}); sub process_article { local(*FH)= shift; my($loc, $key)= @_; # then use print FH "anything" to print to your file # $loc->{keyword} instead of $location{keyword} # and $key->[index] instead of $keyword[index] }
Re: problem with passing array and hash to sub-routine
by PodMaster (Abbot) on Aug 06, 2003 at 07:28 UTC
    `perldoc perlsub'
    use strict; { local $\="\n"; print for %INC; } sub foy { my %a = %{shift(@_)}; my @b = @{shift(@_)}; return %a . ' ' . @b; } die foy ( { 1 .. 10 }, [ 1..10 ] ); __END__ strict.pm C:/Perl/lib/strict.pm 4/8 10 at - line 11.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      ++PodMaster. That tripped me up for a minute. However, I have to point out that technically your are using anonymous arrays/hashes(and therefore references;)

      Peace

Re: problem with passing array and hash to sub-routine
by sgifford (Prior) on Aug 06, 2003 at 18:26 UTC
    You can hide the fact that you're using references from the caller using prototypes:
    #!/usr/bin/perl sub process_article(*\%\@) { my $fh=shift; my ($locations, $keywords)=@_; print "Locations: ",join(" ",keys %{$locations}),"\n"; print " Keywords: @$keywords\n"; } %l = (Flint => 'Michigan', Bangor => 'Maine', Atlanta => 'Georgia', ); @keywords=qw(magic snarf prognosticative doohickey); process_article(STDIN,%l,@keywords);