Schrift
Wiki:Tipp zum Debugging: use Data::Dumper; local $Data::Dumper::Useqq = 1; print Dumper \@var;
[thread]8230[/thread]

Perl Modul Datei: Umwandlung subroutine in ein modul



<< >> 8 Einträge, 1 Seite
Gast Gast
 2006-08-09 16:44
#68798 #68798
Hallo würde gern folgenden Code in ein Modul umwandeln...:

Kann mir bitte jumd. helfen?
Code: (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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
sub show {
my( $cgi, $rh_template_variables ) = @_;

my $coupon_code = $cgi->param("coupon_code");
return unless $coupon_code;

my %stmt_lookup = (
codes => 'SELECT codelist_id, active, last_change, start_date, end_date, (start_date IS NULL OR start_date < NOW()) AND (end_date IS NULL OR end_date > NOW()) FROM codes WHERE code = ?',
benefits_codes => {

codelist => 'SELECT type, percent, amount, currency, start_date, end_date, description_de, (start_date IS NULL OR start_date < NOW()) AND (end_date IS NULL OR end_date > NOW()) FROM benefits_codes WHERE codelist_id = ?',

code => 'SELECT type, percent, amount, currency, start_date, end_date, description_de, (start_date IS NULL OR start_date < NOW()) AND (end_date IS NULL OR end_date > NOW()) FROM benefits_codes WHERE coupon_code = ?'

}
);

$rh_template_variables->{coupon_code} = $coupon_code;
my $found_something = 0; # Variable wird auf 0 gesetzt
foreach my $shop_abbr ( @{$SHOP::Constants::supported{shop_abbr}{Server}} ) {
my $db_id = $SHOP::Constants::shop{$shop_abbr}{db_id};
my $dbh = DBI->connect( @{$SHOP::Constants::db{$db_id}{connect}{shop_write}} );

my $rh_code = $rh_template_variables->{code_lookup}{$shop_abbr}{code} = {};
@$rh_code{ qw( codelist_id active last_change start_date end_date is_valid ) } = $dbh->selectrow_array( $stmt_lookup{codes}, {}, $coupon_code );
#### $hash{eins}, $hash{zwei}, $hash{drei} @hash{'eins', 'zweiter key', 'drei'} @hash{ qw(eins zweiter key drei), 'zweiter key', qw(...) }
# my( $codelist_id, $active, $last_change, $start_date, $end_date ) = $dbh->selectrow_array( $stmt_lookup{codes}, {}, $coupon_code );
if ( defined $rh_code->{codelist_id} ) {
$found_something = 1; # Wenn eingegebener Code gefunden dann Variable auf 1 gesetzt
$rh_code->{last_change} =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/;
$rh_template_variables->{code_lookup}{$shop_abbr}{code}{last_change} = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $1, $2, $3, $4, $5, $6 );

$rh_code->{is_used} = ( ($rh_code->{active} == 1 or $rh_code->{codelist_id} == 0 ) ? 1 : 0 );

if ( $rh_code->{codelist_id} > 0 ) {
my $sth_lookup = $dbh->prepare( $stmt_lookup{benefits_codes}{codelist} );
$sth_lookup->execute( $rh_code->{codelist_id} ); #or warn $dbh->errstr();
while ( my $ra_benefits_codes = $sth_lookup->fetchrow_arrayref() ) {
push @{$rh_template_variables->{code_lookup}{$shop_abbr}{actions}}, {


type => $ra_benefits_codes->[0] || '',


percent => $ra_benefits_codes->[1] || '',


amount => $ra_benefits_codes->[2] || '',


currency => $ra_benefits_codes->[3] || '',


start_date => $ra_benefits_codes->[4] || '',


end_date => $ra_benefits_codes->[5] || '',


description_de => $ra_benefits_codes->[6] || '',


is_used => $ra_benefits_codes->[7] || '',


codelist_id => $rh_code->{codelist_id}


};
}
}
} else {
delete $rh_template_variables->{code_lookup}{$shop_abbr};
}

### lookup in benefits_codes über coupon_code
my $sth_lookup = $dbh->prepare( $stmt_lookup{benefits_codes}{code} );
$sth_lookup->execute( $coupon_code );
while ( my $ra_benefits_codes = $sth_lookup->fetchrow_arrayref() ) {
$found_something = 1;
push @{$rh_template_variables->{code_lookup}{$shop_abbr}{actions}}, {


type => $ra_benefits_codes->[0] || '',


percent => $ra_benefits_codes->[1] || '',


amount => $ra_benefits_codes->[2] || '',


currency => $ra_benefits_codes->[3] || '',


start_date => $ra_benefits_codes->[4] || '',


end_date => $ra_benefits_codes->[5] || '',


description_de => $ra_benefits_codes->[6] || '',


is_used => $ra_benefits_codes->[7] || '',


codelist_id => ''


};
}
$dbh->disconnect();
}
# wenn Variable = 0 oder undefiniert dann gib Fehlermeldung aus!
unless( $found_something ) {
$rh_template_variables->{error} = 'nothing_found';
}
}
\n\n

<!--EDIT|renee|1155128195-->
topeg
 2006-08-09 22:24
#68799 #68799
User since
2006-07-10
2611 Artikel
BenutzerIn

user image
Die einfache Variante:
Speicher das als Datei "show.pm" im selben Ordner wie das Script.
Code: (dl )
1
2
3
4
5
6
package show;
sub show($$)
{
# ...
}
1;

Der Import erfolgt so:
Code: (dl )
require show;

die Funktion nutzt du dann so:
Code: (dl )
show::show($myCGI,\%vars);


Eine Stufe komplexer:
Speicher das als Datei "show.pm" im selben Ordner wie das Script.
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
package show;
use vars qw(@ISA @EXPORT @EXPORT_OK);
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(show);
@EXPORT_OK = qw();

sub show($$)
{
# ...
}
1;

Der Import erfolgt so:
Code: (dl )
use show;

die Funktion nutzt du dann so:
Code: (dl )
show($myCGI,\%vars);


Noch mal komplexer:(objektorientiert)
Speicher das als Datei "show.pm" im selben Ordner wie das Script.
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package show;
use vars qw(@ISA @EXPORT @EXPORT_OK);
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(new);
@EXPORT_OK = qw();

sub new()
{
my $self=shift;
my %vars=@_;
my $obj=\%vars;
bless($obj,$self);
return $obj;
}

sub show($$$)
{
my $obj=shift(@_);
# ...
}
1;

Der Import erfolgt so:
Code: (dl )
1
2
use show;
my $myshow=show->new();

die Funktion nutzt du dann so:
Code: (dl )
$myshow->show($myCGI,\%vars);


Eine Bemerkung zu Modulpfaden.
Für Perl ist beim Import von Modulen ein "::" gleichbedeutend mit einmen "/" im Dateipfad. Bei einem "use module::anfang::show" sucht Perl "./module/anfang/show.pm"

Aber das kannst du dir auch im Internet durchlesen. Fast alle Dokus zu Perl haben ein entprechendes Kapitel.

Edit:
Habe den Fehler behoben. Danke betterworld habe ich einfach übersehen. :-)\n\n

<!--EDIT|topeg|1155182438-->
betterworld
 2006-08-10 00:35
#68800 #68800
User since
2003-08-21
2613 Artikel
ModeratorIn

user image
[quote=topeg,09.08.2006, 20:24]Der Import erfolgt so:
Code: (dl )
1
2
use show;
my $myshow=show::new();

die Funktion nutzt du dann so:
Code: (dl )
$myshow->show($myCGI,\%vars);
[/quote]
Das muesste aber show->new(); statt show::new(); heißen. Ansonsten wird das $self (was eigentlich besser $class heißen sollte) in der Subroutine "new" undefiniert sein, was dazu fuehrt, dass der Aufruf $myshow->show nicht funktioniert.

Ich wuerde die Datei auch besser "Show.pm" statt "show.pm" nennen und entsprechend "package Show;" statt "package show;" schreiben. Denn kleingeschriebene Paketnamen sind fuer Perl-interne Module.
MartinR
 2006-08-10 00:45
#68801 #68801
User since
2004-06-17
305 Artikel
BenutzerIn
[default_avatar]
Kann mir bitte jemand die Funktion der zwei bzw. drei Dollarzeichen erklären in sub show($$) bzw. sub show($$$)?

Danke\n\n

<!--EDIT|MartinR|1155156418-->
sid burn
 2006-08-10 02:07
#68802 #68802
User since
2006-03-29
1520 Artikel
BenutzerIn

user image
[quote=MartinR,09.Aug..2006, 22:45]Kann mir bitte jemand die Funktion der zwei bzw. drei Dollarzeichen erklären in sub show($$) bzw. sub show($$$)?

Danke[/quote]
Das sind sogenannte Prototypes. Zum einen kannst du damit verhindern das deine Argumentenliste abgeflacht werden. Zum anderen kannst du damit erzwingen was deiner Funktion übergeben werden muss.

show($$)

muss also immer 2 Arguemente übergeben werden, wenn du weniger oder mehr als 2 Argumente übergibst, gibt es beim Kompilieren des Skriptes schon eine Fehlermeldung.

"show(1)" würde als Aufruf in deinem Skript z.B. nicht klappen.


Ansonsten kannst du damit z.B. verhindern das die Argumentenliste abgeflacht wird und alles in "@_" landet. Jedenfalls wird das nur gemacht sofern du deine Subroutine nicht mit einem & aufrufst. Also &show(...).

Wenn du z.B. folgendes Schreibst:

mypush(\@@)

dann Arbeitet die Subroutine genauso wie das eingebaute "push". Du kannst also "mypush(@array1, @array2, $wert)" schreiben. Allerdings landen die Werte der Arrays nicht alle in @_. Sondern von @array1 wird eine Referenz erzeugt und diese dann @_ hinzugefügt. Der Rest ist dann normal und die Inhalte von @array2 und $wert würde in @_ landen.

http://perldoc.perl.org/perlsub.html\n\n

<!--EDIT|sid burn|1155161379-->
Nicht mehr aktiv. Bei Kontakt: ICQ: 404181669 E-Mail: perl@david-raab.de
pq
 2006-08-10 11:05
#68803 #68803
User since
2003-08-04
12208 Artikel
Admin1
[Homepage]
user image
@sid burn: prototypen haben aber bei objektorientierung keinerlei
wirkung. sollte man vielleicht noch erwähnen.
also bei $obj->show(...) wird der prototyp schlichtweg ignoriert.
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem
nepos
 2006-08-10 11:38
#68804 #68804
User since
2005-08-17
1420 Artikel
BenutzerIn
[Homepage] [default_avatar]
Gut zu wissen :)
MartinR
 2006-08-10 12:27
#68805 #68805
User since
2004-06-17
305 Artikel
BenutzerIn
[default_avatar]
Danke schön ....
<< >> 8 Einträge, 1 Seite



View all threads created 2006-08-09 16:44.