That looks alright to me, the second version avoids repetition and is very clear, since that's done only once there's no need to worry about optimisation. You can do this with a for loop instead of the map (for two reasons: first, using map for turning a list into another and for for doing something with every element of a list makes the intent clearer, the second reason is that map in a void context will trigger the same kind of reactions as goto). I guess something with closures (I like closures :) ) could work, but I gave it a try and only ended up with something harder to read than your code. But here is another way to do it, in the spirit of TIMTOWTDI.
{ no strict 'refs'; *$_ = eval "sub { &main::trigger; shift()->SUPER::$_(\@_) }" for q +w(STORE CLEAR PUSH POP SHIFT UNSHIFT); }
Notice that I didn't use the "$self" scalar twice (well, I didn't use it at all actually), because $var->method(@params) already adds $var as the first parameter.
Your problem made me think of Python's decorators, so I ended up on this thread which could be of interest to you. The thing is, you may not want to modify the subs in Tie::StdArray, in case you use another module that uses this module, so you have to define your own methods anyway.
Edit : and I think you'll need to tie the contained scalars too if you want to call your trigger function only when your elements are modified. Your tied array should actually just tie any scalar pushed into it.
In reply to Re: Is tie inteded to be used in this way?
by Eily
in thread Is tie inteded to be used in this way?
by Discipulus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |