vx has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

Is there an way to check if an var contains packed data ?

Something like :

if (is_packed($data)) { $data = unpack(...); }

Thanks

Replies are listed 'Best First'.
Re: Detecting packed data
by dave_the_m (Monsignor) on Apr 10, 2005 at 16:12 UTC
    Is there an way to check if an var contains packed data
    No, not in general. It all depends on the nature of the packing.

    Dave.

Re: Detecting packed data
by spurperl (Priest) on Apr 10, 2005 at 17:24 UTC
    In the general case the answer is no. Intuitively, think about:

    $a = 17;
    Is $a packed ? Under the hood, in its binary representation it's probably just-another-integer that "looks packed".

    Though in general you can't say, don't despair. Your case can be more specific... Maybe there's something special about *your* data that will make it stand out ?

      You are wrong about what it looks like under the hood in Perl. You would be right in C. (The difference being that in Perl every scalar has a ton of structure associated with it.)
Re: Detecting packed data
by gam3 (Curate) on Apr 10, 2005 at 17:48 UTC
    While there can not be a is_packed function you can write a is_not_string function.
    sub is_not_string { my $a = shift; my $packed; for (split(//,$a)) { $packed = 1 if (ord($_) == 0); $packed = 1 if (ord($_) > 127); } return $packed; }
    Unicode might make the much harder. Update: did not think of "\n" and "\t". Thanks Zaxo
    -- gam3
    A picture is worth a thousand words, but takes 200K.

      Your function might be replaced by matching the character class for control characters, /\p{IsC}/ (unicode character class, just for fun). That doesn't have anything to do with whether a string contains packed data.

      Neither does it have anything do do with whether a scalar is a string. Many text strings contain "\n" or "\t".

      After Compline,
      Zaxo