in reply to Passing value outside of loop

my() creates a variable in the current lexical scope. Since the "my $file" is in the while loop's condition, $file is created in the while loop and then goes away. Try this:

my $file; while($file = readdir(DIR)) { ...

Also, unless you have a bunch of other code in the while loop that needs to run, you should exit the loop once $bool becomes true (BTW--$bool is a really lousy name for a variable). Additionally, it is usually good practice in a regex match to use '^' and '$' to indicate line beginnings/ends, and '\A' and '\z' to indicate string beginnings/ends. This is not a strict requirement, and you'll usually be OK if you switch them around.

my $file; while ($file = readdir(DIR)) { if($file =~ /\Amyname\.doc\z/i) { $bool = 1; last; } }