in reply to Extracting a hash reference from a string

See Devel::Pointer.
  • Comment on Re: Extracting a hash reference from a string

Replies are listed 'Best First'.
Re: Re: Extracting a hash reference from a string
by Anonymous Monk on Mar 23, 2003 at 03:49 UTC

    This is exactly the pointer (pun intended :) I was looking for.

    The following code accomplishes my task:

    #!/usr/bin/perl -T use strict; use warnings; use Devel::Pointer; eval { require('some_code.pl'); }; if ($@) { my $start = index($@, 'HASH(0x') + 7; my $end = index($@, ')', $start); my $hex = substr($@, $start, $end-$start); my $addr = hex($hex); my $href = unsmash_hv(0+$addr); print("$$href{'Error data'}\n"); } # EOF

    Since the hash is guaranteed (in my case) to have a non-zero reference count, this code is safe for my purposes.

      my $start = index($@, 'HASH(0x') + 7; my $end = index($@, ')', $start); my $hex = substr($@, $start, $end-$start);
      Why the handwork? Why aren't you using a regex?
      my($hex) = $@ =~ /HASH\(0x(\w+)\)/;