Using 'my $variable_name' is kinda like a declaration of the variable that
tells perl the scope of the variable. So if you do my $var1 at the root
level of a file, the variable will be accessible throughout the entire
file.. or like in the problem you ran into, if you declare my $var2 inside a
while loop, its scope is only inside the while loop. Once you get used to
it, it will be like second nature.

The practice of putting 'my $varname' outside the while loop to get the
scoping you want is the apropriate solution, ommiting the 'my' part would
create a global variable and that probably isnt a good thing.

ryan

> Hi, I'm completly confused by this my(X) not being available outside of a
> subroutine, I've read everything but must not still get it....is this an
> approriate solution? it makes @countries available...
>
>  my @countries; <- solution?
>  my $sql = "select distinct country from geo";
>  my $sth = $match::dbh->prepare( "$sql" );
>  $sth->execute;
>  while ( (my $country_db) = $sth->fetchrow )
>  {
>   push(@countries,"$country_db");
>  }
>
> I've read the solutions but dang if they don't seem like rocket science
for
> something simple. Like suppose I wanted to do this
> if ($var eq "whatever) {
>     $sql = "select.....yada";
> } else {
>     $sql="somthing else...";
> }
> dbi $sql stuff
> In this case $sql wouldn't be available and use strict would complain. So
if
> I did this:
> my ($sql);
> if....as above
>
> Is that an appropiate solution?
>
> Thanks for _any_ info anyone can provide,
> shawn
>
>

Reply via email to