Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Perl 5.8 to 5.16 and HASH error

by robsgoingmad (Initiate)
on Sep 10, 2012 at 18:52 UTC ( [id://992824]=perlquestion: print w/replies, xml ) Need Help??

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

Hi

I have at least 3 challenges 1. My knowledge of perl is growing but not good, developing for 25+ years in a variety of other languages 2. I have been presented someone elses code to work on 3. Due to a new server I have had to upgrade perl from 5.8 to 5.16.0. Should be irrelevant but perl code runs and tests ok on 5.8, but not on 5.16

Below are code snippets. The code is passed xml, parses the data and then uses mysql to update a DB with the data. Tag keys are field names, Tag values are the data values.
....... use strict; use XML::Simple; .... sub updateRes($){ my ($key,$tbl,$ds,$rv,$dbi,$dbu,$dbx,$dbd,@prms,@slct,$sql1,$sql2,$sq +l3,$sql4,$sql5); foreach $key (keys %{$Xml->{data}}){ $sql1.="`$key`,";$sql2.="`$key`=?,";$sql3.='?,'; } ........

I am getting a "Not a HASH reference at ....pl line xx" error, generated by the foreach line above. From documentation I have found there seems to be a change from perl 5.8 to 5.16 with regards to HASH and the foreach. Does the change mean that code needs to change or is that a "red herring"? $Xml->data contains a number of xml tags and data values. perl is running on Linux Centos. Would appreciate any advice

Replies are listed 'Best First'.
Re: Perl 5.8 to 5.16 and HASH error
by toolic (Bishop) on Sep 10, 2012 at 19:10 UTC

    Tips from the Basic debugging checklist:

    • diagnostics to get more info about that warning message.
    • use Data::Dumper; warn Dumper($Xml->{data}); to get a clearer picture of your data structure.
      Hi

      This is the content of the $XML structure

      Regards Rob

        $VAR1 = { 'Table' => 'siclonesites', 'Site' => 'admin_eventres', 'Pwd' => 'woev1', 'e21' => 'siclone', 'Action' => 'Update', 'data' => [ { 'SCSS' => {},

        The data key is an array of hashes. See perldsc.

Re: Perl 5.8 to 5.16 and HASH error
by AnomalousMonk (Archbishop) on Sep 10, 2012 at 20:03 UTC
    foreach $key (keys %{$Xml->{data}}){ ... }

    In the quoted code,  $Xml is used as a hash reference. Further in your post, you say

    $Xml->[data] contains a number of xml tags and data values.
    Note the difference: curly versus square braces. (I am assuming you meant to write  $Xml->[data] in a code block because as it stands, it renders as a dead  <a> link.)

    So which is it: a hash reference (curly braces) ofor an array reference (square braces)?

      Hi

      Below is the data from $Xml using Dumper. I am not expecting to change any code. Code has moved from an old server to new server. Only differences are Linux version has changed from 2.6.24 to 2.6.18 and perl version has changed from 5.8.8 to 5.16.0

      At this point I am expecting all the code to remain as it is. It has been working for years. If you advise that "things have changed" then I will need to reconsider.

      I tried the code change you suggested and received a 500 Internal Server Error. Hope the above information helps

      Regards

      Rob

Re: Perl 5.8 to 5.16 and HASH error
by ig (Vicar) on Sep 10, 2012 at 23:52 UTC

    When I am dealing with non-trivial data structures and when variables seem not to have the values I expect or need, I often use Data::Dumper to print the variables and see what I am dealing with. I might print to a log file but often find it sufficient to terminate the program with something like:

    use Data::Dumper; ... die Dumper($Xml); foreach $key (keys %{$Xml->{data}}){ $sql1.="`$key`,";$sql2.="`$key`=?,";$sql3.='?,'; }

    You know that either $Xml or $Xml->{data} is not a hash reference, from the error message. I would first want to know what it is. Then I would decide whether to change the preceding code to set it to what the following code requires, or change the following code to handle what it is.

      Hi

      Dumper data below. Hope it helps

      Thanks for your help

      Rob

        From your Dumper output you can see that $Xml->{data} is a reference to an array, not a hash.

        You can either change your code to produce the hash reference as it did before or change your code to work with the array as you are getting now. Which will be easier for you I can't say, having seen so little of your code.

        I would look carefully to see why the second hash in the array appears: the one with a single key 'XxX'. That key looks a little odd to me. Perhaps someone added some 'test' data to the input?

        One option would be to ignore the second and any other hashes that might appear in the array and process only the first one. This would be an easy next step at least, and you could always make further changes later.

        To process the first hash, you can change your loop to something like:

        foreach $key (keys %{$Xml->{data}->[0]}){ $sql1.="`$key`,";$sql2.="`$key`=?,";$sql3.='?,'; }
Re: Perl 5.8 to 5.16 and HASH error (mod)
by tye (Sage) on Sep 10, 2012 at 19:35 UTC

    I'd start looking at the module version upgrade before the Perl version upgrade to explain the problem you seem to be having.

    - tye        

      Hi

      Not sure what you mean regarding module version upgrade.

      1. Old server running Linux 2.6.24 GNU/Linux. New server running Linux 2.6.18 GBU/Linux 2. Old perl version 5.8.8, new perl version 5.16.0 I have used cpanm to upgrade all "use libraries" to latest versions. 3. The code has been copied from the old server to the new server.

      I am not expecting to change any of the code. Surely the same code should work?

      If you advise that there are differences in how code functions between the 2 different perl versions then I have a big problem. This is the first phase of transferring code from the old server. There are bigger applications to move once this is working.

      Thanks for your help

      Regards

      Rob

        In your original node, you only actual show a couple of modules being used:

        use strict; use XML::Simpl­e;

        I wasn't thinking the problem was due to strict.pm changes.

        I believe the data structure that is no longer in the same shape that it used to be in is one being generated by that other module.

        You might want to compare the version numbers of that module and even read the Changes file for that module.

        - tye        

        Not sure what you mean regarding module version upgrade.

        He means, before upgrading perl (from 5.8.8), upgrade the modules in the old perl ( INSTALL_BASE )

        That ought to break some things and perl version won't be the reason

        After you've fixed it to work with latest modules under 5.8.8, then upgrade to 5.16 to see what else breaks

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-20 00:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found