in reply to passing of variables

Here’s how to do what you want to do:
use strict; use warnings; use Data::Dump; { my %toys = ( frisbee => 5 ); dd \%toys; play_around(\%toys); dd \%toys; } sub play_around { die "expected one arg" unless @_ == 1; die "expected hash arg" unless ref($_[0]) eq "HASH"; our %t; local *t = $_[0]; $t{leggo} = "yes"; }
When run, that prints out:
{ frisbee => 5 } { frisbee => 5, leggo => "yes" }
See? Nice and easy!

Replies are listed 'Best First'.
Re^2: passing of variables
by itsscott (Sexton) on May 06, 2011 at 18:13 UTC
    Interesting, this method actually seems easier to read too, I especially like the die's that you have included, I try to error check, but sometimes in perl am not sure how, so thank you again for this wonderful example.
      I think that's a bit "advanced" or "tricky", and not something you should imitate just now. Get used to dealing with $$name{key} and $name->{key} syntax, as that is present everywhere in modern Perl.

      Using a localized package variable instead of a true lexical variable will lose the benefit of lexical variables: if the function calls another function that uses the package variable, it will see the current local-ized use of it.

        Wow, ok, well, this is where I get a little lost with pointers, I get that way in C also when it gets 'weird' like that (pointers to pointers etc). I would love to see a short example as I can't quite see how I would write it. Also is it a bad thing to have a few 'master hashes' that I control, and pass around and modify? The first time I wrote this, I had almost all global vars and it was terrible! I'm all self taught and all this help sure has opened my eyes to the power and ease, tho at times complex ways to work with perl to be efficient. Thanks again for all the help from all the members, it's very much appreciated!
      The Perl debugger will let you see exactly what is going on. I almost always end up using it when dealing with nested data structures or hashes.