Bowie, Bowie, Bowie!  Look at the code! The assignment's inside the IF and the usage is inside the ELSIF. IOW, if the if is true, the variable gets its assignment, but if the if statement is false, and the elsif statement is true, does the variable get used.  Check it out:

if ($something) {
   # assign a value to $x
   # but don't do anything with it
}
elsif ($something_else) {
   # try to do something using $x
   # but, since we ended up here, $x has no assigned value!
}
else {
   # die a horrible death
}

It just ain't gonna work this way. At all!

Deane



Bowie Bailey <[EMAIL PROTECTED]>
Sent by: [EMAIL PROTECTED]

06/20/2006 07:59 AM

       
        To:        [EMAIL PROTECTED]
        cc:        [email protected]
        Subject:        RE: accessing referenced array from subroutine



[EMAIL PROTECTED] wrote:
> I'm still fighting with that little program, mostly again with
> scoping. I tried to write it the way most of you recommended, passing
> variables to the subroutines. Now my problem is, I get a value back,
> but then it's gone:
>
> my $cgi = CGI->new();
> if ( $cgi->param('select_dir') ) {
>     my $sessionids = List_Sessions($server);

You declared this variable to be local to the block.  Once you get to
the end of the if block, it goes away.

>     # here $sessionids is filled, it's a hashref
> } elsif ( $cgi->param('select_files') ) {
>     Kill_Sessions($sessionids);
>     # this $sessionids is empty though...
> } else {
>     List_Dirs($rootpath);
> }
>
> I get the hashref back fine from List_Sessions($server), but how do I
> get it into the next elsif where I need it?

If you want the variable to exist past the end of the if block, you
need to define it outside the block.  The rule of thumb is to define
the variable in the smallest block that contains the code that uses
it.

Try it this way:

   my $cgi = CGI->new();
   my $sessionids;
   if ( $cgi->param('select_dir') ) {
       $sessionids = List_Sessions($server);
   } elsif ( $cgi->param('select_files') ) {
       Kill_Sessions($sessionids);
   } else {
       List_Dirs($rootpath);
   }

--
Bowie
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to