Basil A. Daoust wrote:
> Help,
> 
> OK - a perl procedure returns a reference to a hash, or at least I think 
> that what it returns.
> 
> datadumper returns the following.
> $VAR1 = {
>           'discrete' => 'text',
>           'composite' => 'plain',
>           'attributes' => {
>                             'charset' => 'us-ascii'
>                           }
>         };
> 
> now all I have is:
> my $data = parse_content_type($ct);
> 
> How to I access $var1{attributes}{charset} as the above returns %data 
> requires explicit package ...
> I know its because I don't fully understand references, help please.

Example:

use strict;
use warnings;
use Data::Dumper; $Data::Dumper::Indent=1; $Data::Dumper::Sortkeys=1;

my $hashref = hashref_returner ();      # get a hashref returned

print Data::Dumper->Dump([$hashref], [qw($hashref)]);   # print it out

# just for some exercise using the hashref simulate dumping it:

foreach (sort keys %{$hashref}) {       # loop through hash keys

        if (ref $hashref->{$_} eq 'HASH') {     # deeper loop for hashref

                print "$_ => {\n";

                my $ref = $hashref->{$_};
                foreach (sort keys %{$ref}) {   # loop through inner hash
                        print "\t$_ => $ref->{$_}\n";
                }
                print "}\n";

                next;
        }
        print "$_ => $hashref->{$_}\n";
}
exit;

#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

sub hashref_returner {

my %hash = (                    # let's assume this is the hash
   'discrete' => 'text',
   'composite' => 'plain',
   'attributes' => {
     'charset' => 'us-ascii',
   }
);
return \%hash;                  # return ref to hash to caller

}

__END__

_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to