Solved

what's wrong with this short program?

Posted on 2001-09-17
5
132 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 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now