Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Problem with symbolic deferencing into XML::Simple's internal representation of an XML file.

by Rhose (Priest)
on Dec 05, 2001 at 20:58 UTC ( [id://129656]=note: print w/replies, xml ) Need Help??


in reply to Problem with symbolic deferencing into XML::Simple's internal representation of an XML file.

First off, the code I am attaching is not pretty, and there may well be a LOT better way of doing this, but I hope I am at least on the right track.

Also, I noticed in your code, you referenced tel_no, but the XML file has tel_num... I will assume tel_num is correct. I also noticed the following was probably a typo:

$parm_str = "\$" . "config->{jane}->{tel_no}, "\n"; $parm_str = '$config->{jane}->{tel_no}';

as the string "config->{jane}->{tel_no}" was not correctly terminated. Anyway, here is my first pass, please let me know if this helps/does not help. (I whimped out and used recursion. *Smiles*)

package xmlutil; #-- Use modules use strict; use XML::Simple; #-- Define constants use constant DEBUG => 1; use constant DELIM => ','; use constant XMLFILE => './galaxy.xml'; #-- Define global variables my $gConfig = XMLin(XMLFILE); #-- This is the sub which is used to interface to the #-- main perl code. Had to do this as the C program does not #-- know $gConfig sub get_ival { #-- Get parameter my $pParm=shift; #-- Return the value print get_ivnode(\$gConfig,$pParm); } #-- This is the sub which recursively processes the XML hash array sub get_ivnode { #-- Get parameters my $pPtr=shift; my @pParm = split(DELIM,shift); #-- Define local variables my $lKey; my $lNewPtr; #-- Get the next key $lKey=shift(@pParm); #-- Display debugging information print '[',$lKey,']',"\n" if DEBUG; #-- Return the value if there are no further keys return $$pPtr unless $lKey; #-- Build the new pointer $lNewPtr=$$pPtr->{$lKey}; #-- Call with next node return get_ivnode(\$lNewPtr,join(DELIM,@pParm)); }
The code I used to test was:

use strict; use lib '/temp'; use xmlutil; xmlutil::get_ival('jane,tel_num');

Notice this "solution" requires you to change how you are calling the perl code -- instead of building the "perlish" string, you list the hierarchy separated by commas.

Am I off base here, or is this what you wanted?

Update:

Might want to change the following line so the recursive sub is called one time fewer.

return $$pPtr unless $lKey; return $$pPtr->{$lKey} unless $pParm[0];
  • Comment on Re: Problem with symbolic deferencing into XML::Simple's internal representation of an XML file.
  • Select or Download Code

Replies are listed 'Best First'.
Re: Re: Problem with symbolic deferencing into XML::Simple's internal representation of an XML file.
by Anonymous Monk on Dec 05, 2001 at 21:54 UTC
    Thanks for your help... this is great. Sorry 'bout the typos you pointed out. I typed the code in because my cut'n'paste has packed in. Thanks again, I will try your solution immediately. Phil

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://129656]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-26 02:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found