Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: oo code ref

by jbert (Priest)
on Nov 10, 2008 at 15:51 UTC ( #722674=note: print w/replies, xml ) Need Help??

in reply to oo code ref

If you want a goto because you don't want to continue in your existing function any more, then that means you want a return:
return $self->myFunction;
If you really do want a code ref which allows you to make use of $self, then you can create one with sub:
# Pick a method, wrap it up an a coderef and return it sub method_chooser { my $self = shift; return (rand % 2) : ? sub { $self->runAlice; } : sub { $self->runCha +rlie }; } sub do_stuff { my $self = shift; # Dunno what the chooser will choose my $code_ref = $self->method_chooser(); # But run it all the same $code_ref->(); }
Note that method_chooser will return a coderef which stores ('closes over') the value of $self. i.e. it will keep a reference to your object. This is handy, but it obviously counts as a reference for reference counting purposes, i.e. you can get a ref loop that way (and the consequent memory leak).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://722674]
[oiskuu]: ... getlogin refers to tty owner; if you close STDIN, then getlogin returns undef. I get undef in my shell (running under X)
[tye]: Then there is a more persistent big of data similar to getlogin(). I don't believe even daemonizing drops it. So you can tell services that were started at boot time from those started by a person.

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (10)
As of 2017-06-23 19:17 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (554 votes). Check out past polls.