3 Replies - 692 Views - Last Post: 30 October 2009 - 05:52 AM

#1 Mila  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 33
  • View blog
  • Posts: 193
  • Joined: 28-October 06

File IO in Perl

Posted 29 October 2009 - 07:42 PM

Hi all, Perl newbie here with a very newbie-ish question.

So I'm writing a small program to read a file and print to the screen. The file, called abbrevs.abr, is a text file with the following lines of text:
United States,US
Poland,PL
United Kingdom,UK
Canada,CA


My code is as follows:
#!/usr/bin/perl -w
print "What is the filename? ";
$filename = <STDIN>;

# load abbreviations
open(ABBR, $filename) || die ("Could not open file.");
@raw_data = <ABBR>;
close(ABBR);
print "The abbreviation list:\n";
foreach $abbreviation (@raw_data) {
	chomp($abbreviation);
	($fullname, $abbrev) = split(/,/, $abbreviation);
	print "$fullname abbreviates to $abbrev\n";
}


What it should do is print a series of lines that says something like "United States abbreviates to US" and so forth.

It doesn't.

It properly prompts for the filename, the file is in the same folder as the .pl file, it prints "The abbreviation list:" and then finishes. No errors, no nothing.

It's probably a very stupid little error, since I'm trying to teach myself this language out of a book that's 15 years old and the internet.

I also tried this, just to straight out print the information from the file:
#!/usr/bin/perl -w
print "What is the filename? ";
$filename = <STDIN>;

# load abbreviations
open(ABBR, $filename);
print "The abbreviation list:\n";
while (<ABBR>) {
	chomp;
	print "Line: $_\n";
}


That gave the same result as the other. What am I doing wrong? Since I'm running Xubuntu, I thought maybe I'd have to run it as root/sudo, but that made no difference either.

Thanks in advance,
Mila

This post has been edited by Mila: 29 October 2009 - 07:42 PM


Is This A Good Question/Topic? 0
  • +

Replies To: File IO in Perl

#2 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4466
  • View blog
  • Posts: 24,916
  • Joined: 10-May 07

Re: File IO in Perl

Posted 29 October 2009 - 07:46 PM

Are you actually trying to open a file? Because you have standard in for the file name...

Maybe if you replace this line :

$filename = <STDIN>;



With the text name of the file ...

Read a file, display the results
Was This Post Helpful? 0
  • +
  • -

#3 Mila  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 33
  • View blog
  • Posts: 193
  • Joined: 28-October 06

Re: File IO in Perl

Posted 29 October 2009 - 07:51 PM

Well that's what traps the user's input, yes? So the user could type "abbrev.abr" and that's what would be saved as $filename... or am I totally wrong?

I also tried hard-coding the filename in, and that didn't work either.

And I tried the code from this snipper, substituting my filename for "data.txt" and that didn't work either. Is it maybe a problem with my perl installation?

This post has been edited by Mila: 29 October 2009 - 07:54 PM

Was This Post Helpful? 0
  • +
  • -

#4 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 220
  • View blog
  • Posts: 636
  • Joined: 29-March 09

Re: File IO in Perl

Posted 30 October 2009 - 05:52 AM

View PostMila, on 30 Oct, 2009 - 02:42 AM, said:

open(ABBR, $filename) || die ("Could not open file.");

You've forgotten to tell Perl what to do with the file (read/write/append) that you're opening. Try
open(ABBR, '<', $filename) || die...
instead.

Also take a look at my reply to CatchThi5Drift's question at http://www.dreaminco...topic134769.htm for a lot of additional general Perl advice, most of which applies to the code you've posted here, too.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1