adriani has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I'm trying to write some test code for the subroutine ScanDeps however, I'm seeing some odd behaviour that I can't explain. A cut-down version of my test code looks like this:
sub generic_rv_test { my $rv = shift; foreach my $key (keys %$rv) { if (exists($rv->{$key}{used_by})) { # test used_by ... } else { # fail test } } } my $rv = scan_deps( files => ['t/data/pluggable/Foo.pm'], recurse => 1, ); generic_rv_test($rv);
However, my test was failing so I added the following lines after generic_rv_test($rv); to start debugging the issue:
use Data::Dump qw(dump); print dump $rv;
However, adding these lines somehow caused my test to start working! What seems to be happening is that $rv is more complete with the dump lines than without. Can someone explain what is happening? Basically, I want to be able to ensure $rv is contains all it's information before calling the test function on it but without having to dump $rv ...

$rv without the dump lines:

{ "Pluggable.pm" => { file => "C:/Perl/site/lib/Module/Pluggable.pm", key => "Module/Pluggable.pm", type => "module", }, }
$rv with the dump lines:
{ "Pluggable.pm" => { file => "C:/Perl/site/lib/Module/Pluggable.pm", key => "Module/Pluggable.pm", type => "module", used_by => ["Foo.pm"], }, }
Thanks, AdrianI

Replies are listed 'Best First'.
Re: Problem: Lazy Hash Evaluation
by diotalevi (Canon) on Feb 25, 2007 at 00:28 UTC

    If the problem is that the value used_by is only present when you dump, there's clearly a missing piece here. There's nothing about perl to magically invent this used_by value. You ought to show your cards.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      > If the problem is that the value used_by is only present when you dump
      That's exactly the problem.

      > There's nothing about perl to magically invent this used_by value
      I was expecting it to be produced as part of the output of scan_deps() and the subrountine generic_rv_test() is therefore testing that it was produced.

      The only thing that I can think of that might explain this is that the data stucture $rv hasn't been completely filled out, as a result of scan_deps(), before it's used by generic_rv_test() except in the case when I dump $rv. However, that strikes me as being unlikely.

      > You ought to show your cards.
      I assume you mean give you the full code so here it is:

      The test script (4-pluggable_fake.t)"

      NB if I uncomment either of the two groups of comments in the above I get the used_by => ["Foo.pm"], line I'm testing for and all is OK.

      The complete generic_rv_test subrountine:

      And finally, the subroutine scan_deps comes from the CPAN module Module::ScanDeps.

      Thanks for the help,
      Adrian

Re: Problem: Lazy Hash Evaluation
by blazar (Canon) on Feb 24, 2007 at 22:40 UTC
    Hello, I'm trying to write some test code for the subroutine ScanDeps however, I'm seeing some odd behaviour that I can't explain. A cut-down version of my test code looks like this:

    I have some trouble understanding what your actual problem may be: indeed you did good to prepare a cut-down version of you code, but you should go as far as making that into a minimal but complete example exhibiting the problem. You should specify what output you expect and what you get instead.

    use Data::Dump qw(dump); print dump $rv;

    Data::Dumper exports Dumper() and AFAICT knows no dump():

    C:\temp>perl -MData::Dumper=dump -e "" "dump" is not exported by the Data::Dumper module Can't continue after import errors at -e line 0 BEGIN failed--compilation aborted.
    However, adding these lines somehow caused my test to start working!

    However, you failed to explain how it was failing to work before you added them.

    $rv without the dump lines:
    [snip]
    $rv with the dump lines:
    [snip]

    Very hard to believe...

    Update: misread Data::Dumper for Data::Dump, thanks to rhesa for pointing out.