in reply to where to declare a variable...
Well, depends on what you want to do.
I quote, perldoc -f my:
A "my" declares the listed variables to be local (lexically) to the enclosing block, file, or "eval".
#!/usr/bin/perl #Is you is, or is you ain't, my foobar? #The dope on scope. baz(); #call baz. sub baz { my $foobar="I feel Baz!\n"; print $foobar; #$foobar is now lexically scoped for #the whole subroutine block. if (1) { #This is another block, it belongs to if. my $foobar="I am the FooMan!\n"; print $foobar; #$foobar is now lexically scoped #for this if block; } print $foobar; if ("I own several pairs of pants") { #That ^^^^ is another if, and this is its block my $foobar="I am the BarMan!\n"; print $foobar; #$foobar is now lexically scoped for this if block; } print $foobar; }
Now, what I would do for your little file opener is
sub openFile { my @entries; #is now lexically scoped for the entire subroutine; if ($file1) { open FH, "$file1" or die $!; @entries = <FH>; close FH; } if ($file2) { open FH, "$file2" or die $!; @entries = <FH>; #Replace all the entries? what? close FH; } }
Get it? Also, there are many flaws with the openFile code, but I digress
Have fun!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: where to declare a variable...
by kiat (Vicar) on Dec 10, 2001 at 20:34 UTC |