global variables

W

worlman385

if I call a perl function 26 times,

are the variables $decode, $flag global variables?

I thought they are local variables like methods in Java, once the
function returns, $decode , $flag will get destroy.

But when i look into komodo debugger they are global variables.

How can i use them as local variables like method variables in Java?

Thanks



for ( $i = 1; $i <= 26; $i++)
{
decode($i);
}

sub decode
{
$decode = "";
$flag = $_[0];
if ( $flag != undef ) {
$data_file="brute.txt";
} else {
$data_file="encoded.txt";
$flag = 3;
}
open(DAT, $data_file) || die("Could not open encoded.txt!");
@raw=<DAT>;
close(DAT);
}
 
K

kens

if I call a perl function 26 times,

are the variables $decode, $flag global variables?

I thought they are local variables like methods in Java, once the
function returns, $decode , $flag will get destroy.

But when i look into komodo debugger they are global variables.

How can i use them as local variables like method variables in Java?

Thanks

for ( $i = 1; $i <= 26; $i++)
{
decode($i);

}

sub decode
{
$decode = "";
$flag = $_[0];
if ( $flag != undef ) {
$data_file="brute.txt";
} else {
$data_file="encoded.txt";
$flag = 3;
}
open(DAT, $data_file) || die("Could not open encoded.txt!");
@raw=<DAT>;
close(DAT);

}

No, they are not 'local' variables. They are global to the package
they are used in.

Use a lexical variable (declared with the 'my' keyword to limit a
variable's scope.

For instance:

sub decode
{
# This variables are only scoped for subroutine
# decode.
my $decode = "";
my $flag = $_[0];
...

Note that you should always start your programs with the following two
lines:

use strict;
use warnings;

Using strict will force you to define all variables, thus, they will
not default to be a package global. It will alos save you from
misspelling variable names (at least part of the time - you could
misspell it to the name of another variable I suppose).

HTH, Ken
 
J

Joe Smith

if I call a perl function 26 times,
are the variables $decode, $flag global variables?

All variables are global unless explicitly specified otherwise.
I thought they are local variables like methods in Java, once the
function returns, $decode , $flag will get destroy.
But when i look into komodo debugger they are global variables.

How can i use them as local variables like method variables in Java?

To do that in perl is sort of like Java, in that such variables need
to be explicitly declared. The keyword to do this is "my".

my $decode = "";
my $flag = $_[0];
for my $i (1 .. 26) {...};

Do not be mislead by "local" in perl. It refers to a global variable
with a local value. Instead, put "use strict; use warnings;" near the
top of the file to have perl help you make sure all variables are
properly declared.

-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top