Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by demerphq (Chancellor)
on Jan 16, 2003 at 20:10 UTC ( [id://227476]=note: print w/replies, xml ) Need Help??


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

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....

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Context aware functions - best practices?
by MarkM (Curate) on Jan 16, 2003 at 20:18 UTC

    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....

Re: Re: Re: Re: Re: Context aware functions - best practices?
by BrowserUk (Patriarch) on Jan 16, 2003 at 20:39 UTC

    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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://227476]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-20 02:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found