Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Noob OO Question

by Anonymous Monk
on Jun 07, 2010 at 21:41 UTC ( [id://843579]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Below is my package and script.
package Hi; use strict; use Data::Dumper; $DATA::Dumper::Deparse = 1; sub new { my ($class, %args) = @_; my $self = { method_c => sub { my ($var_c) = @_; return $var_c . 'this is me.'; }, %args, }; return bless $self, $class; } sub method_a { my ($self, %args) = @_; return $self->{test_a}; } sub method_b { my ($self, %args) = @_; return $self->{test_b}; } sub method_d { my ($self, %args) = @_; my $method_c = $self->{method_c}; my $execute_c = $method_c->($method_c); return $execute_c; } 1; ----------------------- #!/usr/bin/perl use strict; use Hi; my ($get_a, $get_b) = ('get_a', 'get_b'); my $test = new Hi( test_a => 'hello', test_b => \sub{ return 1 }, test_c => \sub{ get_me( $get_a, $get_b ) }, ); sub get_me { my ($get_a, $get_b) = @_; return $get_a . $get_b; } print $test->method_a() . "\n"; print $test->method_b() . "\n"; print $test->method_d() . "\n";
This is the output of the script.
hello REF(0x9fb3bdc) CODE(0x9fd3208)this is me.
How do I execute the different methods in the Hi.pm package?

Replies are listed 'Best First'.
Re: Noob OO Question
by ikegami (Patriarch) on Jun 07, 2010 at 21:45 UTC

    How do I execute the different methods in the Hi.pm package?

    You are already executing every method in package "Hi".
    Class method newnew Hi(...) (although Hi->new(...) is usually recommended)
    Object method method_a$test->method_a()
    Object method method_b$test->method_b()
    Object method method_c$test->method_d()

Re: Noob OO Question
by JavaFan (Canon) on Jun 07, 2010 at 22:02 UTC
    What is your question? Since you're executing various methods in the Hi.pm, I think you mean another question. Perhaps you're confusing yourself by making methods returning references of references to subs. But I've no idea what your intention is.
Re: Noob OO Question
by Anonymous Monk on Jun 07, 2010 at 22:28 UTC
    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.

      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()'?
Re: Noob OO Question
by Anonymous Monk on Jun 08, 2010 at 00:16 UTC
    I was looking at this...
    sub method_d { my ($self, %args) = @_; my $method_c = $self->{method_c}; my $execute_c = $method_c->($method_c); return $execute_c; }
    And I realized that it should be this...
    sub method_d { my ($self, %args) = @_; my $method_c = $self->{method_c}; my $execute_c = $method_c->($self->(test_c)); return $execute_c; }
    And it is giving me this output.
    Can't use string ("REF(0x8420678)this is me.") as a subroutine ref whi +le "strict refs" in use at Hi.pm
    But I wanted this output.
    get_aget_bthisisme.
Re: Noob OO Question
by bichonfrise74 (Vicar) on Jun 08, 2010 at 18:37 UTC
    I think you want something like this...
    sub method_d { my ($self, %args) = @_; my $method_c = $self->{method_c}; my $test_c = $self->{test_c}; my $exec_test_c = $$test_c->(); my $exec_method_c = $method_c->( $exec_test_c ); return $exec_method_c; }
Re: Noob OO Question
by Anonymous Monk on Jun 08, 2010 at 05:44 UTC
    Can anyone help me with this? I seem to be missing something to get $test->method_d() to have the correct output and not give me CODE(XXXX).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://843579]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (8)
As of 2024-04-25 11:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found