It looks like you are using Req_Trace as a setter/getter, and that Req_Trace should only be set to a hash ref if set at all. In the above code, it looks like you are checking to see if its a hash ref after it's already been set, which IMO is the wrong place to be doing that. You should check to see if its a hash ref while it is being set, and in the above code, you would/may only need to check to see if it is set at all.
That being said, to check to see if Req_Trace is passed a hash reference, some here would say to use UNIVERSAL::isa($rt_arg, 'HASH'), and some would say ref($rt_arg) eq 'HASH' is fine. Either way is 'broken' in one way or another:IMO, if you don't want to accept a blessed object which happens to be implemented as a hash ref (which is fine IMO), then ref is fine (I just want a hash ref, I don't want no damn stinkin' objects! :-) (update: oops, but it also accepts an object that thinks its a hash :). If you want to make absolutely sure that the argument passed is a hash reference, blessed or not, then you use Scalar::Util::reftype(). If you think people deserve what they get if they bless a non-hash reference into a 'HASH' package (I would lean this way also), then UNIVERSAL::isa is fine too.my $aref = [ 1..5 ]; bless $aref, 'HASH'; # An array ref now thinks it's a hash ref print "aref is a HASH\n" if ref($aref) eq 'HASH'; print "aref is a HASH\n" if UNIVERSAL::isa($aref, 'HASH'); my $href = { 0..9 }; # This is ok print "href is a hash\n" if ref($href) eq 'HASH'; bless $href, 'Some::Package'; # This may not be ok print "href is not a hash\n" unless ref($href) eq 'HASH';
In reply to Re: Re: Is "ref $date eq 'ARRAY'" wrong?
by runrig
in thread Is "ref $date eq 'ARRAY'" wrong?
by bronto
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |