Professional Web Applications Themes

determining reference type - PERL Beginners

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...

  1. #1

    Default determining reference type

    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

    Andrew Guest

  2. #2

    Default Re: determining reference type

    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

    HTH
    -Sx-
    Wc Guest

  3. #3

    Default Re: determining reference type

    On Tue, Apr 06, 2004 at 05:15:17PM -0500, Andrew Gaffney wrote:
     

    Take a look at ref and UNIVERSAL::isa.

    perldoc -f ref
    perldoc UNIVERSAL

    --
    Paul Johnson - net
    http://www.pjcj.net
    Paul Guest

  4. #4

    Default Re: determining reference type


     
    function (one in 
    hash references. I 
    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
    Wiggins Guest

  5. #5

    Default Re: determining reference type

    WC -Sx- Jones 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[/ref]

    That did the trick. Thanks.

    --
    Andrew Gaffney
    Network Administrator
    Skyline Aeronautics, LLC.
    636-357-1548

    Andrew Guest

  6. #6

    Default Re: determining reference type



    Andrew Gaffney wrote:
     

    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

    Jupiterhost.Net Guest

Similar Threads

  1. Determining node type
    By RavenWorks in forum Macromedia Director 3D
    Replies: 1
    Last Post: September 21st, 04:02 PM
  2. Determining the Integrated Windows Authentication type
    By Jim Dennison in forum ASP.NET Security
    Replies: 0
    Last Post: September 14th, 03:37 PM
  3. Determining media type for a volume under OS X
    By Keith in forum Mac Programming
    Replies: 0
    Last Post: October 21st, 08:14 PM
  4. determining type of referance
    By Dave Odell in forum PERL Beginners
    Replies: 2
    Last Post: September 23rd, 07:51 PM
  5. Determining list type resolved
    By Robert Tweed in forum Macromedia Director Lingo
    Replies: 1
    Last Post: August 2nd, 09:55 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139