Jeroen Lodewijks wrote:
> 
> Hi all,

Hello,

> I have a 2 questions about the internal representation of a hash or array.
> Consider this piece of code:
> 
> 1)
> 
> sub PassHash
> {
>     my (%hash) = @_;
> 
>     $hash{$some_key} = 'test';
>    ...
> }
> 
> PassHash(%hash);
> 
> What happens internally?

perldoc perlsub

> Will the whole contents of the hash be copied in memory?

Yes, the statement "my (%hash) = @_;" copies the list passed to
PassHash() into the hash %hash.

> Or is only a reference passed?

If you use the contents of @_ directly then you are dealing with
references to the original variables.

> Will the $some_key element show up after PassHash?

Yes.

> Does the same apply to arrays?

Yes.

> 2)
> 
> sub CreatHash
> {
>     my %hash;
> 
>     ... do something with hash;
> 
>     return %hash;
> }
> 
> sub OtherProc
> {
>     my %new_hash = CreateHash();
> 
>     ....
> }
> 
> What happens internally?

perldoc perlsub

> Will the whole contents of the hash be copied in memory to %new_hash?

The whole contents of %hash will be flattened to a list in memory and
the list will be copied to %new_hash.

> Or is only a reference passed?

No.

> When is %hash garbage collected and when is %new_hash?

When they go out of scope at the end of the subs they are in.

> Does the same apply to arrays?

Yes.

> Thank you for you answers in advance. I need this information to
> optimise memory requirements for my program.

perldoc -q "How can I make my Perl program run faster"
perldoc -q "How can I make my Perl program take less memory"

And if you are really brave search for 'memory' in perldebguts.pod

perldoc perldebguts



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to