Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why isn't my hash empty? 2

Status
Not open for further replies.

cpjust

Programmer
Sep 23, 2003
2,132
US
Hi,
I have another problem now.
I have a Perl function that calls a Java function over XMLRPC which returns a Map, but in this test, it should return an empty Map (and does in the Java version of this test); but in Perl Dumper reports it's returning the following:
Code:
$VAR1 = {
          '' => undef
        };
The code is:
Code:
my %ret = ();
...
%ret = $api->getAllExternalData( $ticketID );
When I use Dumper on %ret right after I declare it, it is empty:
Code:
$VAR1 = {};
 
Another thing I noticed is that when my hash isn't empty (i.e. Java returns a non-empty Map) I get this in Dumper:
Code:
$VAR1 = {
          'HASH(0x869ac4)' => undef
        };
Is that a hash inside a hash?
 
You can't use a reference as a hash key so that explains the stringification of the reference when used as a hash key. Maybe you should be returning the results to a scalar instead of a hash:

Code:
my [red]$[/red]hash_ref = $api->getAllExternalData( $ticketID );
print Dumper \$hash_ref;

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
OK, now when I do this in the $api->getAllExternalData() function:
Code:
sub getAllExternalData
{
   my $self = shift;
   my $ticketID = shift;
   my $result;
   my $xmlrpc = XML::RPC->new( $url, %{$self->{lwp_useragent}} );
   $result = $xmlrpc->call( $GET_ALL_EXTERNAL_DATA, ($ticketID) );
   print Dumper \$result;
   return $result;
}
I get these results for an empty Java Map & a Map with data:
Code:
$VAR1 = \undef;

$VAR1 = \{
            'url_artifacts' => 'External Data for url_artifacts'
          };
Which looks good, but when my test function calls $api->getAllExternalData() and uses Dumper on the hash it returns:
Code:
my %ret = $api->getAllExternalData( $ticketID );
print "\n Size of extData = " . keys(%ret) . "\n";
print Dumper \%ret;
I get this from Dumper for an empty & non-Empty Map:
Code:
$VAR1 = {
          '' => undef
        };

$VAR1 = {
          'HASH(0x8af480)' => undef
        };
How do I return the hash properly from getAllExternalData() so Dumper reports the same thing both inside & outside the function?

P.S. I'm not a Perl programmer, so I'm not sure what a hash reference is...
 
Like this:

Code:
my $ret = $api->getAllExternalData( $ticketID );
print Dumper \$ret;
sub getAllExternalData
{
   my $self = shift;
   my $ticketID = shift;
   my $result;
   my $xmlrpc = XML::RPC->new( $url, %{$self->{lwp_useragent}} );
   $result = $xmlrpc->call( $GET_ALL_EXTERNAL_DATA, ($ticketID) );
   print Dumper \$result;
   return $result;
}



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I don't see any difference between that and what I posted?

Anyways, I did some reading about references and got it working by doing this:
Code:
sub getAllExternalData
{
	my $self = shift;
	my $ticketID = shift;
	my $result;
	my %extData = ();

	my $xmlrpc = XML::RPC->new( $url, %{$self->{lwp_useragent}} );
	$result = $xmlrpc->call( $GET_ALL_EXTERNAL_DATA, ($ticketID) );

	[b]if ( defined( $result ) ) {
		%extData = %{$result};
	}

	return %extData;[/b]
}
 
You did this:

Code:
my [red]%[/red]ret = $api->getAllExternalData( $ticketID );

I did this:

Code:
my [red]$[/red]ret = $api->getAllExternalData( $ticketID );

Your function was returning a reference to a hash, and a reference is always a scalar. Now you are dereferencing the reference and then returning it as a regular hash. Thats fine as long as you are assigning it to a hash when its returned back from the function. What best is that you understand the difference between what you were doing, what you are now doing, and what I was showing you. I think you do now.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Ah, now I see, you changed the line that calls the function, not the function itself.
I actually fixed another problem using the same code change, but using a reference to an array instead of hash:
Code:
	if ( defined( $result ) ) {
		@keys = @{$result};
	}
Thanks for pointing me in the right direction.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top