Thread not a hash reference bei JSON decode (4 answers)
Opened by perl_gast at 2015-02-18 00:35

Linuxer
 2015-02-19 22:57
#179750 #179750
User since
2006-01-27
3875 Artikel
HausmeisterIn

user image
Du kannst den Teil in ein (Block-) eval() packen und dann eine Warnung ausgeben, falls ein Fehler auftrat.
Siehe: Perldoc:perlfunc eval

Schema:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#! /usr/bin/perl
use strict;
use warnings;
use 5.010.000;

my $data = {
        foo => [
                { bar => 123 },
        ],
};

# keep this declaration outside of the eval-block
my $val = "not defined";

# this with error
eval { $val = $data->{foo}->{bar}; }; 
warn "$@\n" if $@;

# check
say "I'm alive and val is: $val";

# this hopefully without error
eval { $val = $data->{foo}->[0]->{bar}; }; 
warn "$@\n" if $@;

# check again
say "I'm still alive and val is: $val";
meine Beiträge: I.d.R. alle Angaben ohne Gewähr und auf Linux abgestimmt!
Die Sprache heisst Perl, nicht PERL. - Bitte Crossposts als solche kenntlich machen!

View full thread not a hash reference bei JSON decode