#!/usr/bin/perl -w #use strict; # can optionally enable to show no "unstrict" constructs ## ## this prog demonstrates 3 related problems and a 4th example ## that may also be a problem; (all 4, likely same cause) ## See descriptive text below after __END__ ## { package F; our $count=-1; sub boom { ++$count; $count==0 ? "two\n": $count==1 ?"one\n":"unprint\n";} } { package KA; our @ISA = qw(F); ## PROBLEM #1 'can' works even when 'boom' can't be called ## PROBLEM #3b ( -- call to boom fails 2nd time through) ## called as valid method sub innocent{ if ($_[0]->can("boom")) {$_[0]->boom; }} #ln 18 # sub laytrap { { my @dummy=(\&boom); #ln 22 # (see comments below about 4th "concern") # my $boom_defined=$dummy[0]; #ln 24 # print "boom defined = " . defined $boom_defined; #ln 25 # print "; boom exists = " . exists $boom_defined; #ln 26 # print "\n"; #ln 27 } "zero:trap laid\n" } } package main; my $o = bless [],"KA"; ## These two calls (boom & innocent work... print $o->boom; print $o->innocent; ## lay trap to break boom & innocent print $o->laytrap; ## Now same two calls, boom & innocent, will fail: ## PROBLEM #2 -- can no longer call parent method 'boom' ## (using eval to catch error) eval {if ($o->can("boom")) {print $o->boom}}; #ln 43 $@ && print "$@"; ## PROBLEM #3 - within 'innocent', call to 'boom' fails ## this call terminates program (fatal Undefined error) print $o->innocent; __END__ The above prog demonstrates three (3) OO-call related problems. Perl autovivifies "\&boom" into "@dummy" in "sub laytrap" (ln 22, above). This *interferes* with OO calls through a blessed ref later on in program execution. The 4th "problem is the commented-out section above numbered lines "24-26" (in comments). _*If*_ you can autovivify functions or methods, then "exists()" should work with function references. Instead, if you uncomment lines 24-27, you will get an error: "exists argument is not a HASH or ARRAY element at ./pbug1 line 26." On the other hand, if you allow "exists" to work with coderefs, why not "simple vars"?