denex has asked for the wisdom of the Perl Monks concerning the following question:

I need to create object in program (DBI module)
like this:
$dbi = new.... _________ package MyModule; ....
And use the same object in MyModule
like this:
$dbi->quote(...);

Help please!
(sorry for my bad english...)

$ perl pltests/nodeedit.pl exit

Edited: ~Tue Sep 24 18:34:13 2002 (GMT) by footpad: Added <code> tags, per Consideration.

Replies are listed 'Best First'.
Re: Help with modules!
by hiseldl (Priest) on Sep 24, 2002 at 13:56 UTC
    If you mean that you need to create your db handle in your program and use it in your module, then you can just pass the handle to your module's new method.
    #!/usr/bin/perl -w use DBI; my $dbh = new DBI->connect(...); my $pkg = new MyModule($dbh); ############################################## package MyModule; sub new { my ($dbh) = @_; my $self = (); $self{dbh} = $dbh; ... } sub MyMethod { my ($self) = @_; # use the db handle something like this $self{dbh}->quote(...); ... }

    --
    hiseldl
    "Act better than you feel"

Re: Help with modules!
by derby (Abbot) on Sep 24, 2002 at 13:49 UTC
    Without more info, it's hard to determine what you exactly want. All you need to do is pass the variable to the appropriate method.

    $dbi = new ... $mod = new MyModule(); $mod->a_method( $dbi ); .... package MyModule # different file I assume sub a_method { my( $self, $dbi ) = @_; $dbi->quote( ... ); } ...

    Granted there are a lot better styles. Depending on how many methods need a database handle, passing it to each method may not be the best practice.

    -derby