From: Tony Bowden Date: Sat, 25 Aug 2001 14:58:17 +0000 (+0100) Subject: Re: 'can' with undefined subs X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=39d11b7fff60fb4dbe9c17fee36ce5399b4376d8;p=p5sagit%2Fp5-mst-13.2.git Re: 'can' with undefined subs Message-Id: <20010825145817.A11788@soto.kasei.com> (Applied with minor modifications.) p4raw-id: //depot/perl@11752 --- diff --git a/t/op/universal.t b/t/op/universal.t index 23c616c..b6596a3 100755 --- a/t/op/universal.t +++ b/t/op/universal.t @@ -24,7 +24,8 @@ package Female; 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 @@ $Alice::VERSION = 2.718; 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("Male"); 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 $a->$ref("tea") eq "drinking tea"; # ... which works +test $ref = $a->can("sing"); +eval { $a->sing }; +test $@; # ... but not if no actual subroutine test (!Cedric->isa('Programmer'));