Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Autovivification for dummies (using Test::More)

by tphyahoo (Vicar)
on Jul 28, 2005 at 18:51 UTC ( #479087=perlmeditation: print w/ replies, xml ) Need Help??

Here's a little snip I put together messing around with autovivification. Perhaps some monks that feel queasy about this topic may find it useful.

For a more in depth tutorial on the topic The Bad, the Ugly, and the Good of autovivification may be of interest. Cheers :)

use strict; use warnings; use Test::More qw(no_plan); use Data::Dumper; my %hash = (a => 'a', b => 'b'); ok( ! exists($hash{c}), "hash -- c doesn't exist"); ok( ! exists($hash{c}), "hash -- c still doesn't exist"); ok( ! defined($hash{c}), "hash -- c not defined either"); my $hashref = { %hash }; ok( ! exists($hashref->{c}), "hashref -- c doesn't exist"); ok( ! exists($hashref->{c}), "hashref -- c still doesn't exist"); ok( ! exists( $hashref->{c}->{d} ), "hashref, deeper -- c->{d} doesn't + exist"); ok( ! exists( $hashref->{c}->{d} ), "hashref -- c->{d} still doesn't e +xist"); ok( ! exists($hashref->{c}), "hashref -- c still doesn't exist"); #fai +ls. c does exist. ok( ! defined($hashref->{c}), "hashref -- c still doesn't exist"); #fa +ils. c is an empty hashfref, which is something other than undef.

Comment on Autovivification for dummies (using Test::More)
Download Code
Re: Autovivification for dummies (using Test::More)
by revdiablo (Prior) on Jul 29, 2005 at 06:45 UTC

    I'm not quite sure why you arranged your last two tests to fail. It's my understanding that test suites are intended to test the promises made by a certain interface, usually described in documentation. In this case, autovivification is a documented and expected behavior. Why should that cause the tests to fail?

      Good question.

      Here, I was using Test::Morefor rhetorical and didactical purposes: in other words, I was trying to make a point. My point was, to illustrate how the "naive" expectation of dereferencing is contradicted by the reality of how autovivification works. I didn't mean that autovivification is a bug; I understand that this behavior is there for a purpose, controversial though it may be.

      Your question made me reflect a bit, and realize that I have lately been incorporating Test::More into my perlmonks posts, in two ways.

      1. In SOPW posts, I am showing the behavior I want, but am not getting, with test more failing.
      2. In Meditations, I am using test more rhetorical, as here, to make points.
      I guess I am also hoping to spread the testing gospel in this way, as well as my own test fu.
        My point was to illustrate how the "naive" expectation of dereferencing is contradicted by the reality of how autovivification works.

        Ok, that is a perfectly reasonable explanation. It might have been nice to say that in the original post, but I know how hard it can be to figure out the things people will have questions or confusion about.

        I have lately been incorporating Test::More into my perlmonks posts

        I like this idea. One of the reasons I like tests is they can convey a lot of information in a convenient, machine-usable little packet. They are just like the test snippets I usually end up writing, but in addition they show where my expectations are violated or upheld.

        Understood, but I think people would find it clearer if you negated your test so that they all past, rather than posting tests which are meant to fail.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://479087]
Approved by tye
Front-paged by sparkyichi
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2014-07-29 19:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (226 votes), past polls