in reply to enabling OO and non-OO access to the same module using a hash reference

I suppose my first thought was that foo( bar => { hoo => hah } ) would be 3 parameters (which would not coerce back) when I now realise it is 2, with the second being a hash reference to the (anonymous) nested hash.

But the problem I am actually having with providing support for a coerced hash (or even an ordered list of parameters for that matter) is this:

my $oref = OtherModule -> new(); Package::method( $oref, 'gotcha' );
Just to make it clearer we'll say OtherModule is a subclass of Package. So the problem is that testing for OO-access in Package::method will go false-positive if the above non-oo construction is used. Requiring all non-OO users to use a hash reference for their parameters makes it (a) easier to test access method and (b) code applying to the object reference that normally arrives will work compatibly at once if a hash reference is passed non-oo intead. So this is why I am hoping I can go that route. For example, perlbot even suggests testing ref($_[0]) as an alternative to the forbidden (ref($_[0]) eq 'ClassName'). But that would prevent some trivial methods from having say a file handle as parameter owing to the same false-positive result I am trying to avoid. And I don;t want to have to test all possible references that aren't to some class! (update: because a class is not allowed to make assumptions about subclasses so that would be what had to be tested for a completely generic test for oo-ness)

-M

Free your mind

  • Comment on Re: enabling OO and non-OO access to the same module using a hash reference
  • Download Code

Replies are listed 'Best First'.
Re^2: enabling OO and non-OO access to the same module using a hash reference
by ysth (Canon) on Jan 16, 2007 at 01:10 UTC
Re^2: enabling OO and non-OO access to the same module using a hash reference
by ikegami (Patriarch) on Jan 16, 2007 at 04:05 UTC
    ref($_[0]) works if you use named parameters, since the first arg is either an object or a parameter name.