(e-mail address removed) said:
I sort of see who that works. Are you saying that to have the values
in an 'include' change when it is executed more than once, I need to
basically make that include a packaged subroutine?
If your code all is in a single file/package anyway, all you need is
a subroutine declared within that file. So, you don't even need the
use/require syntax. If you're setting up a multi-file program, then
it'd do good to play with some simple value-passing code first to
learn what happens (and contrast your findings with the relevant
documentation).
Somehow it looks here that the mere concept of a subroutine is foreign
to you, so start with that.
But, here you are dealing with a single value. How would you code
thing that deals with arrays, and things like this:
$sel = $dbh->prepare("SELECT table_name, proper_name, acronym FROM
table_data $where");
$sel->execute() || die "Select of table names failed: ",
$dbh->errstr, "\n";
while (($table, $name, $acr) = $sel->fetchrow_array()) {
$tables{$table} = $name;
}
$sel->finish;
Let's see; apparently that is dependent on the external variables
$dbh and $where, and the results should be inserted to a hash named
%tables . $sel, $table, $name and $acr are local to the routine.
sub get_name_and_acronym()
{
my ($dbh,$where,$tables_ref)=@_;
my ($sel,$table,$name,$acr);
$sel = $dbh->prepare( ... );
...
while ( ... ) {
$tables_ref->{$table} = $name;
}
$sel->finish();
}
.... and call from surrounding code with
get_name_and_acronym($dbh,$where,\%tables);
Things to note:
- the subroutine only accesses variables declared within the subroutine
(parameters are explicitly passed through the 'my (...) = @_;'
mechanism)
- variable names within the subroutine are independent of the variable
names outside the subroutine (so, within the subroutine you could
rename $dbh to $d, $where to $w, and so on, and it would still work
the same
- all variables declared with 'my' within the subroutine are local
to the subroutine, and will not be accessible anywhere outside the
subroutine (though, you may have other variables with the same names
outside the subroutine)
- the \-sign preceding the %tables when calling the subroutine;
this'll create a reference to the named data structure, instead of
passing the structure itself, and thus the called routine can access
(even to modify) the contents of the structure
- the arrow notation for accessing hash contents through a reference
(so "$tables_ref->{$table}" because $tables_ref is a reference to
a hash)