* On Fri, May 02 2008, Eirik Berg Hanssen wrote: > nadim khemir <[EMAIL PROTECTED]> writes: > >> On Friday 02 May 2008 01.24.00 Eirik Berg Hanssen wrote: >>> use Test::More tests => 1; >>> use Test::Trap; >>> trap { warn "1\n2\n3" }; >>> $trap->warn_like(0, qr/1\n2\n3/); >> >> Ah! I like this. How did I miss this module?? > > Perhaps I just haven't been _quite_ shameless enough plugging it? ;-) > >> is it possible to have trap{} return an object/sub/whatever that is created >> by >> the trap{}. The automagic $trap is too automagic for me. > > Possible? Sure, if you wrap it up for that. This is pure Perl, > after all. :) > > Recommended? Not really. It kinda runs counter to the design. The > idea was for trap{}/$trap to mimic eval{}/$@; for one thing > list/scalar/void context is propagated into the block; for another, > $trap is a (package, but still) global, so you may localize it. > > It seems to me any alternative interface will be either overly, > un-perly verbose or else severely restricted. But, granted, the > severely restricted interface may be enough for you.
I think the most sane interface would be: my $trap = trap { ... }; is $trap->foo, 'foo'; is $trap->bar, 'bar'; You said you're trying to emulate $@, but $@ can be changed out from under you rather easily, so instead of: eval { foo() }; if($@){ error } The defensive programmer will write: my $result = eval { foo() }; if(!defined $result){ error } # use $@ for more details, if necessary. This gives you more flexibility anyway: my $a = eval { try_something }; my $b = eval { try_something_else }; if(!$a && !$b){ die 'both alternatives failed!' } Anyway, automatically setting variables should always be avoided, regardless of whether or not it is "perlish". I want correct tests, not "perlish" tests. Regards, Jonathan Rockway -- print just => another => perl => hacker => if $,=$"