txholdem
asked on
what's wrong with this short program?
when I do "perl -w test.pl, I get a warning: "use of uninnitialized value in string ne at test.pl line 8, <IN_FILE> line 2. what's the problem?
here is test.pl
-------------------------
print "Enter a file name: ";
chomp($infile=<STDIN>);
open(IN_FILE, $infile) || die "cannot open $infile";
$startDir=<IN_FILE>;
while($startDir ne ""){
chop ($startDir);
print "the dir is: $startDir\n";
$startDir=<IN_FILE>;
} # while not EOF..
here is test.pl
-------------------------
print "Enter a file name: ";
chomp($infile=<STDIN>);
open(IN_FILE, $infile) || die "cannot open $infile";
$startDir=<IN_FILE>;
while($startDir ne ""){
chop ($startDir);
print "the dir is: $startDir\n";
$startDir=<IN_FILE>;
} # while not EOF..
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It means that it removes the newline character (or whatever character assigned to $/) at the end of the line. In your case, i'm not sure you need it.
ASKER
how do I know what is assigned to '$/'? I tried to print $/, but it didn't print anything..
$/ is always set to a newline ( \n ) as default.
Try this:
print "test$/test";
You'll see that it prints test on 2 lines to confirm $/ is actually set to a newline.
Normally you don't want to change the value of $/.
However, it can be useful sometimes.
Say you wanted to read a whole file into a variable.
Normally when you do $variable = <FILE> it will put the first line into $variable.
Now if you do this:
undef( $/ );
$variable = <FILE>
$variable will now have the whole file content.
Normally it's not considered good programming to just change perl's predefined variables without cleaning up after you, so you should do the undef inside a local scope:
{
undef( $/);
$variable = <FILE>
}
$/ is now back to what it was outside the local scope( {} ) above.
Try this:
print "test$/test";
You'll see that it prints test on 2 lines to confirm $/ is actually set to a newline.
Normally you don't want to change the value of $/.
However, it can be useful sometimes.
Say you wanted to read a whole file into a variable.
Normally when you do $variable = <FILE> it will put the first line into $variable.
Now if you do this:
undef( $/ );
$variable = <FILE>
$variable will now have the whole file content.
Normally it's not considered good programming to just change perl's predefined variables without cleaning up after you, so you should do the undef inside a local scope:
{
undef( $/);
$variable = <FILE>
}
$/ is now back to what it was outside the local scope( {} ) above.
ASKER