Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^5: Reference assessment techniques and how they fail (run?)

by tye (Cardinal)
on Feb 18, 2008 at 04:02 UTC ( #668471=note: print w/ replies, xml ) Need Help??


in reply to Re^4: Reference assessment techniques and how they fail
in thread Reference assessment techniques and how they fail

Just a quick note that I tested some form of eval-style test and was pleasantly surprised to find that no overloaded tied methods were actually invoked by the test (as I recall). So my preference is an eval-style test that doesn't run any user code. When such is indeed possible, then it wins (or ties) at both criteria. Unfortunately, I don't have the exact check code nor test handy and I won't be likely to dive into the details of that in the immediate future, it appears. But coming up with eval-style tests that won't emit warnings and demonstrating that they don't call user code (or how little user code they call) would be useful results (hopefully from this thread).

When such isn't possible, then I wouldn't want to use your preferred test unless it came rolled up in a core module such as overload (or, next best, Scalar::Util) so that the next time this stuff changes (or a problem is found) the test is likely to get updated. Of course, that still leaves the problem of using it in a backward-compatible way. For the optimists in the crowd, that reduces to just requiring the install of an updated overload module. But getting something a ton better than what Scalar::Util has managed to provide is long overdue and would certainly be welcomed by me. :)

Updated: to note that I was testing tied items, not overloaded.

- tye        


Comment on Re^5: Reference assessment techniques and how they fail (run?)
Re^6: Reference assessment techniques and how they fail (run?)
by kyle (Abbot) on Feb 18, 2008 at 04:27 UTC

    When I tested eval, it did invoke overload methods. Here's the test (see also Re^4: Reference assessment techniques and how they fail for a longer version for code refs):

    use strict; use warnings; use Test::More 'tests' => 4; use Scalar::Util qw( blessed reftype ); my $side_effect = 0; package OverloadHash; use overload '%{}' => sub { $side_effect = 1; {} }; package main; sub is_hash_eval { no warnings qw( void uninitialized ); return '' ne ref $_[0] && eval { %{$_[0]}; 1 }; } sub is_hash_util { my $suspected_hash = shift; return 0 if '' eq ref $suspected_hash; return 1 if 'HASH' eq reftype $suspected_hash; if ( blessed $suspected_hash && overload::Method( $suspected_hash, '%{}' ) ) { return 1; } return 0; } check_method( \&is_hash_util, 'utils' ); check_method( \&is_hash_eval, 'eval' ); sub check_method { my ( $tester, $name ) = @_; $side_effect = 0; my $overhash = bless {}, 'OverloadHash'; ok( $tester->( $overhash ), "$name: overloaded reference" ); # this test fails for the eval method ok( ! $side_effect, "$name: no side effect" ); }

    I agree that the best solution to all this would be something sweet in Scalar::Util. I trust it to stay up to date, and it's the right place for it.

Re^6: Reference assessment techniques and how they fail (run?)
by kyle (Abbot) on Feb 18, 2008 at 15:56 UTC

    Here's a test for side effects of an eval test on a tied array.

    It seems the reason nothing is called during the test is that it's happening in void context. If I give the array dereference a context in the eval, it has to make a call to the object.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://668471]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2014-12-25 01:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (159 votes), past polls