perl-diddler has asked for the wisdom of the Perl Monks concerning the following question:
What does it mean to "autovivify" a function -- i.e. how would one use such functionality (or is anyone using such?). If you believe autovivifying a function is a useful feature, then shouldn't you be able to use "exists" to see if the function actually exists yet (if the function has been initialized with a value). Currently, "exists()" only works on arrays and hashes. How would you code it?
If you allow exists to work with a function or method call, then shouldn't you also allow exists to work with autovivified scalars? It would mean the same thing as for array or hashes: returns true if the specified scalar has ever been initialized & the scalar (or function) is not autovivified if it doesn't exist.
Maybe the parser would be smart enough to figure things out, but it seems the construct "$a=exists &b", would normally be thought to invoke "b" -- not what is wanted. So would you need to encode the function name in a string, ala: "$a=exists '$b'"? What about scalars -- same problem? Or would the parser figure out scalars the same way it does now with array and hash refs ("exists $hash{$key}").
It might mean (if I understand), that "exists $b" tests if "$b" has ever been assigned a value. What'cha think? Here's the code demonstrating the problems (3 cases, 4 if you count the "exists" issue).
Linda
#!/usr/bin/perl -w #use strict; # can optionally enable to show no "unstrict" construc +ts ## ## 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":"unprin +t\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"?
p.s. -- shouldn't "code" sections be displayed with a fixed font? Seems like my columns don't line up in the code above when I preview it...*sigh*
|
|---|