Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Re: Re: Context aware functions - best practices?

by BrowserUk (Pope)
on Jan 16, 2003 at 19:45 UTC ( #227464=note: print w/ replies, xml ) Need Help??


in reply to Re: Re: Context aware functions - best practices?
in thread Context aware functions - best practices?

I'll bite:) What should I do if I write a sub that is expecting an array ref and I get passed something other than an array ref?


Examine what is said, not who speaks.

The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.


Comment on Re: Re: Re: Context aware functions - best practices?
Re: Re: Re: Re: Context aware functions - best practices?
by demerphq (Chancellor) on Jan 16, 2003 at 20:10 UTC
    Well theres three possibilities.... First one is just let Perl throw an exception when it tries to use the value in the wrong way. Second is to replace the test code with something that checks that the item ISA correct type, instead of checking if the type is ARRAY.
    UNIVERSAL::isa($array_ref,"ARRAY") or die "Can't use anything but an array";
    However this is still not an exhaustive check as it will fail if some moron does
    my $array_ref=bless {},"ARRAY";
    Which is a case where afaik only perl itself can tell the difference.

    The third option is I guess the most paranoid and would be something like:

    eval{ ref($arrayref) and @$arrayref ? 1 : 1 } or die "Must have a reference to something that behaves like an ARRA +Y.\n". "Failed to coerce '$arrayref': $@";
    So personally I would just let my code choke and have the end user track the problem down. (This assumes that I have already sufficient regression test to ensure that my code is not in error in the first place ;-)

    --- demerphq
    my friends call me, usually because I'm late....

      Perl 5.8.0 comes with Scalar::Util. One of the subroutines provided by this module is reftype(). reftype() is superior to UNIVERSAL::isa() in this context. For blessed or non-blessed array references, reftype() will always return 'ARRAY'.

      Personally, I plan to use Scalar::Util::reftype() quite a bit in cases where I used to use UNIVERSAL::isa().

        Yup. Thats a good solution too. :-)

        Too bad it wasnt standard kit earlier on. In fact, too bad that reftype wasnt accepted as a pure perl keyword. Likewise for most of the modules contents.

        --- demerphq
        my friends call me, usually because I'm late....

      Okay. So am I right in thinking that the objection only really applies if the sub in question is a method of a class and therefore break when called via the subclass?


      Examine what is said, not who speaks.

      The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

        Im not sure which objection you mean exactly. But my original point applies to both OO and classical-imperative programming. Basically its "dont try to out-smart your user". Its usually unnecessary given the nature of Perl, and often a PITA to work around when you find out that you the user is smarter than the original programmer anyway.

        :-)

        --- demerphq
        my friends call me, usually because I'm late....

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2014-11-29 01:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (203 votes), past polls