Solved

what's wrong with this short program?

Posted on 2001-09-17
5
136 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
psogaa earned 50 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

Technology Partners: 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

Suggested Solutions

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

756 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