Home > Archive > PERL Beginners > June 2007 > Inherit a database connection object
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Inherit a database connection object
|
|
| Patrik Hasibuan 2007-06-24, 6:59 pm |
| Dear all,
I am still new in Perl especially in its OOP concept.
I want to write once a script as a part of building a connection to my MySQL DB server. The parts only use a kind of vabiable such as "$dbh". Is it possible?
================
main.pl
================
#!/usr/bin/perl -w
use iterdir;
$namadir=iterdir->baru;
$namadir=iterdir->bukadir;
================
================
package kueri;
use DBI;
sub baru{
my $kelas = shift;
}
sub konek{
$dbh=DBI->connect("DBI:mysql:arsipan;localhost:3306","root","dearest") or die "gagal konek ke mysql";
}
1;
================
package iterdir;
use kueri;
sub baru{
my $kelas = shift;
}
sub bukadir{
my $kelas = shift;
$rootdir="/home/patrikh/sementara/tes";
opendir("dirku", "$rootdir");
$statusp=chdir($rootdir);
if ($statusp){
print "berhasil membuka direktori-->$rootdir\n";
while ($entridir=readdir("dirku")){
print "$entridir\n";
$sqlku=kueri->baru;
$sqlku=kueri->konek;
$sth=$dbh->prepare("insert into tblarsip (location, informasi) values ('$entridir', '')");
$sth->execute;
$sth->finish;
}
} else{
print "gagal membuka direktori yang diinginkan: $rootdir\n";
exit 1;
}
}
1;
================
The error message is:
"
Can't call method "prepare" on an undefined value at iterdir.pm line 22.
"
Please tell me how a class inherits connection object to the another class.
Thank you very much in advance.
--
Patrik Hasibuan <patrikh@penguin-teknologi.com>
Junior Programmer
| |
| Tom Phoenix 2007-06-24, 6:59 pm |
| On 6/24/07, Patrik Hasibuan <patrikh@penguin-teknologi.com> wrote:
> I want to write once a script as a part of building a connection to my MySQL
> DB server. The parts only use a kind of vabiable such as "$dbh". Is it possible?
It's possible. In fact, that's what most of us do with objects.
> #!/usr/bin/perl -w
That's a good start. You could also use "use warnings" instead of -w,
and adding "use strict" will prevent some common mistakes.
use strict;
use warnings;
> use iterdir;
What's this? Module names in all lower case are reserved for pragmas.
In the privacy of your own home directory, of course, you can name
things whatever you want. But normal modules, and the package names
they use, begin with a capital letter.
> Can't call method "prepare" on an undefined value at iterdir.pm line 22.
That means that the expression to the left of "->prepare" near line 22
turned out to be undef instead of an object.
> Please tell me how a class inherits connection object to the another class.
Perl objects use the @ISA mechanism, documented in the perlobj manpage.
http://search.cpan.org/~nwclark/per...pod/perlobj.pod
But it may help to start in the barnyard:
http://search.cpan.org/~nwclark/per...od/perlboot.pod
Hope this helps!
--Tom Phoenix
Stonehenge Perl Training
| |
| Patrik Hasibuan 2007-06-25, 7:59 am |
| Hi Tom...
Thank you very much for your help.
Now, it's time for me to learn perl from:
http://search.cpan.org/~nwclark/per...pod/perlobj.pod
http://search.cpan.org/~nwclark/per...od/perlboot.pod
........as your advise.
I am going be back again to you in this mailing-list afterall if I still can not solve the problem.
Thank you very much again, Tom.
===
On Sun, 24 Jun 2007 11:33:52 -0700
"Tom Phoenix" <tom@stonehenge.com> wrote:
> On 6/24/07, Patrik Hasibuan <patrikh@penguin-teknologi.com> wrote:
>
>
> It's possible. In fact, that's what most of us do with objects.
>
>
> That's a good start. You could also use "use warnings" instead of -w,
> and adding "use strict" will prevent some common mistakes.
>
> use strict;
> use warnings;
>
>
> What's this? Module names in all lower case are reserved for pragmas.
> In the privacy of your own home directory, of course, you can name
> things whatever you want. But normal modules, and the package names
> they use, begin with a capital letter.
>
>
> That means that the expression to the left of "->prepare" near line 22
> turned out to be undef instead of an object.
>
>
> Perl objects use the @ISA mechanism, documented in the perlobj manpage.
>
> http://search.cpan.org/~nwclark/per...pod/perlobj.pod
>
> But it may help to start in the barnyard:
>
> http://search.cpan.org/~nwclark/per...od/perlboot.pod
>
> Hope this helps!
>
> --Tom Phoenix
> Stonehenge Perl Training
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>
>
--
Patrik Hasibuan <patrikh@penguin-teknologi.com>
Junior Programmer
| |
| Patrik Hasibuan 2007-06-27, 4:00 am |
| Dear my friend, Tom Phoenix...
I read the reference you told me. And now I am informing you that my problem is solved. I found the solution in the manuals.
Thank you very...very....much.
====
On Sun, 24 Jun 2007 11:33:52 -0700
"Tom Phoenix" <tom@stonehenge.com> wrote:
> On 6/24/07, Patrik Hasibuan <patrikh@penguin-teknologi.com> wrote:
>
>
> It's possible. In fact, that's what most of us do with objects.
>
>
> That's a good start. You could also use "use warnings" instead of -w,
> and adding "use strict" will prevent some common mistakes.
>
> use strict;
> use warnings;
>
>
> What's this? Module names in all lower case are reserved for pragmas.
> In the privacy of your own home directory, of course, you can name
> things whatever you want. But normal modules, and the package names
> they use, begin with a capital letter.
>
>
> That means that the expression to the left of "->prepare" near line 22
> turned out to be undef instead of an object.
>
>
> Perl objects use the @ISA mechanism, documented in the perlobj manpage.
>
> http://search.cpan.org/~nwclark/per...pod/perlobj.pod
>
> But it may help to start in the barnyard:
>
> http://search.cpan.org/~nwclark/per...od/perlboot.pod
>
> Hope this helps!
>
> --Tom Phoenix
> Stonehenge Perl Training
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>
>
--
Patrik Hasibuan <patrikh@penguin-teknologi.com>
Junior Programmer
|
|
|
|
|