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.

In reply to WDDX.pm weird eval error by alienhuman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.