CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
unable to access variable declared inside if loop

 



ajitbhaskaran
Novice

Jan 21, 2008, 9:58 PM

Post #1 of 2 (483 views)
unable to access variable declared inside if loop Can't Post

hi,

i have written a script where i need to split the file name into 4 parts and save in 4 variables, but the problem is i want to access the variable outside the if loop, when i use the variable outside the lf loop it gives the error.

Global symbol "$x" requires explicit package name at test3.pl line 38.
Execution of test3.pl aborted due to compilation errors.




My script is:

#!/usr/bin/perl

use strict;
use warnings;

my $list;
system "ls *.log >> list.out" ;
my @check_files=("list.out");
for my $check_file (@check_files)
{
if ( -f "$check_file" )
{
open(LOG_FILE,"$check_file") || die "Cannot open $check_file";
while( my $line = <LOG_FILE> )
{
my $thread_line=$line;
if ( $thread_line =~/_/)
{
my $x=$`;
my $y=$';
if ( $y =~/_/)
{
my $a=$`;
my $b=$';
if ( $b=~/_/)
{
my $p=$`;
my $q=$';
}
}
}
#my $from = "'$thread_line'";
#my $to = " ../WIP/'$thread_line'";
#my $action= " mv $from $to ";
#rename("$from", "$to");
#system($action);
my $site_code=$x;
print $site_code;

}
close(LOG_FILE);
}
}


KevinR
Veteran


Jan 22, 2008, 9:50 AM

Post #2 of 2 (476 views)
Re: [ajitbhaskaran] unable to access variable declared inside if loop [In reply to] Can't Post

You have a scoping problem. All you need to do is declare the variables so they are scoped (can be seen) by the rest of the script. Where you have :

my $list;

change it to:

my $x;
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives