in reply to Using External Subroutines

OK, Well...I wont post all of it because it is huge, but here is an example: config file
sub check { $statement = 'SELECT * FROM users'; $dbh = DBI->connect("DBI:mysql:$DBNAME:localhost","user","pass"); $sth = $dbh->prepare($statement); $sth->execute(); while(my $ref = $sth->fetchrow_hashref()) { $address= $ref->{'add'}; $phone= $ref->{'phone'}; $password= $ref->{'pass'}; } }

The main script then has the following sort of set-up:
require "config.pl"; check(); print "$name";
Thats not the actual files but it will give you an idea of what i am doing, the $name would produce a -w error saying used only once

Replies are listed 'Best First'.
Re: Re: Using External Subroutines
by data64 (Chaplain) on Nov 19, 2001 at 01:50 UTC
    Your config.pl should be made into a module and $name needs to be exported from config so that it is available in the calling script.
    For more information see perlman:lib:Exporter, perlman:perlmodlib and perlman:perlmod.

    So you will need to add code like
    package ModuleName; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(...); # symbols to export by default

    @EXPORT should contain the name of the variables and the subroutines you want to be able to access from calling scripts.
    Also, your calling script should do a use ModuleName; rather than require ModuleName;
      is that really the only way to do it? no way to make variables global or anything ?
        well, if you want to do this and really, really want to avoid creating a proper module using exporter....

        You can do something like this:
        $name = \config::name;
        in your calling script. Note that $name cannot be declared by "my" in config.pl

        Again, I want to stress I would never use something like this. It is some much easier, simpler and safer to convert config.pl into a module and then use exporter to expose whatever you need.
        Of course you can,
        but maybe you'll get in trouble with global variables.
        For example, if you decide not to use strict and warnings, you won't declare your variables...

        You want this?
        Of course I don't in a "huge" set of scripts

        Cheers
        Hopes