in reply to Re^3: Seeking advice for OO-related strategy
in thread Seeking advice for OO-related strategy
I realised you were looking for callbacks, but given the number of questions here that are of the form "I want to do X, here's my solution for Y - what's wrong?"Indeed, "X-Y", it's called...
Actually it would have been more cleaner if I had called them callbacks from start, which I didn't for it simply didn't occur to me it was the right term, even though of course I knew about it having read it e.g. in Tk's docs.
However, in certain cases, I want access to variables that I don't want generic access to - or, perhaps, I can't have generic access to. So I put it my own closure:Another interesting technique: seems obvious now that you point it out, but just like most obvious things one can easily overlook it...my @strings = $self->expand_string($foo, sub { /^VAR1$/ && return $blah; /^VAR2$/ && return @some_list; $self->expand_variable() } );
So, from here, there's another minor point I would make with the code you started with: rather than passing in the variable, is it unreasonable to use $_? You may notice my expand_string code does that with do {local $_ = $var; $self->$func()}.Yes and no: although I try to take advantage of Perl's topicalization as often as possible in conjunction with builtins, but for some reason I am not so keen on doing so with subs I write myself; however I have no argument against doing so, I simply generally don't feel like following this approach systematically...
|
|---|