in reply to hash parameter question

If by "nested hash" you mean a hash inside a hash, or really any kind of complicated data structure, it is a lot easier to pass the argument as a hash reference. ie,
sub find_deletion_candidates { my $users=shift; my %users = %{$users} #etc }
and earlier on you had
my $users = { %some_complicated_hash }; find_deletion_candidates($users);
Good luck!

UPDATE: Changed "you have to" to "it is a lot easier to" after chromatic's reply.

Replies are listed 'Best First'.
Re^2: hash parameter question
by chromatic (Archbishop) on Dec 12, 2005 at 23:04 UTC
    ...you have to pass the argument as a hash reference

    Untrue:

    use Test::More tests => 2; my %nested = ( first => { one => 'two' }, second => { three => 'four' +} ); sub get_element { my ($level_one, $level_two, %nested) = @_; return $nested{ $level_one }{ $level_two }; } is( get_element( 'first', 'one', %nested ), 'two', 'first element +access' ); is( get_element( 'second', 'three', %nested ), 'four', 'second element + access' );
      Well, chromatic is right, but his answer bothered me anyway. After thinking about it for a bit I believe I put my finger on why. I tend to parse arguments using the my $arg1 = shift;my $arg2 = shift; idiom. I know, the OP didn't do that, but bear with me.

      Parsing the arguments like chromatic did will likely lead to headache if you are passing anything other than scalars. And if your sub has more than 1 argument, there is a good chance you are. Passing arguments as scalar refs just makes my code simpler to think about. That's why I almost always do it that way, and why I gave the advice I did. Examples of headaches and non-DWIM behavior are in code below, which produces errors on both subs:

      use strict; use warnings; my %nested = ( first => { one => 'two' }, second => { three => 'four' +} ); pass_the_hash_1(%nested, 'second argument'); pass_the_hash_2(%nested, 'second argument'); sub pass_the_hash_1 { my %first_argument = shift; my $second_argment = shift; print "$first_argument{first}{one} should be two\n"; } sub pass_the_hash_2 { my %first_argument = shift; my $second_argment = shift; print "$second_argment should be second argument\n"; }