[EMAIL PROTECTED] wrote:

> Hi,
> > I am doing some studies on sub modules and Lexical variables (my). > > With regards to diagram 1 below, what can I do so that the lexical $counter can count up to 4. > > Of course, one way of doing this is to change the lexical $counter into a global variable as seen in diagram 2 but I dont think this is a good idea as the sub-module will then have a mixture of both lexical (my) and global variables. > > So, are there any other ways of writing in diagram 1 so that the $counter can count up to 4?
> Thanks
> > > > ############## Diagram 1 ---- lexical $counter t###############
> use strict;
> my $anything = 0;
> > while ($anything < 5){
>   $anything +=1;
>   &testing_module ;
> }
> > > sub testing_module {
>    my $counter = ();
>    if ($counter < 5){
>      $counter += 1;
>   }
> }
> > ###########################################
> ############# Diagram 2  --------  global $counter below 
######################
> use strict;
> my $anything = 0;
> my $counter = (); #global counter
> > while ($anything < 5){
>   $anything +=1;
>   &testing_module ;
> }
> > > sub testing_module {
>    $counter = ();
>    if ($counter < 5){
>      $counter += 1;
>   }
> }
I'd reckon that a while/for loop would do just as well.

sub testing_module {
        #You could assign counter to 0 here, but in a for loop not needed.
        for(my $counter;$counter < 5; $counter++) {
                # Do things with $counter..
        }
        # while($counter < 5) { $counter++ } # this also would work.
}

--START NOTE--
I didn't include the first loop because I didn't see why it'd make a difference 
:P
The counter was all you were asking about, so that's all I worried about.
--END NOTE--

TMTOWTDI,
        bloo`


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to