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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.