Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

I don't see how a tied hash (ref) is a special case.

use strict; use warnings; package Nothing; sub isa { 0 } package NotHash; use overload '%{}' => sub { {} }; sub new { bless [], shift } package OverHash; use overload '%{}' => sub { {} }; sub new { bless {}, shift } package main; use Tie::Memoize; use Test::More; my @test_cases = ( { name => 'unblessed hash', test => {}, is_hash => 1, }, { name => 'unblessed array', test => [], is_hash => 0, }, { name => q{hash blessed as 'HASH'}, test => bless( {}, 'HASH' ), is_hash => 1, }, { name => q{array blessed as 'HASH'}, test => bless( [], 'HASH' ), is_hash => 0, }, { name => q{hash blessed as 'ARRAY'}, test => bless( {}, 'ARRAY' ), is_hash => 1, }, { name => 'hash in package 0', test => bless( {}, '0' ), is_hash => 1, }, { name => 'array in package 0', test => bless( [], '0' ), is_hash => 0, }, { name => 'hash with ->isa overridden', test => bless( {}, 'Nothing' ), is_hash => 1, }, { name => 'blessed array with %{} overloaded', test => NotHash->new(), is_hash => 1, }, { name => 'blessed hash with %{} overloaded', test => OverHash->new(), is_hash => 1, }, { name => 'tied hash', test => get_tied_hash(), is_hash => 1, }, { name => 'not a reference', test => 'HASH', is_hash => 0, }, ); sub get_tied_hash { tie my %h, 'Tie::Memoize', sub {}; return \%h; } plan 'tests' => scalar @test_cases; foreach my $test ( @test_cases ) { is( !!isHash( $test->{test} ), !!$test->{is_hash}, $test->{name}); } use Scalar::Util qw( reftype blessed ); sub isHash { 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; }

The code for isHash is based heavily on blokhead's from "Is it a hashref" vs "Can I use it like a hashref?"

Have I missed an important test case here?


In reply to Re^2: Reference assessment techniques and how they fail by kyle
in thread Reference assessment techniques and how they fail by kyle

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (8)
    As of 2014-04-19 23:55 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (485 votes), past polls