develooper Front page | perl.perl5.porters | Postings from August 2001

Re: 'can' with undefined subs

Thread Previous | Thread Next
From:
Tony Bowden
Date:
August 25, 2001 12:06
Subject:
Re: 'can' with undefined subs
Message ID:
20010825145817.A11788@soto.kasei.com
On Sat, Aug 18, 2001 at 05:33:01PM +0100, Hugo van der Sanden wrote:
> I'd guess comp/decl.t and/or comp/proto.t. Be a bit careful though -
> since comp/* tests appear so early in the test suite, it is preferable
> to avoid using complex code.

Actually, I decided to test it in op/universal, as that seems to be what
is testing 'can'.

I also added a test for 'can' returning a coderef, as that seemed to be 
missing.

Tony

------------------

--- t/op/universal.t.11710      Sat Aug 25 14:40:57 2001
+++ t/op/universal.t    Sat Aug 25 14:51:50 2001
@@ -24,7 +24,8 @@

 package Alice;
 @ISA=qw(Bob Female);
-sub drink {}
+sub sing;
+sub drink { return "drinking " . $_[1]  }
 sub new { bless {} }

 $Alice::VERSION = 2.718;
@@ -44,8 +45,9 @@

 package main;

-my $i = 2;
-sub test { print "not " unless shift; print "ok $i\n"; $i++; }
+{ my $i = 2;
+  sub test { print "not " unless shift; print "ok $i\n"; $i++; }
+}

 $a = new Alice;

@@ -61,11 +63,13 @@

 test ! $a->isa('Programmer');

-test $a->can("drink");
-
 test $a->can("eat");
-
 test ! $a->can("sleep");
+test my $ref = $a->can("drink");      # returns a coderef
+test $ref->("tea") eq "drinking tea"; # ... which works
+test $ref = $a->can("sing");
+eval { $a->sing };
+test $@; # Calling a stubbed          # ... unless no actual subroutine

 test (!Cedric->isa('Programmer'));

------------------

-- 
--------------------------------------------------------------------------
 Tony Bowden | tony@tmtm.com | http://www.tmtm.com/
                          if more people were screaming then I could relax
--------------------------------------------------------------------------


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About