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

Is it possible to pack and unpack a variable reference?

--
Brother Marvell

Replies are listed 'Best First'.
Re: reference packing?
by grinder (Bishop) on Jul 23, 2001 at 19:27 UTC
    No.

    Long answer is you need to squirrel away both the reference and what the reference points to. You will probably be happier with the results of Data::Dumper or Storable.

    --
    g r i n d e r

      Just to let you know what I was thinking ...

      I wanted to pack a reference with a sort key, in the style of a Guttman-Rosler transform. If I have to pack in the whole data structure, that might make the sort a bit fat.

      --
      Brother Marvell

      ¤

        Sorting on a reference?? That doesn't make much sense. (:

        But however you encode the references into strings, you'll need to keep the real references external to the strings and use a lookup to restore them.

        To minimize space, you could stringify and cache via:

        my %refs; $str= pack( "N", 0+$ref ); $refs{0+$ref}= $ref;
        then restore via:     $ref= $refs{unpack("N",$str)};

        How to incorporate the packed value into the sort string and then extract it out again depends on how the rest of the string is built and so is left as an exercise. :)

                - tye (but my friends call me "Tye")
Re: reference packing?
by Hofmator (Curate) on Jul 23, 2001 at 19:27 UTC

    Yes you can. Have you tried it??

    my $ref = \1; my @array = unpack("c*",$ref); print join ':', map {chr} @array; print "\n$ref\n";
    I'm not sure what you want to do with that but this works. Perl just takes the string representation of the reference.

    Update: ;-))

    -- Hofmator