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

In the years since I've written this, UNIVERSAL::isa has been written. Use that instead.


No, you'd have to say it as isa( $date, 'ARRAY' ). The documentation for UNIVERSAL says to import isa() before using it so here's the proper way. By using isa() you've just allowed your users to bless their arrays before giving them to you. When you check ref() you impose a no-blessings and no-subclassing requirement which really isn't all that friendly or perlish.

*isa = \&UNIVERSAL::isa; sub foo { my $date = shift; return unless isa( $date, 'ARRAY' ); bar( $date ); }

Replies are listed 'Best First'.
Re: Is "ref $date eq 'ARRAY'" wrong?
by Abigail-II (Bishop) on Dec 19, 2003 at 16:36 UTC
    By using isa() you've just allowed your users to bless their arrays before giving them to you.
    Is that a good thing? You are suddenly going to accept object and depend on its internals. Not touching the internals of foreign objects (what the original code is doing) is IMO a good thing.

    Abigail

      Sure it is. You advertised that you accept array references it shouldn't matter to you what someone else thinks of that array. If someone else has an object and they think that its fine for someone else to treat their object as a plain array then it isn't a problem for the isa-using function author to do so.
Re: Re: Is "ref $date eq 'ARRAY'" wrong?
by sauoq (Abbot) on Dec 20, 2003 at 04:20 UTC
    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.";
    
      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.";
        
Re: Re: Is "ref $date eq 'ARRAY'" wrong?
by jmanning2k (Pilgrim) on Dec 22, 2003 at 14:55 UTC
    Actually, if you read the docs carefully, they say if you really want to use a local function (merely to save yourself some typing?), then you can alias it as you describe (since you can't import it using Exporter).

    However, it's perfectly acceptable to call it as UNIVERSAL::isa. It's not necessary to do that aliasing stuff.

    ~J