A small Perl problem

A

Ali

Hi,

I have this small program and its giving error at file handler when I
put -w option like /urs/bin/perl -w
Program is as following

/usr/bin/perl -w

sub ReadInputFile {
my $FileName = $_;
my @lines;
open(FILE_H, "./ $FileName") or die "Oopp";
@lines = <FILE_H>;
close(FILE_H);
return @lines;
}

my $File = "test";
my @P_lines = ReadInputFile($File);

I am using Perl 5.005.

Thanks
 
G

gnari

Ali said:
I have this small program and its giving error at file handler when I
put -w option like /urs/bin/perl -w
Program is as following

/usr/bin/perl -w

you probably mean:
#!/usr/bin/perl -w
sub ReadInputFile {
my $FileName = $_;

you probably want one of:
my ($FileName) = @_;
my $FileName = $_[0];
my $FileName = shift;
my @lines;
open(FILE_H, "./ $FileName") or die "Oopp";

from here, it looks like there is a space after the ./
also, you might want to use a more helpful error message
open(FILE_H, "< ./$FileName") or die "could not open
'./$FileName': $!";
I am using Perl 5.005.

consider upgrading

gnari
 
M

Michele Dondi

Hi,
Hi!

I have this small program and its giving error at file handler when I
put -w option like /urs/bin/perl -w

First of all, better

use warnings;

nowadays...
Program is as following

/usr/bin/perl -w

This can't be your program. Don't retype code, paste it.
sub ReadInputFile {
my $FileName = $_;

Isn't it that you mean

my $FileName = $_[0];

instead?
my @lines;
open(FILE_H, "./ $FileName") or die "Oopp";
^
^

Better use lexical FHs nowadays:

open my $fh, '<', $FileName or die "Oopps: $!";

Of course this is not exactly the same as you wrote. But I doubt that
you *do* want *that* space. And if you don't want it, then "./" is not
needed either.
@lines = <FILE_H>;
close(FILE_H);

No need for this if using lexical FHs
return @lines;
}

All in all this may have been

my $File = "test";
my @P_lines = ReadInputFile($File);

I am using Perl 5.005.

D'Oh! Some of the suggestions I gave you may not be adequate. Better
use a more recent perl, anyway.


PS: don't take this as a personal attack, but do a favor to yourself
and read some basic introduction to Perl...


Michele
 
W

wana

Ali said:
Hi,

I have this small program and its giving error at file handler when I
put -w option like /urs/bin/perl -w
Program is as following

/usr/bin/perl -w

sub ReadInputFile {
my $FileName = $_;
my @lines;
open(FILE_H, "./ $FileName") or die "Oopp";
@lines = <FILE_H>;
close(FILE_H);
return @lines;
}

my $File = "test";
my @P_lines = ReadInputFile($File);

I am using Perl 5.005.

Thanks

Both the local and remote server I am working on use version 5.8.3. I think
you really should upgrade.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,432
Latest member
GTRNorbert

Latest Threads

Top