?
Solved

what's wrong with this short program?

Posted on 2001-09-17
5
Medium Priority
?
143 Views
Last Modified: 2010-03-05
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..
0
Comment
Question by:txholdem
  • 3
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
psogaa earned 200 total points
ID: 6488578
It seems to complain when you use <IN_FILE> more than once. Not sure why though...

But if you want to read each line, chop it, and then print it out, why not do like this:?

print "Enter a file name: ";
chomp($infile=<STDIN>);
open(IN_FILE, $infile) || die "cannot open $infile";
while(<IN_FILE>){
          chop;
          print "the dir is: $_\n";
}
close( IN_FILE );
0
 
LVL 1

Author Comment

by:txholdem
ID: 6488733
Fine. Can you tell me why I need the 'chomp'-line? According to the book, ".. removes input line separator defined by $/ system variable.." What does this mean?
0
 
LVL 2

Expert Comment

by:psogaa
ID: 6488748
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.
0
 
LVL 1

Author Comment

by:txholdem
ID: 6488771
how do I know what is assigned to '$/'? I tried to print $/, but it didn't print anything..
0
 
LVL 2

Expert Comment

by:psogaa
ID: 6488810
$/ 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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question