Leser: 3
|< 1 2 3 >| | 29 Einträge, 3 Seiten |
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
sub new
{
#Arg0 is the type because the constructor will look like
# my($instance) = Tree->new(arg1,arg2,whatever)
#so arg0 will be Tree.
my($type) = $_[0];
#Make subroutine-local var $self, and make it a reference.
#Specifically, make it a reference to a (right now) empty hash.
#Later on, that hash will contain object properties.
my($self) = {};
#For now, we'll have one instance variable (property, whatever)
#It will be in the hash referenced by $self, and will have
#the index 'root'. This will be the first arg (inside the parentheses)
#of the call to the constructor in the main program.
$self->{'root'} = $_[1]; #remember $_[0] was the Tree before the ->
#There's nothing reserved about the word $self. It could have been
#called $oodolaboodola. To link the object with both the hash pointed
#to by $self and the type (Tree), we use the 2 argument version
#of the keyword bless:
bless($self, $type);
#Now finally, return the hash as a reference to be used as an "object"
return($self);
}
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
package MyMysql;
use strict;
use DBI;
sub new {
my $class = shift;
my $self = {}
my $self->{db} = "mydatabase";
my $self->{host} = "localhost";
my $self->{user} = "root";
my $self->{dsn} = "DBI:mysql:$db:$host";
my $self->{attributes} = { PrintError => 0, RaiseError => 0 };
return bless $self, $class;}
sub add_user(){
my $self=shift;
my $self->{nickname} =$_[0];
my $self->{password} =$_[1];
my $self->{firstname} =$_[2];
my $self->{name} =$_[3];
my $self->{gender} =$_[4];
my $self->{birthdate} =$_[5];
my $self->{dbh} = DBI->connect($self->{dsn},$self->{user},undef,
$self->{attributes}) or
$self->WriteError('',"Error in connecting to $self->{dsn}", die => 1); )
$self->{nickname} = $dbh->quote($self->{nickname});
$slef->{password} = $dbh->quote($self->{password});}
1
2
3
4
5
6
7
8
9
subb add_user{
my ($self,@data) = @_;
my $i = 0;
for(qw/nickname password firstname name gender birthday/){
$self->{$_} = $dbh->quote($data[$i]);
++$i;
}
# connect;
}
QuoteBei Perl brauchst du einen Konstruktor um das Objekt zu erzeugen.
QuoteUnd du brauchst einen Konstruktor um ein Objekt zu initialisieren (grundeinstellungen laden z.b.).
|< 1 2 3 >| | 29 Einträge, 3 Seiten |