in reply to Being more Assert-ive with Perl

I want to be sure that if I am expecting you to pass a hash-ref, that I will never accept an array-ref in its place.

Here's one that will make your brane hurt ...

my $arrayref = [qw(i like pie)]; bless($arrayref, 'HASH');
If I pass $arrayref to a sub that expects a hashref, how will you tell that it is not what you expect?

Dealing with this correctly is on my to-do list for Data::Compare. It will probably involve lots of eval evil.

Replies are listed 'Best First'.
Re^2: Being more Assert-ive with Perl
by stvn (Monsignor) on Sep 17, 2004 at 16:38 UTC

    Yeah, this is a hard one, but IMO, if you do such things, you should expect programs to die horrible deaths because of it, and the blame falls on your code not the modules parameter checking code.

    This is one of the important points of Design By Contract (and all contracts for that matter), you must abide by the provisions of the contract, and if you try to subvert the contract, there should be reprocusions.

    -stvn
Re^2: Being more Assert-ive with Perl
by antirice (Priest) on Sep 18, 2004 at 02:10 UTC

    Perhaps instead of using ref you should check out Scalar::Util's reftype?

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

      The only real solution to this problem is to see if you can use something as the type of ref you want:
      sub hashref { eval { \%{$_[0]} } } sub myfunc { hashref($_[0]) or die "myfunc expected a hashref"; }
      reftype won't handle all cases (e.g. deref-overloading or perl4-style glob passing).