Prev: FAQ 7.24 How can I catch accesses to undefined variables, functions, or methods?
Next: using bash process substitution in perl pipe?
From: Peng Yu on 5 Jun 2010 10:52 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
From: Charlton Wilbur on 5 Jun 2010 14:26 >>>>> "PY" == Peng Yu <pengyu.ut(a)gmail.com> writes: 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 -- Charlton Wilbur cwilbur(a)chromatico.net
From: Peng Yu on 5 Jun 2010 16:05 On Jun 5, 1:26 pm, Charlton Wilbur <cwil...(a)chromatico.net> wrote: > >>>>> "PY" == Peng Yu <pengyu...(a)gmail.com> writes: > > 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.
From: Charlton Wilbur on 5 Jun 2010 16:13
>>>>> "PY" == Peng Yu <pengyu.ut(a)gmail.com> writes: 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 -- Charlton Wilbur cwilbur(a)chromatico.net |