On Mon, Aug 15, 2005 at 05:58:23PM +0200, S?bastien Aperghis-Tramoni wrote:
>     use strict;
>     use Test::More tests => 2;
>     use Test::Exception;
>     use Net::Pcap;
> 
>     throws_ok(
>         sub { Net::Pcap::lookupdev() },
>         '/^Usage: Net::Pcap::lookupdev\(err\)/',
>         "calling lookupdev() with no argument"
>     );
> 
>     throws_ok
>         { Net::Pcap::lookupdev() }
>         '/^Usage: Net::Pcap::lookupdev\(err\)/',
>         "calling lookupdev() with no argument"
>     ;
> Now, if I move the "use Test::Exception" inside an eval-string and 
> execute the new script:
> 
>     $ perl -W exception.pl
>     1..2
>     ok 1 - calling lookupdev() with no argument
>     Usage: Net::Pcap::lookupdev(err) at exception.pl line 13.
>     # Looks like you planned 2 tests but only ran 1.
>     # Looks like your test died just after 1.
> 
> Aha! The first test, which uses the normal form of throws_ok() passes, 
> but the second one, which uses the grep-like form, fails.

The throw_ok { ... } syntax only works because the throw_ok sub exists
and has a prototype that specifies a subref is expected; if you don't
load Test::Exception by the time the throw_ok call is compiled, it
is parsed as an indirect object call of the "throw_ok" method on the
object or class returned by the {} block:

$ perl -MO=Deparse,-p -we'throws_ok {  Net::Pcap::lookupdev() } "/^Usage: 
Net::Pcap::lookupdev\(err\)/", "calling lookupdev() with no argument"'
BEGIN { $^W = 1; }
do {
    Net::Pcap::lookupdev()
}->throws_ok('/^Usage: Net::Pcap::lookupdev(err)/', 'calling lookupdev() with no
 argument');
-e syntax OK

which is perfectly valid perl, but unlikely to do what you want.

Reply via email to