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 28 29 30 31
#!/usr/bin/perl use strict; use warnings; use 5.010; my $test1 = ['Birne','Apfel']; my @test2 = ('Fred','Anna'); my %test3 = (foo => 'bar',abc => 'def'); my $test4 = 'string'; my $ref = [ 0, 1, 2, [$test1,\@test2,\%test3,\$test4], ]; if (defined $ref->[3]) { require Data::Dumper; local $Data::Dumper::Useqq; $Data::Dumper::Useqq = 1; local $Data::Dumper::Sortkeys; $Data::Dumper::Sortkeys = sub { my ($hash) = @_; return [(sort {$a =~ /^\d+$/ && $b =~ /^\d+$/ ? $a <=> $b : lc($a) cmp lc($b)} keys %$hash)]; }; foreach my $keyref (@{$ref->[3]}) { say Data::Dumper::Dumper($keyref); } }
Quote$VAR1 = [
"Birne",
"Apfel"
];
$VAR1 = [
"Fred",
"Anna"
];
$VAR1 = {
"abc" => "def",
"foo" => "bar"
};
$VAR1 = \"string";
Quote@$test1 = [
"Birne",
"Apfel"
];
@test2 = [
"Fred",
"Anna"
];
%$test3 = {
"abc" => "def",
"foo" => "bar"
};
$test4 = \"string";
perldoc Data::Dumper...
# extended usage with names
print Data::Dumper->Dump([$foo, $bar], [qw(foo *ary)]);
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# perl
use 5.010;
require Data::Dumper;
my $foo = [ qw( an array ) ];
my $bar = { a => "hash" };
say Data::Dumper->Dump( [ $foo, $bar ], [ 'foo', '*bar' ]);
^D
$foo = [
'an',
'array'
];
%bar = (
'a' => 'hash'
);
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 28 29 30 31 32 33 34 35 36
#!/usr/bin/perl use strict; use warnings; use 5.010; my $test1 = ['Birne','Apfel']; my @test2 = ('Fred','Anna'); my %test3 = (foo => 'bar',abc => 'def'); my $test4 = 'string'; my @uebergabe = ( 0, 1, 2, [$test1,\@test2,\%test3,\$test4], ); aufruf(\@uebergabe); sub aufruf { my ($ref) = @_; if (defined $ref->[3] && ref($ref->[3]) eq 'ARRAY') { require Data::Dumper; local $Data::Dumper::Useqq; $Data::Dumper::Useqq = 1; local $Data::Dumper::Sortkeys; $Data::Dumper::Sortkeys = sub { my ($hash) = @_; return [(sort {$a =~ /^\d+$/ && $b =~ /^\d+$/ ? $a <=> $b : lc($a) cmp lc($b)} keys %$hash)]; }; foreach my $keyref (@{$ref->[3]}) { say Data::Dumper::Dumper($keyref); # hier weiß ich nicht, dass die test1, test2 usw. heißen } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
use strict; use warnings; use 5.010; sub my_dumper { require Data::Dumper; my %args = @_; my @names = keys %args; my @refs = values %args; warn Data::Dumper->Dump( \@refs, \@names ), "\n"; } my $foo = [ qw( an array ) ]; my $bar = { a => "hash" }; my_dumper( 'foo' => $foo, '*bar' => $bar );
QuoteCan't use string ("$test1") as a HASH ref while "strict refs" in use at sub_meldung_machen.pl line 63.
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#!/usr/bin/perl use strict; use warnings; use 5.010; system 'cls'; my $test1 = ['Birne','Apfel']; my @test2 = ('Fred','Anna'); my %test3 = (foo => 'bar',abc => 'def'); my $test4 = 'string'; my %uebergabe = ( liste => [ [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 'Testtext', # [$test1,\@test2,\%test3,\$test4], [ '$test1' => $test1 ] ] ] ); my $env; meldung_machen($env,\%uebergabe); ############################################################################### ############################################################################### sub meldung_machen{ my ($env,$transp) = @_; foreach my $ref (@{$transp->{liste}}) { # Text setzen my $text = $ref->[9]; # Dump/-s im Text ergänzen if (defined $ref->[10] && ref($ref->[10]) eq 'ARRAY') { require Data::Dumper; local $Data::Dumper::Useqq; $Data::Dumper::Useqq = 1; local $Data::Dumper::Sortkeys; $Data::Dumper::Sortkeys = sub { my ($hash) = @_; return [(sort {$a =~ /^\d+$/ && $b =~ /^\d+$/ ? $a <=> $b : lc($a) cmp lc($b)} keys %$hash)]; }; foreach my $args (@{$ref->[10]}) { #say $args; say "Zeile ".__LINE__.': '.Data::Dumper::Dumper($args); my @names = keys %$args; my @refs = values %$args; my $dump = Data::Dumper::Dump(\@refs,\@names); $dump =~ s/&/&/g; $dump =~ s/</</g; $dump =~ s/>/>/g; $dump =~ s/\n/<br>/g; $text .= "<br><br>Dump von xxx:\n<pre>$dump</pre>"; } } say $text; } }
2021-05-26T11:55:47 LinuxerJa habe ich verstanden und geändert.In Zeile 25-27 definierst Du eine Array-Referenz, keine Hash-Referenz.
Und dann iterierst Du durch die Elemente des Array; scheint mir auch komisch.
QuoteNot a HASH reference at C:/Strawberry/perl/lib/Data/Dumper.pm line 232.
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
#!/usr/bin/perl use strict; use warnings; use 5.010; system 'cls'; my $test1 = ['Birne','Apfel']; my @test2 = ('Fred','Anna'); my %test3 = (foo => 'bar',abc => 'def'); my $test4 = 'string'; my %uebergabe = ( liste => [ [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 'Testtext', { '@test1' => $test1, '@$test2' => \@test2, '%$test3' => \%test3, '$test4' => $test4, } ] ] ); my $env; meldung_machen($env,\%uebergabe); ############################################################################### ############################################################################### sub meldung_machen{ my ($env,$transp) = @_; foreach my $ref (@{$transp->{liste}}) { # Text setzen my $text = $ref->[9]; # Dump/-s im Text ergänzen if (defined $ref->[10] && ref($ref->[10]) eq 'HASH') { require Data::Dumper; local $Data::Dumper::Useqq; $Data::Dumper::Useqq = 1; local $Data::Dumper::Sortkeys; $Data::Dumper::Sortkeys = sub { my ($hash) = @_; return [(sort {$a =~ /^\d+$/ && $b =~ /^\d+$/ ? $a <=> $b : lc($a) cmp lc($b)} keys %$hash)]; }; my @names = keys %{$ref->[10]}; my @refs = values %{$ref->[10]}; my $dump = Data::Dumper::Dump(\@refs,\@names); $dump =~ s/&/&/g; $dump =~ s/</</g; $dump =~ s/>/>/g; $dump =~ s/\n/<br>/g; $text .= "<br><br>Dump von xxx:\n<pre>$dump</pre>"; say $text; } } }
2021-05-26T20:43:12 LinuxerDu rufst in Zeile 65 Dump() als Funktion auf (Data::Dumper::Dump(...)), nicht wie zuvor gezeigt als Methode: Data::Dumper->Dump(...)
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#! /usr/bin/env perl use strict; use warnings; use 5.010; sub my_dumper1 { require Data::Dumper; my @dumps; my $trenner = "\n#-----------------\n\n"; while ( my ( $name, $ref ) = splice( @_, 0, 2 ) ) { push @dumps, Data::Dumper->Dump( [$ref], [$name]); } warn join $trenner, @dumps, ""; } sub my_dumper2 { require Data::Dumper; my %args = @_; my @names = keys %args; my @refs = values %args; my $dump = Data::Dumper->Dump( \@refs, \@names ); my $trenner = "\n#-----------------\n\n"; $dump =~ s/;\s*$/;$trenner/mg; warn $dump; } my $foo = [ qw( an array ) ]; my $bar = { a => "hash" }; warn "Variante 1\n"; my_dumper1( 'foo' => $foo, '*bar' => $bar ); warn "Variante 2\n"; my_dumper2( 'foo' => $foo, '*bar' => $bar );
1 2 3 4 5 6 7 8 9 10 11 12 13 14
my @dumplist; foreach my $key (keys %{$ref->[10]}) { my $dumptxt = Data::Dumper->Dump([$ref->[10]{$key}],[$key]); $dumptxt =~ s/&/&/g; $dumptxt =~ s/</</g; $dumptxt =~ s/>/>/g; $dumptxt =~ s/\n/<br>/g; push @dumplist,$dumptxt; } $text .= "<br><br>Dump/-s:\n<pre>" .join("<b>----------------------------------------------------------------</b><br>",@dumplist) ."</pre>" ;
my_dumper1( 'foo' => $foo, '*bar' => $bar );