in reply to Action at a great distance

Sorry, but I don't see why (on its own) this is a bad idea. If @callbacks is under your complete control and you know that all your callback subs behave according to some set of rules then I see little wrong with it. If however @callbacks is not under your control then you have a different issue. At that point I would recommend a series of checks:

foreach my $callback (@callbacks) { (ref($callback) eq "CODE") || die "$callback is not a code ref"; my @temp_args = @{[@args]}; $callback->(@temp_args); if (verify(@temp_args)) { @args = @temp_args; } else { die "callback did something funky to args"; } }
I'm sorry, but given the amount of information you have supplied here, I don't really see what you are trying to get at. Callbacks are a common enough idiom, but when used loosly they can be dangerous, but if you really wanna go there then you could say that plain old assignment (=) is dangerous.

Please provide a bit more information on the code, and how things like @callbacks and @args get populated, along with maybe a bit more context as to where this all gets executed.

-stvn

Replies are listed 'Best First'.
Re^2: Action at a great distance
by Aristotle (Chancellor) on Mar 20, 2004 at 08:07 UTC
    You're trying too hard, IMO.
    my @copy_args; foreach( @callback ) { local $_ = $_; $_->( @copy_args = @args ); }

    Makeshifts last the longest.

      Maybe, but TIMTOWTDI. Your code solves the problem for sure, my code assumes that everyone is out to get me, and that nothing is safe. But if you read my post, I suggested that code if the sub refs in @callbacks we "untrusted", not a just a solution to the problem. But again, TIMTOWTDI, and my way is certainly the more paranoid way.

      - stvn
        Considering it is impossible for the callbacks to tamper with their caller's data using my solution, I don't see why one needs to try even harder. Unless you don't trust yourself, which is why we use strictures and friends. However, that's an entirely different issue, not paranoia. Perl is not a language for the paranoid, anyway.

        Makeshifts last the longest.

Re^2: Action at a great distance
by Aristotle (Chancellor) on Mar 20, 2004 at 17:30 UTC
    Btw, this is superflous:
    my @temp_args = @{[@args]};
    You are making a copy of a copy of the original. Semantically it is the same as
    my @temp_args = @args;

    Makeshifts last the longest.

      Aristotle,

      Your right, I am making an assumption of a single level structure and that would be copied-by-value anyway. And considering that I do this:

          if (verify(@temp_args)) {         @args = @temp_args;     }     else {         die "callback did something funky to args";     }
      Then actually doing a proper deep-copy of @args would only make sense if you planned to catch the exception being thrown in the else block. Basically, my intention was to build a sandbox for the callbacks to run in with commit and rollback functionality. A real implementation would require much more knowledge of the data in @args and the functionality of the app as a whole.

      -stvn