If you declare a variable with my(), it only exists within the current
scope  (NOTE:  always add 'use strict' and 'use warnings' whenever you
can at the top of your scripts).

What you'll have to do is declare a variable outside of the brackets.
You could even use a subroutine, like the one below.  When it matches,
I'm returning the name of the file.  If I get through the whole loop
without matching, I'm returning 0 to indicate that it failed.

#########################

use strict;
use warnings;

my $file;

if($file = GetTestFileName()){
   print "I got it!  $file\n";
}else{
   print "Failed to locate test file!\n";
}


sub GetTestFileName{
   opendir(DIR,".") or die("Couldn't open the current directory!\n");
   my @files = readdir(DIR);
   foreach my $testfile(sort @files){
      if($testfile =~ /(.*test.*)/i){
         return $testfile;
      }
   }
   return 0;
}

##########################




-----Original Message-----
From: Curt Shaffer [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, February 28, 2006 1:11 PM
To: Timothy Johnson; beginners@perl.org
Subject: RE: regular expression in a variable

That appears to work! The part I am stuck on is how to I take that value
(which would now be $file in your example) and put it into a variable
that I
can use through the rest of the script. When I then try to use $file
outside
of that routine, it is no longer the same value.

I really appreciate your help!



<snip>



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to