Hello,
suppose i have an array and i wont to trigger some action when some of the element are modified, but not when they are accessed.
i discovered the tie ability of Perl and seems appropriate:

Is intended to be used in this way?
#!perl use strict; use warnings; {#bare block because package BLOCK appeared only 5.14 package Arraytrigger; use Tie::Array; use vars qw(@ISA); @ISA = ('Tie::StdArray'); sub STORE { &main::trigger; my $self = shift; $self->SUPER::STORE($self, @_); } sub CLEAR { &main::trigger; my $self = shift; $self->SUPER::CLEAR ($self, @_); } sub PUSH { &main::trigger; my $self = shift; $self->SUPER::PUSH($self, @_); } sub POP { &main::trigger; my $self = shift; $self->SUPER::POP($self, @_); } sub SHIFT { &main::trigger; my $self = shift; $self->SUPER::SHIFT($self, @_); } sub UNSHIFT { &main::trigger; my $self = shift; $self->SUPER::UNSHIFT($self, @_); } } #package main.. sub trigger{print "Triggered [@_]\n"} tie my @arr, 'Arraytrigger'; print "\tSetting list:\n" and @arr = qw(a b c d e); print "\tSetting one:\n" and $arr[0]=0; print "\tpushing:\n" and push @arr,'f'; print "\tpopping:\n" and pop @arr; print "\tshifting:\n" and shift @arr; print "\tunshifting:\n" and unshift @arr, 'zero'; print "\tclearing:\n" and @arr=();#undef @arr and @arr=undef see +m no good..

Or can i avoid all the repeted code in this other way?

#!perl use strict; use warnings; sub trigger{print "Triggered [@_]\n"} #must go BEFORE the eval is seen {#bare block because package BLOCK appeared only 5.14 package Arraytrigger; use Tie::Array; use vars qw(@ISA); @ISA = ('Tie::StdArray'); map { eval "sub $_ { &main::trigger; my \$self = shift; \$self->SUPER::$_(\$self, \@_); } " } qw(STORE CLEAR PUSH POP SHIFT UNSHIFT); } #package main.. tie my @arr, 'Arraytrigger'; print "\tSetting list:\n" and @arr = qw(a b c d e); print "\tSetting one:\n" and $arr[0]=0; print "\tpushing:\n" and push @arr,'f'; print "\tpopping:\n" and pop @arr; print "\tshifting:\n" and shift @arr; print "\tunshifting:\n" and unshift @arr, 'zero'; print "\tclearing:\n" and @arr=();#undef @arr and @arr=undef see +m no good..


Thanks
L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to 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.