I figured out the bug in perl that I mentioned a while back regarding OO-methods not being called when I expected them to. They also raise an interesting question about desirable behavior. The question about behavior first, followed by the code showing the bug(s) & probs.

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*


In reply to OO-call bug uncovered & autovivified functions: defined? exists? by perl-diddler

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.