Re: perl


Subject: Re: perl
From: Greg Jetter (greg@lazymountain.com)
Date: Tue Apr 16 2002 - 22:56:05 AKDT


On Tuesday 16 April 2002 05:10 pm, Chris Hamilton wrote:
> Thanks, sounds like I need to learn about modules. Here is what I am
> trying to do... I want to have a script/module that contains all of my
> database connection parameters and will make these parameters available to
> the calling script.
>
> Here are the connection parameters that I want to include in a separate
> script/module:
>
> my $serverName = "localhost";
> my $serverPort = "3306";
> my $serverUser = "johndoe";
> my $serverPass = "slick";
> my $serverDb = "DBName";
> my $serverTabl = "TableName";
>
> That's it. When I use "require" to try and include a script containing the
> info above, I get nothing (no errors or anything, just the html stuff).
> When I use require to make "Hello world" print in the browser it works. Is
> there something special about passing variables that I should know about.
> I bought a perl for dummies book today. So hopefully my days of being a
> dummy are numbered. I saw Arthurs book there too, but figured I would get
> lost just reading the Acknowledgments.
>
> Chris.
> ----- Original Message -----
> From: "Greg Jetter" <greg@lazymountain.com>
> To: "Chris Hamilton" <chris@digitalalaska.com>; <aklug@aklug.org>
> Sent: Tuesday, April 16, 2002 5:06 PM
> Subject: Re: perl
>
> > On Monday 15 April 2002 07:59 am, Chris Hamilton wrote:
> > > I've got a question for perl people.
> > >
> > > Inside a script I want to refer to another script (and have the
>
> variables
>
> > > carry over). I want to have a separate script that has all my database
> > > connection info, so I can refer to this script whenever connecting to a
> > > particular database. I've got the Learning Perl book, but it doesn't
> > > explain how I would do this. Any hints would be helpful. Thanks.
> > >
> > > Chris.
> > >
> > >
> > >
> > > ---------
> > > To unsubscribe, send email to <aklug-request@aklug.org>
> > > with 'unsubscribe' in the message body.
> >
> > The simple way to do it would be to put all of your data routeens in a
> > modual , make objects out of them and then when you need a routeen in
>
> nother
>
> > script , call the method out of your module andpass it data to work
>
> with.
>
> > all you got to do then is call the routreen
> >
> > Greg
> >
> > ---------
> > To unsubscribe, send email to <aklug-request@aklug.org>
> > with 'unsubscribe' in the message body.
>
> ---------
> To unsubscribe, send email to <aklug-request@aklug.org>
> with 'unsubscribe' in the message body.

hears how I would do it

create a module , name it somthing like "local_database.pm"

#!/usr/bin/perl -w
# local_database.pm
# this module contains constants for my data stuff

package local_database; # must be the same as the file name

use strict;

sub new {

       my $class = shift;
      my $self = shift;
 bless \$self ,$class;
 return \$self;
}

sub SERVERNAME {
my $serverName = "localhost";
}

sub SERVERPORT{
my $serverPort = "3306";
}
sub SERVERUSER {
my $serverUser = "johndoe";
}
sub SERVERPASS {
my $serverPass = "slick";
}
sub SERVERDB{
my $serverDb = "DBName";
}
sub SERVERTABL {
 my $serverTabl = "TableName";
}

1; # important must return true when compiled so just add 1

now you got a module , to use it just use the methods you defined

here's an example bit of code.

#!/usr/bin/perl -w

use local_database;

my $stuff = new local_database;

my $ServerName = $stuff->SERVERNAME(); # now$ServerName contains "localhost"

my $ServerPort = $stuff->SERVERPORT(); # now $ServerPort contains "3306"

you get the idea ?

not very hard once you get the notion down . each subrouteen is a method,
that can recive var's and return results , in this case were just passing
constants from one place to another. I use this method to set up local
parameters for web sites.

enjoy
Long live PERL !!!

Greg

---------
To unsubscribe, send email to <aklug-request@aklug.org>
with 'unsubscribe' in the message body.



This archive was generated by hypermail 2a23 : Tue Apr 16 2002 - 22:40:58 AKDT