in reply to Elaborate Records, arrays and references

The first error would have been caught by use strict;. Don't hide errors; use use strict; use warnings;.

$$reference->[m_emailAddress]

should be

$$reference->{m_emailAddress}

Secondly, you treat $$reference->{m_emailAddress} as a reference to an (anonymous) array of addresses. It is really a reference to a reference to an (anonymous) array that contains one reference to an (anonymous) array of addresses.

$$reference->{m_emailAddress}->[0]

should be

${ $$reference->{m_emailAddress} }->[0]->[0]

What's with references to scalars and the crazy stacking of references?

use strict; use warnings; sub print_record { my ($rec) = @_; print("username: $rec->{m_username}\n"); print("password: $rec->{m_password}\n"); print("email: @{ $rec->{m_email_addresses} }\n"); } my $stg_username = "PerlMonk"; my $stg_password = "VeryGoodPassword"; my @stg_email_addresses = ( "Monk1", "Monk2", "Monk3" ); my $ellab_rec = { m_username => $stg_username, m_password => $stg_password, m_emailAddress => \@stg_email_addresses, }; print_record($ellb_rec);

Replies are listed 'Best First'.
Re^2: Elaborate Records, arrays and references
by KyussRyn (Acolyte) on Jan 06, 2011 at 04:44 UTC
    @Ikegami-san,

    I was looking at having ways of updating the variables on the fly, but reviewing my code passing the Elaborate Record as a parameter and localising it may work.

    The one thing I am trying to do is for one subroutine to pass a Elaborate Record as a parameter to a second subroutine, this second subroutine will update the Elaborate Record and then send it back to the 1st subroutine. The two subroutines are most likely going to be in different packages so just trying to work out how to best do this.

    Thanks for your reply and the help in understanding all this. I also really appreciate the final example aswell. Very helpful.

      You don't need all those levels of indirect to do what you want to do either.

      use strict; use warnings; sub change_record { my ($rec) = @_; $rec->{m_username} = uc($rec->{m_username}); } { my $stg_username = "PerlMonk"; my $stg_password = "VeryGoodPassword"; my @stg_email_addresses = ( "Monk1", "Monk2", "Monk3" ); my $rec = { m_username => $stg_username, m_password => $stg_password, m_emailAddress => \@stg_email_addresses, }; print("username: $rec->{m_username}\n"); change_record($rec); print("username: $rec->{m_username}\n"); }

      The $rec inside of change_record is a different variable than the one on the outside (two "my"), but they both hold a reference to the same hash.