Gossamer Forum
Home : General : Perl Programming :

Pass hash to sub

Quote Reply
Pass hash to sub
Hi

Can I safely pass a hash to another sub in Perl?

I want to be able to do:

&my_sub(%hash);

and then have

sub my_sub {

%hash = %_;

}

Would that work? Would it preserve the hash?

Thanks.

- wil
Quote Reply
Re: [Wil] Pass hash to sub In reply to
Hmm interesting Laugh....I've never seen it attempted like that before.

What you need is...

Code:
some_routine(%hash);

sub some_routine {

my %hash = @_;

}

-OR-

Code:
some_routine(\%hash);

sub some_routine {

my $hash = shift;

}

Last edited by:

PaulW: Dec 18, 2001, 6:16 AM
Quote Reply
Re: [PaulW] Pass hash to sub In reply to
Hm. Does your first example work? So you can't pass the hash, you have to pass it as an array?

I guess that my first post will work if your examples work?

Hmm. I'll have to try a few of these out.

Thanks.


- wil

Last edited by:

Wil: Dec 18, 2001, 6:20 AM
Quote Reply
Re: [Wil] Pass hash to sub In reply to
Nope %_ won't work.

...and yes both my examples work.