[EMAIL PROTECTED] wrote:
I am doing some studies on sub modules and Lexical variables (my).

What you call "sub modules" are usually called "subroutines" (or, sometimes, "functions").

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

What you call a "global variable" is a file scoped, lexical variable since it's my() declared.

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.

No, they are both lexical, but in different scopes.

Guess you'd better learn to live with that. A counter that cannot be accessed from anywhere, but the subroutine that increments it, is rather pointless, isn't it?


############# Diagram 2  --------  global $counter below
use strict;

    use warnings;

my $anything = 0;
my $counter = (); #global counter

    my $counter = 0;

while ($anything < 5){
  $anything +=1;
  &testing_module ;
}

sub testing_module {
   $counter = ();

Get rid of that latter statement.

   if ($counter < 5){
     $counter += 1;
  }
}

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

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


Reply via email to