in reply to Re: Noob OO Question
in thread Noob OO Question

When you instantiated the Hi package you passed references to some subroutines, in method_b for example you return the reference back to the caller, you are not executing the method

Consider this

my $sub_ref = \sub { my $who = shift; print "hello $who\n"; }; #In order to execute need to deref by prefixing with $ $$sub_ref->('dude');

coderefs are tricky so try to keep your examples simple until you get the grasp on it

Replies are listed 'Best First'.
Re^3: Noob OO Question
by Anonymous Monk on Jun 07, 2010 at 23:52 UTC
    Here's a sub-question regarding this. So, I tried dereferencing it by running this.
    my $testing = $self->{test_b}; print $$testing->();
    This works!! But how come I cannot do this?
    print $$self->{test_b}->();
    It gives us this output.
    Not a SCALAR reference at Hi.pm
    Also, how do I dereference '$test->method_d()'?
      $$self->{test_b}->() first dereferences $self. Writing $self->{test_b}->() should work. Or without arrows: &{$$self{test_b}}(). Note the placement of the outer parens.
      Also, how do I dereference '$test->method_d()'?
      You don't. $test->method_d() returns a string.
        Typing it like this doesn't work for me.
        $self->{test_b}->()
        It gives me this output.
        Not a CODE reference at Hi.pm