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>




Reply via email to