in reply to variable has value but then become empty

Hi Randy_j53,

In Perl a variable will exist until the end of the scope it's created in. That's why we create variables with my(), in order to limit them to a restricted scope (block, package, subroutine, etc).

my $foo = 'bar';
If a variable is declared inside a block, it won't be available outside the block.
#!/usr/bin/perl use strict; use warnings; sub foo { my $bar = 'baz'; } foo(); print $bar; # error
Compare with:
#!/usr/bin/perl use strict; use warnings; my $bar = 'baz'; sub foo { $bar = 'qux'; } foo(); print $bar; # prints 'qux'
Here's a PerlMonks tutorial on scoping. You'll find many more resources if you use the Super Search and look up 'scoping.'

And please, always put use strict; and use warnings; at the top of your code. These are Perl's safety features, and one of the main things they do is alert you when you are not getting variable scope right.

Hope this helps!

Edit: oops, missed the sub calls!

The way forward always starts with a minimal test.

Replies are listed 'Best First'.
A reply falls below the community's threshold of quality. You may see it by logging in.