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

Hi, I created an array of hashes with the following command:
push @AoH, { email => $email, reason => $reason, std_reason => $std_re +ason };
when I print this from inside the script it works fine but when I pass this array to a subroutine it doesn't print anything. This is the code I am using to print:
my $href = ""; my $role = ""; for $href ( @AoH ) { print "{ "; for $role ( keys % +$href ) { print "$role= +$href->{$role} "; } print "}\n"; }
The subroutine I am calling is Bounce(@AoH) which is in a .pm file and here is how I am initializing it:
sub Bounce($) { my @AoH = @_; <rest are same as above>
not sure what I am doing wrong, there is no error either.

Replies are listed 'Best First'.
Re: Array of Hashes
by trizen (Hermit) on Apr 19, 2012 at 15:37 UTC
    You need to change the subroutine prototype from
    sub Bounce ($) { ... }
    into
    sub Bounce (@) { ... }
    or (better) don't use prototypes at all.
Re: Array of Hashes
by stevieb (Canon) on Apr 19, 2012 at 16:41 UTC

    In addition to what trizen said regarding not using prototypes, it is better practice to declare/define your variables as tightly as you can (ie. within the scope you use them if possible). In your case, you can remove the $href and $role declarations by writing the block like this:

    for my $href ( @AoH ) { print "{ "; for my $role ( keys %$href ) { print "$role=$href->{$role} "; } print "}\n"; }

    Update: This way, anyone looking at your code in the future won't be left wondering if you are reusing those variables later on in the code beyond the nested for loop.

    Update: For future reference, also see: How do I post a question effectively?