0

A very easy question on variables scope. I have a variable defined in the main code that I use inside a while loop.

my $my_variable;
while(<FILE>) {
   ...using $my_variable
}
if ($my_variable) -> FAILS

When I exit the loop and use the variable I get an error:

Use of uninitialized value $my_variable

Even if I enclose the variable in a naked block I follow with the error.

{
    my $my_variable;
    while(<FILE>) {
       ...using $my_variable
    }
    if ($my_variable) -> FAILS
}

Any suggestion?

4
  • 5
    Could you provide a complete example that can be copied verbatim that shows the problem? Are you using strict? Commented Mar 22, 2011 at 12:26
  • I agree with musiKk. Even more importantly are you using use strict; use warnings;? If not do it and see if it gives you some warning. Commented Mar 22, 2011 at 14:03
  • 2
    Please provide real code that can be run and exhibits the error you are seeing. The code in your question can't be run, and even if it could, it would not output the warning message in your question. Commented Mar 22, 2011 at 14:16
  • 1
    @Joel: If OP is receiving "use of uninitialized value" warnings, then he definitely has warnings turned on, at least. Commented Mar 22, 2011 at 16:03

3 Answers 3

1
  • Are you using $my_variable in the loop or did you redefine it as my $my_variable somewhere in the loop. You'd be surprised how easily a my slips in to variable assignment.

    I even have a frequent tick in my code where I write something like

     my $hash{ $key } = ... some elaborate assignment;
    
  • Also, if should not complain about an uninitialized variable. undef => Boolean false.

  • I wouldn't worry about initializing all my variables -- but its crucial that you learn a mindset about how to use undefs. And always, always USUW (use strict; use warnings) so that variables aren't uninitialized when you don't expect them to be.
Sign up to request clarification or add additional context in comments.

2 Comments

OK actually I was having a bug that was messing up the process inside my while loop. Following your suggestions I discover the bug and make the script work. Thank you all!
Defining a variable outside while loop (with my $my_variable) and using it inside the while loop without re-defining it (without "my") then it's possible to use the value of $my_variable even outside the loop.
1

Do you ever assign to $my_variable? All you show is a declaration (the my) and a usage (the if) but never an assignment.

Comments

0

In the examples given, you didn't initialize $my_variable so it is undefined after the while loop.

You can do my $my_variable = ''; just before the loop.

2 Comments

OK, so I can't try to access $my_variable (modified inside the loop) outside the loop, because is out of scope. So, I can I do it?
If you modify $my_variable inside the loop (ie assign a defined value) you can use it outside the loop. Could you show us the code inside the loop ? May be you never assign a defined value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.