Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Error "Can't use an undefined value as a HASH reference" while working with generated objects

by HJO (Acolyte)
on Oct 11, 2012 at 09:19 UTC ( #998396=perlquestion: print w/ replies, xml ) Need Help??
HJO has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm currently working on a project involving a Perl module which allows me to retreive data from a SAP system.

To do so, I give the Perl module some informations like the connexion parameters to the SAP system and the name of the SAP function-module I want to use (and his parameters as well).

The result is coming back as an Perl object, in which a structure (table of one row) is an hash table structured like key=field name and value = value, and a table an array of hash tables.

My problem is that sometimes, there are no data in the SAP system(not a bug, simply no data for the time period I'm specifying), but when it arrives in the Perl script, I'm suddently having the error Can't use an undefined value as a HASH reference at ./script.pl line 222..

What I'm looking for is a tip on how I could find out right after I retrieved my object that some of his arrays/hashes are empty... I tried to get "blanks" in the array before it was treated, but the array is erased totaly before beeing rewritten... I also tried to use the defined Perl function, but unsuccessfully (altrough I might have used it wrong...)

Here is the code I'm using to unpack my array :

@values = $rc->{parameters}->{$key}->{value}; my %valeur = %{$values[0][0]}; my @fields = keys(%valeur); my $nb_fields = scalar(@fields); for my $i (0 .. $nb_fields-1) { ${$struc_ref}[$i] = $fields[$i]; } #recuperation of the number of rows retrieved by the function-module my $nb_lignes = scalar(@{$values[0]}); #recuperation of the number of rows already in the table my $nb_lignes_base = scalar(@{$table_ref}); #display of data (for control) for my $i (0 .. $nb_lignes-1){ #number of rows retrieved by th F- +M for my $j (0 .. $nb_fields-1){ #number of fields in the table #storing data in a table ${$table_ref}[$nb_lignes_base+$i][$j] = $values[0][$i]{$fields +[$j]}; } }

The variables named $..._ref are referencies to two tables, struc stand for structure(a particular one row table), the variable $rc contains the massiv object that I get from my Perl module, (the 222 line is the one with "my %valeur = %{$values[0][0]};") and I appologize for other variables names, I'm french so I named them in an intuitive way for me, I tried to translate comments thought

I hope I gave you enough data to help me, if not please tell me and I will provide so... for any trouble with my variable names, I can also explain them ^^"

Regards

Comment on Error "Can't use an undefined value as a HASH reference" while working with generated objects
Select or Download Code
Re: Error "Can't use an undefined value as a HASH reference" while working with generated objects
by Anonymous Monk on Oct 11, 2012 at 09:26 UTC

    What I'm looking for is a tip on how I could find out right after I retrieved my object that some of his arrays/hashes are empty..

    use defined, and exists, or use Data::Diver 'Dive';

    if( my $want = Dive( $foo, qw/ bar anything you want / ) ){ ... }

      Well thank you Anonymous Monk, I tried to use http://perldoc.perl.org/functions/defined.html, but I didn't manage to get what I wanted, srely because I don't know very well how to use it...

      I applied the answer that McA gave me and it worked just fine, so thanks for the answer, I'll look for this Dive function...

Re: Error "Can't use an undefined value as a HASH reference" while working with generated objects
by McA (Priest) on Oct 11, 2012 at 09:29 UTC
    my %valeur = defined($values[0][0]) && ref($values[0][0]) eq 'HASH' ? +%{$values[0][0]} : ();

    Probably somthing like this?

    Best regards
    McA

      Hi McA,

      Waow, thanks it worked perfectly, but could you just please explain it to me ^^" I'm unused to your use of : ? %{$values[0][0]} : ();

        my %valeur; if( @values and defined $values[0] and 'ARRAY' eq ref $values[0] and defined $values[0][0] and 'HASH' eq ref $values[0][0] ) { %valeur = %{ $values[0][0] }; }

        my %valeur; if ( my $want = Dive( \@values, qw/ 0 0 / ){ %valeur = %$want; }

        Hi!

        Sorry for the late reply: Please have a look at http://perldoc.perl.org/perlop.html#Conditional-Operator . This operator is also known as triadic operator. It's a kind of if-then-else-operator.

        Hope it helps

        Best regards
        McA

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (18)
As of 2015-07-02 13:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (39 votes), past polls