Leser: 24
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 Data::Dumper; #+~~~~~~~ Referenz { my %vars; my $mode = 1; my $curr_grad =\$vars{'gradient'}{$mode}; #...tu was... $vars{'gradient'}{1} = [[1,1,1],[1,2,3]]; print $$curr_grad->[1][1]++,"\n"; print $vars{'gradient'}{$mode}[1][1],"\n"; } #+~~~~~~~ Array { my %vars; my $mode = 1; $vars{'gradient'}{1} = [[],[]]; my @curr_grad = @{$vars{'gradient'}{$mode}}; #...tu was... (@{$vars{'gradient'}{1}[0]}[0..2],@{$vars{'gradient'}{1}[1]}[0..2]) = ((1,1,1),(1,2,3)); print $curr_grad[1][1]++,"\n"; print $vars{'gradient'}{$mode}[1][1],"\n"; }
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
use Storable qw(freeze thaw); # example of data structure my $r = { ARRAY => [localtime], HASH => \%ENV, }; # hier die Möglichkeiten zum Speichern in einer Datei # Herzstück ist freeze() und thaw() # mit IO::File dann weitermachen.... my $bin = freeze $r; # Serialize my $rep = thaw $bin; # Deserialize # test it #print dump $rep; # Entwicklung in Richtung Klasse # Zum testen wird das Objekt hier zu einer Instanz der main-Class gemacht bless $r, __PACKAGE__; # main-Class # damit kann nun eine Methode aufgerufen werden $r->printarray; sub printarray{ my $self = shift; # extrahiere Instanz foreach my $e( @{$self->{ARRAY}} ){ print "$e\n", } return $self->{ARRAY}; # falls wir das Array draußen brauchen }
1 2 3 4 5 6
my $m = myObjects->new( autostore => 1, file => '/tmp/messwerte.bin', lock => 1 ); $m->insert('2011-10-03', {ort => 'Oppenheim', temp => [12, 13, 25, 19]}) or die $m->errstr; $m->insert('2011-10-04', {ort => 'Oppenheim', temp => [8, 12, 22, 18]}); my $werte = $m->getobject('2011-10-04'); print $werte->{ort}, "\n"; # Oppenheim print join("\n", @{$werte->{temp}});
1 2 3 4 5 6 7 8 9 10
my (variables); read_param(); create_input(); run_a_program(); read_its_log(); calculate_stuff(); write_output();
1 2 3
my $ob = Foo->new('2011-10-05'); # alles drin für einen Tag my $array_ref = $o->show_temperaturen; my $hash_ref = $o->show_messpunkte;
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
#!/usr/bin/perl use strict; use warnings; {package Matrix; use overload '+' => \&plus, '""' => \± sub new{ my $class = shift; bless {}, $class; } sub fl{ my $self = shift; my $line = shift; $self->{'mat'}[$line] = [@_]; } sub mat{ return $_[0]->{'mat'}; } sub pm{ for (@{$_[0]->{'mat'}}){ print "@{$_}\n"; } } sub plus{ my $m1 = $_[0]->mat; my $m2 = $_[1]->mat; my @res; for my $i (0..$#{$m1}){ for my $j (0..$#{$m1->[$i]}){ $res[$i][$j] = $m1->[$i][$j]+$m2->[$i][$j]; } } bless {mat => [@res]}, 'Matrix'; } } my $x; $x = Matrix->new(); $x->fl(0,1,2,3); $x->fl(1,6,5,4); $x->fl(2,2,2,2); my $y; $y = Matrix->new(); $y->fl(0,6,7,8); $y->fl(1,4,4,4); $y->fl(2,1,1,1); my $z = $x + $y; print $x,"+\n",$y,"=\n",$z;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; # Referenz { my %vars; my $mode = 1; my $curr_grad = $vars{gradient}->{$mode}; # ...tu was... # $vars{gradient}->{1} = [ [1, 1, 1], [1, 2, 3] ]; @$curr_grad = ( [1, 1, 1], [1, 2, 3] ); print $curr_grad->[1]->[1]++, "\n"; print $vars{gradient}->{$mode}->[1]->[1], "\n"; }
$vars{gradient}->{1} = [ [1, 1, 1], [1, 2, 3] ];