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

Yo Monks,

I'm parsing a textfile, turning the data within into a HoHoH, and using WDDX.pm (link) to output it as a WDDX (Web Distributed Data Exchange) packet--an xml document with a structure that corresponds to data types commonly used by various programming languages (including perl).

In my script, I'm using a technique that's new to me. I took it from the documentation of WDDX.pm--I assign a subroutine to a variable, and then pass the variable to a method of the wddx object. However, when I modify the subroutine, I get a very strange error from WDDX.

My code follows (the technique I mention is at line 18, which starts with my $type_sub = ...):

Sorry for the CGI and other variables that aren't being used yet--this script is destined to run as a CGI.

use strict; use CGI; use WDDX; use Data::Dumper; my $q = new CGI; my $wddx = new WDDX; # get params my $report = $q->param('report'); # send request to josh's data service my $hashref = getData($report); #print Dumper($hashref); #print "\n\n"; my $type_sub = sub { my( $name, $row, $mode ) = @_; #print "$name--$row--$mode\n"; #$name =~ /wind/ and return "number"; }; my $wddx_obj = $wddx->hash2wddx( $hashref, $type_sub ); $WDDX::INDENT = " "; print $wddx->serialize( $wddx_obj ); exit; sub getData { # here's where we'll get various query params for ea report my $report = shift; # dummy value # return a hash w data structure my $data; open (FH,"/home/httpd/html/newdev2/wddx/ib_basics.txt") or die("Co +uldn't open: $!"); while (<FH>) { if ($_ =~ /^"\@QUERY:(.*)"/) { my $query = $1; my $i = 0; my @keys = (); while (<FH>) { last if /^"\@ENDQUERY"/; $_ =~ s/\r\n//; $_ =~ s/\"//g; # get the keys push(@keys,split(",",$_)) unless $i ne 0; # reset @values and get 'em my @values = (); push(@values,split(",",$_)) unless $i eq 0; # fill da hashes for(my $k = 0; $k <= $#keys; $k++) { $data->{$query}->{"r$i"}->{$keys[$k]} = $values[$k +] unless $values[$k] eq ""; } # increment the row $i++ } } } close FH; return $data; }

The error is:
Unable to create object of type WDDX::3: Can't locate object method "n +ew" via package "WDDX::3" (perhaps you forgot to load "WDDX::3"?) at +(eval 4) line 1. at text2wddx.cgi line 42

However, there is no package WDDX::3... and I thought it was a strange coincidence that in the $type_sub subroutine I split $_ into 3 variables, so I uncommented this line in my code and everything started running again:

#$name =~ /wind/ and return "number";

My question: What is this technique that I'm stealing, and why does that line confuse WDDX.pm?

For completeness, here is what I think is the eval that is bombing in WDDX.pm:

### from sub hash2wddx my $var = eval "WDDX::\u$type->new( \$val )" or croak "Unable to create object of type WDDX::\u$type: " . _shift_blame( $@ ); $new_hash->{$name} = $var; next; ## sub continues

Thanks,

AH

----------
Using perl 5.6.1 unless otherwise noted. Apache 1.3.27 unless otherwise noted. Redhat 7.1 unless otherwise noted.

Replies are listed 'Best First'.
Re: WDDX.pm weird eval error
by halley (Prior) on Apr 15, 2004 at 15:08 UTC
    If you do WDDX::$type->new(...) and it says "no such WDDX::3", then your $type has something you weren't expecting.

    You get your $type variable by calling a subroutine. What does the subroutine return? It returns whatever you tell it to return, or the last expression evaluated.

    Your subroutine's last line is (or was) a complicated thing which might explicitly return something, and might not. And if you comment out everything else in the sub, you're going to return the list ( $name, $row, $mode ). In scalar context, an array is noted for its length (3), while a list is noted for its last element, and it's perl's guess here as to which you might want to implicitly return.

    --
    [ e d @ h a l l e y . c c ]