cross von common.pl nach MoreCommon verschoben.
[kivitendo-erp.git] / SL / MoreCommon.pm
1 package SL::MoreCommon;
2
3 require Exporter;
4 @ISA = qw(Exporter);
5
6 @EXPORT = qw(save_form restore_form compare_numbers any cross);
7
8 use YAML;
9
10 use SL::AM;
11
12 sub save_form {
13   $main::lxdebug->enter_sub();
14
15   my @dont_dump_keys = @_;
16   my %not_dumped_values;
17
18   foreach my $key (@dont_dump_keys) {
19     $not_dumped_values{$key} = $main::form->{$key};
20     delete $main::form->{$key};
21   }
22
23   my $old_form = YAML::Dump($main::form);
24   $old_form =~ s|!|!:|g;
25   $old_form =~ s|\n|!n|g;
26   $old_form =~ s|\r|!r|g;
27
28   map { $main::form->{$_} = $not_dumped_values{$_} } keys %not_dumped_values;
29
30   $main::lxdebug->leave_sub();
31
32   return $old_form;
33 }
34
35 sub restore_form {
36   $main::lxdebug->enter_sub();
37
38   my ($old_form, $no_delete, @keep_vars) = @_;
39
40   my $form          = $main::form;
41   my %keep_vars_map = map { $_ => 1 } @keep_vars;
42
43   map { delete $form->{$_} if (!$keep_vars_map{$_}); } keys %{$form} unless ($no_delete);
44
45   $old_form =~ s|!r|\r|g;
46   $old_form =~ s|!n|\n|g;
47   $old_form =~ s|![!:]|!|g;
48
49   my $new_form = YAML::Load($old_form);
50   map { $form->{$_} = $new_form->{$_} if (!$keep_vars_map{$_}) } keys %{ $new_form };
51
52   $main::lxdebug->leave_sub();
53 }
54
55 sub compare_numbers {
56   $main::lxdebug->enter_sub();
57
58   my $a      = shift;
59   my $a_unit = shift;
60   my $b      = shift;
61   my $b_unit = shift;
62
63   $main::all_units ||= AM->retrieve_units(\%main::myconfig, $main::form);
64   my $units          = $main::all_units;
65
66   if (!$units->{$a_unit} || !$units->{$b_unit} || ($units->{$a_unit}->{base_unit} ne $units->{$b_unit}->{base_unit})) {
67     $main::lxdebug->leave_sub();
68     return undef;
69   }
70
71   $a *= $units->{$a_unit}->{factor};
72   $b *= $units->{$b_unit}->{factor};
73
74   $main::lxdebug->leave_sub();
75
76   return $a <=> $b;
77 }
78
79 sub any (&@) {
80   my $f = shift;
81   return if ! @_;
82   for (@_) {
83     return 1 if $f->();
84   }
85   return 0;
86 }
87
88 =item cross BLOCK ARRAY ARRAY
89
90 Evaluates BLOCK for each combination of elements in ARRAY1 and ARRAY2
91 and returns a new list consisting of BLOCK's return values.
92 The two elements are set to $a and $b.
93 Note that those two are aliases to the original value so changing them
94 will modify the input arrays.
95
96   # append each to each
97   @a = qw/a b c/;
98   @b = qw/1 2 3/;
99   @x = pairwise { "$a$b" } @a, @b;
100   # returns a1, a2, a3, b1, b2, b3, c1, c2, c3
101
102 As cross expects an array but returns a list it is not directly chainable
103 at the moment. This will be corrected in the future.
104
105 =cut
106 sub cross(&\@\@) {
107   my $op = shift;
108   use vars qw/@A @B/;
109   local (*A, *B) = @_;    # syms for caller's input arrays
110
111   # Localise $a, $b
112   my ($caller_a, $caller_b) = do {
113     my $pkg = caller();
114     no strict 'refs';
115     \*{$pkg.'::a'}, \*{$pkg.'::b'};
116   };
117
118   local(*$caller_a, *$caller_b);
119
120   # This map expression is also the return value.
121   map { my $a_index = $_;
122     map { my $b_index = $_;
123       # assign to $a, $b as refs to caller's array elements
124       (*$caller_a, *$caller_b) = \($A[$a_index], $B[$b_index]);
125       $op->();    # perform the transformation
126     }  0 .. $#B;
127   }  0 .. $#A;
128 }
129
130 1;