in reply to How to Print Return Data from Subroutine

After changing your parentheses to curlies in your sub, and adding a,b,c variables, then assigning the output of your sub to a scalar variable (a reference), I just print the results using Data::Dumper. I also print what is returned by the ref function:
use strict; use warnings; use Data::Dumper; my $ref = parse(); print Dumper($ref); print ref $ref, "\n"; sub parse { my @a = 0..2; my %b = (foo => 7); my $c = 55; return { a => \@a, b => \%b, c => $c } } __END__ $VAR1 = { 'c' => 55, 'a' => [ 0, 1, 2 ], 'b' => { 'foo' => 7 } }; HASH

Replies are listed 'Best First'.
Re^2: How to Print Return Data from Subroutine
by Anonymous Monk on Aug 24, 2008 at 02:50 UTC
    Thank you so much. If you can be a little bit more patient with me, how do I print them in the following format without using data dumper?
    print $ref->{c}; # This gave me 55 which is what I want. foreach my $i ($ref->@a) { print $i; # I want to see the content of the array, this + gave me an error. } foreach my $i ($ref->%b} { print $i; # I want to see the content of the hash. }
      foreach my $i ($ref->@a) { print $i; # I want to see the content of the array, this + gave me an error. }

      I can't tell if this is what you mean or not:

      foreach my $i ( @{$ref}{@a} ) { print $i; }

      Or maybe it's simpler than that:

      foreach my $i ( @a ) { print $i; }
      foreach my $i ($ref->%b} { print $i; # I want to see the content of the hash. }

      "b" in the hash is now only a hash key, and therefore you cannot put a "%" sign before "b", but you need a %{...} to dereference the hash. This will work:

      foreach my $i ( %{ $ref->{b} } ) { print $i; # I want to see the content of the hash. }

      (It will print keys and values by turns.)