|
From: worlman385 on 18 Feb 2007 19:35 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); }
From: kens on 18 Feb 2007 19:58 On Feb 18, 7:35 pm, worlman...(a)yahoo.com wrote: > 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
From: Joe Smith on 19 Feb 2007 04:29 worlman385(a)yahoo.com wrote: > 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
From: Michele Dondi on 19 Feb 2007 05:42 On Sun, 18 Feb 2007 16:35:41 -0800, worlman385(a)yahoo.com wrote: >if I call a perl function 26 times, > >are the variables $decode, $flag global variables? Variables do not change their global or local status according to the number of times a function is called. >I thought they are local variables like methods in Java, once the >function returns, $decode , $flag will get destroy. I don't know Java, but I doubt that methods are *variables* at all. >for ( $i = 1; $i <= 26; $i++) >{ > decode($i); >} for my $i (1..26) { decode($i); } or decode($_) for 1..26; >sub decode >{ > $decode = ""; > $flag = $_[0]; sub decode { my $decode = ""; my $flag = $_[0]; > if ( $flag != undef ) { > $data_file="brute.txt"; > } else { > $data_file="encoded.txt"; > $flag = 3; > } my $data_file; if ( defined $flag ) { # ... If you didn't need to also set $flag, but actually you don't use it anymore later on, you could do, more concisely my $data_file = defined $flag ? 'brute.txt' : 'encoded.txt'; > open(DAT, $data_file) || die("Could not open encoded.txt!"); open my $dat, '<', $data_file or die "Could not open `$data_file': $!\n" > @raw=<DAT>; my @raw=<$dat>; Michele -- {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB=' ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_, 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
From: Tad McClellan on 19 Feb 2007 06:53 worlman385(a)yahoo.com <worlman385(a)yahoo.com> wrote: > I thought they are local variables like methods in Java, once the > function returns, $decode , $flag will get destroy. "Coping with Scoping": http://perl.plover.com/FAQs/Namespaces.html -- Tad McClellan SGML consulting tadmc(a)augustmail.com Perl programming Fort Worth, Texas
|
Pages: 1 Prev: FAQ 4.50 How do I select a random element from an array? Next: FAQ 5.19 How can I lock a file? |