http://www.perlmonks.org?node_id=1186068

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

my %site_map = ( Bang => [ qw(rate_bang_current) ], Nor => [ qw(rate_nor_current) ], Wilming => [ qw(rate_wilming_current) ], Lime => [ qw(rate_lime_current) ], ); sub connect { $host = "dev.sp1.wilinlog.com"; $database = "rate_bang_current" ; $user = "senthom" ; $pw = "ask123"; my $dsn = "DBI:mysql:host=$host"; my $dbh = DBI->connect($dsn, $user, $pw) || die "ERROR: can't connect to database server\n"; return $dbh; $host = "dev.sp1.wilinlog.com"; $database = "rate_nor_current" ; $user = "senthom" ; $pw = "ask123"; my $dsn = "DBI:mysql:host=$host"; my $dbh = DBI->connect($dsn, $user, $pw) || die "ERROR: can't connect to database server\n"; return $dbh; $host = "dev.sp1.wilinlog.com"; $database = "rate_wilming_current" ; $user = "senthom" ; $pw = "ask123"; my $dsn = "DBI:mysql:host=$host"; my $dbh = DBI->connect($dsn, $user, $pw) || die "ERROR: can't connect to database server\n"; return $dbh; $host = "dev.sp1.wilinlog.com"; $database = "rate_lime_current" ; $user = "senthom" ; $pw = "ask123"; my $dsn = "DBI:mysql:host=$host"; my $dbh = DBI->connect($dsn, $user, $pw) || die "ERROR: can't connect to database server\n"; return $dbh; }
In the above sub routine i had tried to connect mapped database inside the subroutine.But i hardcoded manually each database.Now those databases are not fetching the contents properly because it is displaying only the rate_bang_current database contents for all other four DATABASES.How can i automatically select the database using perl without hard coding the database names and connection manually.
  • Comment on How to connect multiple databases with single sub routine connection using perl?
  • Download Code

Replies are listed 'Best First'.
Re: How to connect multiple databases with single sub routine connection using perl?
by Corion (Patriarch) on Mar 27, 2017 at 11:16 UTC

    As you are asking some sensible questions here about your code, I imagine that somebody is prompting you about your code. I think this is your employer or your teacher, so the best approach to proceed would be to ask them about guidance and to follow their guidance. If they recommend a book or course material, you should read that material and (re)do all the exercises yourself.

    If you get no guidance from them, please follow the guidance you receive here. Please tell us your situation, so we can provide advice appropriate for your level of learning.

    I think that you should read perlsub to learn about subroutine parameters.

    The traditional approach would be to pass the connection parameters to your subroutine instead of hardcoding them.

    Also, you might want to re-read what the return statement does in perlsyn.

    A good introduction to Perl is Modern Perl, which is available online for free. Subroutine parameters are explained here and here.

    For example, you could call connect with parameters:

    connect( $database, $user, $password );

    That means you need to receive the parameters in the subroutine:

    sub connect { my( $db, $user, $pw ) = @_; ... };
Re: How to connect multiple databases with single sub routine connection using perl?
by hippo (Bishop) on Mar 27, 2017 at 11:13 UTC

    Perl permits the passing of arguments to subroutines. See perlsub.

Re: How to connect multiple databases with single sub routine connection using perl?
by Anonymous Monk on Mar 27, 2017 at 15:47 UTC
    Um... do you realize that the code after the first return is unreachable? Your code only connects to one database. You're probably going to want an array of database handles.
Re: How to connect multiple databases with single sub routine connection using perl?
by Anonymous Monk on Mar 27, 2017 at 19:50 UTC
    Oh so easy just watch. You change name of subroutine to connect_multiple and add sitemap liek this: You just watch so easy! xD
    sub connect { my %site_map = ( Bang => [ qw(rate_bang_current) ], Nor => [ qw(rate_nor_current) ], Wilming => [ qw(rate_wilming_current) ], Lime => [ qw(rate_lime_current) ], ); $host = "dev.sp1.wilinlog.com"; $database = "rate_bang_current" ; $user = "senthom" ; $pw = "ask123"; my $dsn = "DBI:mysql:host=$host"; my $dbh = DBI->connect($dsn, $user, $pw) || die "ERROR: can't connect to database server\n"; return $dbh; $host = "dev.sp1.wilinlog.com"; $database = "rate_nor_current" ; $user = "senthom" ; $pw = "ask123"; my %site_map = ( Bang => [ qw(rate_bang_current) ], Nor => [ qw(rate_nor_current) ], Wilming => [ qw(rate_wilming_current) ], Lime => [ qw(rate_lime_current) ], ); my $dsn = "DBI:mysql:host=$host"; my $dbh = DBI->connect($dsn, $user, $pw) || die "ERROR: can't connect to database server\n"; return $dbh; $host = "dev.sp1.wilinlog.com"; $database = "rate_wilming_current" ; $user = "senthom" ; $pw = "ask123"; my %site_map = ( Bang => [ qw(rate_bang_current) ], Nor => [ qw(rate_nor_current) ], Wilming => [ qw(rate_wilming_current) ], Lime => [ qw(rate_lime_current) ], ); my $dsn = "DBI:mysql:host=$host"; my $dbh = DBI->connect($dsn, $user, $pw) || die "ERROR: can't connect to database server\n"; return $dbh; $host = "dev.sp1.wilinlog.com"; $database = "rate_lime_current" ; $user = "senthom" ; $pw = "ask123"; my %site_map = ( Bang => [ qw(rate_bang_current) ], Nor => [ qw(rate_nor_current) ], Wilming => [ qw(rate_wilming_current) ], Lime => [ qw(rate_lime_current) ], ); my $dsn = "DBI:mysql:host=$host"; my $dbh = DBI->connect($dsn, $user, $pw) || die "ERROR: can't connect to database server\n"; return $dbh; }
      Will it work.Becaue it will consider only one database and it will have the same database contents to other database also.
        Will it work.

        It will not.

        my $dbh = DBI->connect($dsn, $user, $pw) || die "ERROR: can't connect to database server\n"; return $dbh; $host = "dev.sp1.wilinlog.com";

        Regardless of the name of the function, it still has the problem: nothing after the first
            return $dbh;
        statement will ever be executed. The
                $host     = "dev.sp1.wilinlog.com";
        statement will never be executed. Please see return. There are some other problems, but those will only result in warnings and so are of relatively minor importance.

        My head hurts.


        Give a man a fish:  <%-{-{-{-<