in reply to Noob OO Question

Oh, I mean I am not getting the correct 'output' from the methods. For example,
print $test->method_b() . "\n"; print $test->method_d() . "\n";
I was expecting it to have something like this...
print $test->method_b() . "\n"; ---> 1? print $test->method_d() . "\n"; ---> get_aget_bthis is me.
But instead I am getting this result.
REF(0x9fb3bdc) CODE(0x9fd3208)this is me.

Replies are listed 'Best First'.
Re^2: Noob OO Question
by bluescreen (Friar) on Jun 07, 2010 at 23:20 UTC

    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

      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.