in reply to Re: Is "ref $date eq 'ARRAY'" wrong?
in thread Is "ref $date eq 'ARRAY'" wrong?

When you check ref() you impose a no-blessings and no-subclassing requirement which really isn't all that friendly or perlish.

I don't agree.

First off, you don't really "impose a no-blessings and no-subclassing requirement"; you just decide that you won't be responsible for it. There is nothing preventing a user of your code from writing something like

my $original_package = ref $object; bless $object, 'ARRAY'; call_sub_that_expects_array_ref( $object ); bless $object, $original_package;

I think it's probably more prudent to let your user take responsibility for treating his $object like an array reference. You can be permissive, but you might make it hard to catch some subtle bugs and you may be encouraging maintenance problems. Besides, the practical benefits are likely to be very few.

Edit: Minor grammatical fixes.

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re: Re: Re: Is "ref $date eq 'ARRAY'" wrong?
by diotalevi (Canon) on Dec 21, 2003 at 04:10 UTC
    I have never before considered that blessing into ARRAY, HASH, and friends were anything outside of a pathological case. I mean, are you suggesting that someone should say that just to be depantic (which I'll understand) or that it is actually reasonable?
      I mean, are you suggesting that someone should say that just to be depantic (which I'll understand) or that it is actually reasonable?

      It is much more reasonable to make you, the user of Module::X, decide explicitly that it should mess with the internals of an object of Some::Random::Class rather than expect Module::X to just go ahead and do it because Some::Random::Class happens to be implemented using the right type of reference as its blessed thingy.

      The author of Module::X should be kind enough to respect that, if ref() reports $parameter to be blessed into Some::Random::Class, then its implementation is off limits. A blessed thingy is a blessed thingy regardless of what type of reference it happens to be. If you want to take the responsibility of relying on Some::Random::Class's implementation, then you can do it... by reblessing it.

      If you want targets painted on your boots, do it yourself; don't expect someone else to do it for you.

      P.S. If you write modules that would make use of such a misfeature in Module::X, you are almost certainly making poor design decisions.

      P.P.S. Setting aside the flagrant disregard of the principles of encapsulation that it would entail, why should Module::X be riddled with sticky parameter checking that would probably only be useful in rare cases anyway? I'm not a fan of bloat. The additional test cases alone would be reason enough to avoid it. As I said elsewhere in this thread, keeping things simple is a sound approach. This suggestion is neither simple nor sound.

      -sauoq
      "My two cents aren't worth a dime.";