How to get the composite type information?

P

Peng Yu

ref is not capable to getting composite type information. In the
following example, I actually want to print that the type is array of
array (I would be better if it can print the first a few element in an
array). Is there a function for this in perl?

$ cat arrary_of_array.pl
#!/usr/bin/env perl

use strict;
use warnings;

my $aref=[
[1, 2, 3]
, [4, 5, 6]
, [7, 8, 9]
];
print ref($aref), "\n";

$ ./arrary_of_array.pl
ARRAY
 
C

Charlton Wilbur

PY> ref is not capable to getting composite type information. In the
PY> following example, I actually want to print that the type is
PY> array of array (I would be better if it can print the first a
PY> few element in an array).

That's because the Perl interpreter does not care about composite types.
There is nothing constraining an array to only contain only arrayrefs
except the programmer, so for the interpreter to call it an array of
arrayrefs would be inaccurate - the interpreter cannot tell if it is
intentionally an array of only arrayrefs, or it just happened that way
by accident.

PY> Is there a function for this in perl?

Not in core Perl. But it should be trivial for you to write one, if you
need it.

Charlton
 
P

Peng Yu

    PY> ref is not capable to getting composite type information. In the
    PY> following example, I actually want to print that the type is
    PY> array of array (I would be better if it can print the first a
    PY> few element in an array).

That's because the Perl interpreter does not care about composite types.
There is nothing constraining an array to only contain only arrayrefs
except the programmer, so for the interpreter to call it an array of
arrayrefs would be inaccurate - the interpreter cannot tell if it is
intentionally an array of only arrayrefs, or it just happened that way
by accident.

    PY> Is there a function for this in perl?

Not in core Perl.  But it should be trivial for you to write one, if you
need it.

Does any add-on package have this function? I don't know what you mean
by 'trivial'. But I think it would take some effort to make one
function to robustly handle arbitrarily complex cases.
 
C

Charlton Wilbur

PY> I don't know what you mean by 'trivial'. But I think it would
PY> take some effort to make one function to robustly handle
PY> arbitrarily complex cases.

"Trivial" means that once you have figured out what you want it to do,
it is largely a simple matter of typing the code; it should not require
much thought.

Charlton
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,193
Latest member
TopCryptoTaxSoftwares2024

Latest Threads

Top