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

Good day monks. I have a socket application running that returns a data structure using a Data::Dumper string. Here is the sub that calls it:
sub processtext { my ($hostid,$port,$text) = @_; my $socket = IO::Socket::INET->new(PeerAddr => $hostid, PeerPort => $port, Proto => "tcp", Type => SOCK_STREAM) or die "Couldn't connect: $!\n"; print $socket "$text\n"; # read the remote answer, my ($blob,$answer,$obj); while ($answer = <$socket>) { $blob .= $answer; } close($socket); $obj = eval($blob); return $obj; }
At the end of this sub, $obj is an undefined scalar rather than a hash like it should be. The socket app is working and is returning a valid dumper string. I know this because I cut and pasted the value of $blob from the debugger into a separate script and I was able to eval it and get the correct object back.

What could be keeping the $blob string from evaluating properly in this sub? I'm kinda baffled here.

TIA....Steve

Replies are listed 'Best First'.
Re: Dumper output not evaling properly
by davido (Cardinal) on Mar 16, 2006 at 01:02 UTC

    To see the problem's actual error, try this:

    $obj = eval( $blob ) or die $@;

    And here's a workaround that worked in my test:

    $blob =~ s/^\$VAR\d+\s*=//; $obj = eval( $blob ) or die $@;

    Updated.

    The problem is that evaling $blob tries to create a global variable named $VAR1, which violates strictures, causing the eval to fail. Without the "or die..." part, it fails silently.


    Dave

      Yup. That's it. Actually declaring our $VAR1; does the trick with the original code.

      Many thanks. I thought I was losing it there for a while!

      Steve