When i have had to do this its looked much like you have posted. However as others have pointed out you will have issues with using ref() for this task. And unfortunately reftype() isn't a complete solution to the problem as 'Regexp' is unfortunately not treated as a native Perl type but rather as a blessed scalar reference. Thus reftype(qr/./) will return SCALAR not "Regexp".

In Data::Dump::Streamer I provide some utility XS methods that make this job a little easier. Data::Dump::Streamer::reftype() is similar to that provided by Scalar::Util except it returns PL_no (false) instead of undef which means you don't have to bother with a checking if the response is defined. A simple (if reftype($foo) eq 'SCALAR') will not raise a warning.

A second utility function of interest is Data::Dump::Streamer::regex() which in list context returns a list containing the regex pattern (without quoting or '(?:)' wrapper) and the modifiers on the pattern, and in scalar context returns the same as a stringified qr// would. This routine can not be fooled by blessing the qr// or by overloading its stringification logic (which says to me that regex's are native types distinct from normal SCALAR's and that they should receive a unique reftype designation)

So the logic would look something like this:

use Data::Dump::Streamer qw(reftype regex); my $type=reftype($obj); if ($type eq 'SCALAR' and my $re=regex($obj)) { # Its a regex, $re contains the unquoted pattern } elsif ($type eq 'REF' or $type eq 'SCALAR' or $type eq 'GLOB') { # its something that can be derefed by $$obj } elsif ( $type eq 'HASH' ) # its a hash } elsif ( $type eq 'ARRAY' ) # its an array } elsif ( $type eq 'CODE' ) # its code ref } elsif ( $type eq 'FORMAT' ) # its a format ref } elsif ( $type) { warn "Unhandled type: '$type'\n"; } else { # its not a reference at all }

For my uses I never found a dispatch table to be worthwhile. I think the overhead of doing the lookup and dispatch is more than doing the string comparisons in this case. For a larger dispatch table it might be worth while tho. You'd have to benchmark to see.

---
$world=~s/war/peace/g


In reply to Re: Behaving appropriately based on ref() result by demerphq
in thread Behaving appropriately based on ref() result by qbxk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.