| Author |
determining reference type
|
|
| Andrew Gaffney 2004-04-06, 7:30 pm |
| How do you determine what type of data a reference points to? I have a function (one in
previous post) that can take either an array of scalars or an array of hash references. I
want to execute different code based on which one it was. How can I do that?
--
Andrew Gaffney
Network Administrator
Skyline Aeronautics, LLC.
636-357-1548
| |
| Paul Johnson 2004-04-06, 7:30 pm |
| On Tue, Apr 06, 2004 at 05:15:17PM -0500, Andrew Gaffney wrote:
> How do you determine what type of data a reference points to? I have a
> function (one in previous post) that can take either an array of scalars or
> an array of hash references. I want to execute different code based on
> which one it was. How can I do that?
Take a look at ref and UNIVERSAL::isa.
perldoc -f ref
perldoc UNIVERSAL
--
Paul Johnson - paul@pjcj.net
http://www.pjcj.net
| |
| Wc -Sx- Jones 2004-04-06, 7:31 pm |
| Andrew Gaffney wrote:
> How do you determine what type of data a reference points to? I have a
> function (one in previous post) that can take either an array of scalars
> or an array of hash references. I want to execute different code based
> on which one it was. How can I do that?
>
use the ref() function -
perldoc -f ref
sub marshall {
my($thing) = @_;
$type = ref($thing);
if ($type eq "ARRAY") {
return(encode_list($thing));
}
elsif ($type eq "HASH") {
return(encode_hash($thing));
}
elsif (!$type) {
return(encode_scalar($thing));
}
else { die("Can't handle $type\n"); }
}
http://www.usenix.org/publications/perl/perl12.html
HTH
-Sx-
| |
| Wiggins D Anconia 2004-04-06, 7:31 pm |
|
> How do you determine what type of data a reference points to? I have a
function (one in
> previous post) that can take either an array of scalars or an array of
hash references. I
> want to execute different code based on which one it was. How can I do
that?
>
perldoc -f ref
Alternatively if the reference can hold an object you may want the 'isa'
method of:
perldoc UNIVERSAL
my $ref ...
if (UNIVERSAL::isa($ref, 'Gaff::Obj')) {
# handle Gaff objects
}
elsif (ref($ref) eq 'HASH') {
# handle hash ref
}
elsif (ref($ref) eq 'ARRAY') {
# handle array ref
}
etc.
http://danconia.org
| |
| Andrew Gaffney 2004-04-06, 8:30 pm |
| WC -Sx- Jones wrote:
> Andrew Gaffney wrote:
>
>
> use the ref() function -
> perldoc -f ref
>
> sub marshall {
> my($thing) = @_;
>
> $type = ref($thing);
> if ($type eq "ARRAY") {
> return(encode_list($thing));
> }
> elsif ($type eq "HASH") {
> return(encode_hash($thing));
> }
> elsif (!$type) {
> return(encode_scalar($thing));
> }
> else { die("Can't handle $type\n"); }
> }
>
>
> http://www.usenix.org/publications/perl/perl12.html
That did the trick. Thanks.
--
Andrew Gaffney
Network Administrator
Skyline Aeronautics, LLC.
636-357-1548
| |
| Jupiterhost.Net 2004-04-07, 1:31 pm |
|
Andrew Gaffney wrote:
> How do you determine what type of data a reference points to? I have a
> function (one in previous post) that can take either an array of scalars
> or an array of hash references. I want to execute different code based
> on which one it was. How can I do that?
perldoc -f ref
sub dohicky {
for(@_) {
if(ref($_) eq 'ARRAY') {
# process as an array ref
} elsif(ref($_) eq 'HASH') {
# process as a hash ref
} else {
# process non HASH/ARRAY REF
}
}
}
HTH
Lee.M - JupiterHost.Net
|
|
|
|