Lee,

First off, thanks for your advice! Took what you said and passed back a hash within {}, making it 'anonymous', which I learned about today after checking out your code.

Now, what we just did, vs. what I was doing in the example without the Method call (if you don't mind, of course). In my example below, I used online examples and set the scalar to be equal to the reference of the subroutine (&hashit()). From the subroutine I passed a reference of the hash (\%hash). Why does this work in the non-class case? How does it work to set a scalar to be a reference to a subroutine passing a reference, and end up with the referenced return value? (That sentence might take more than one read.)

A second question: I'm having trouble getting a clear picture of what anonymous data, or more specifically, a anonymous hash, really is. Is this a reference in itself? If so, how is it different from preceding a hash variable with "\"? If not, how does passing an anon. hash from a method called with:

$var = $obj->hashit();

?

No problem if you don't get to these questions, but they might help seal the deal for me and Perl referencing a little better.

Thanks again for previous help,
Steve



steve abrams wrote:

How do I get a hash back from a class method call?

Without a class involved (works):

sub hashit {
my %hash = ("a", "1", "b", "2");
return \%hash;
}

my $hash_ref = &hashit();

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

With class (doesn't work):

sub hashit { # in class def

class def ? this does not throw an error? or you never tried it?

my %hash = ("a", "1", "b", "2");
return \%hash;
}

$obj = Class1->new();
$obj = &Class1->hashit();       # doesn't work


1) drop the &

2) why are you assigning $obj new() and hashit()?

3) hashit() is probably still main::hashit() not Class1::hashit(), make sure it packaged right.

4) there are no strict or warnings it appears, please post strict and warnings code that you've actually run and tested that illustrates the problem.

This illustrates how it works with all 4 points utilized:

#!/usr/bin/perl

use strict;
use warnings;
# always do this, always always always

use Data::Dumper;

my $obj = Class1->new();

my $hash_ref = $obj->hashit();

print Dumper $hash_ref;

package Class1;

sub new {
    my $class = shift;
    my $self = {};
    bless $self, $class;
    return $self;
}

sub hashit {
    return {a => 1, b => 2};
}


HTH :) Lee.M - JupiterHost.Net

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>





-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to