closed filehandle

S

Sam

Hello
when I run the code below I get
readline() on closed filehandle DATA at practice.plx line 6.

#!/usr/bin/perl

use warnings;

use strict;

open DATA, 'home/username/data/avg' || die $!;

while (<DATA>) {

print $_;

}
 
K

ko

Sam said:
Hello
when I run the code below I get
readline() on closed filehandle DATA at practice.plx line 6.

#!/usr/bin/perl

use warnings;

use strict;

open DATA, 'home/username/data/avg' || die $!;

while (<DATA>) {

print $_;

}

You need to be specific when passing file/path names to open(). Unless
the script is running from your home directory (you passed a relative
path), it won't work. Try:

open DATA, '/home/username/data/avg' || die $!;

HTH - keith
 
J

Jürgen Exner

Sam said:
when I run the code below I get
readline() on closed filehandle DATA at practice.plx line 6.

use warnings;
use strict;
open DATA, 'home/username/data/avg' || die $!;

There are two problems in this one line:
Due to precedence rules it will be evaluated as
open DATA, ('home/username/data/avg' || die $!);

If you change this to
open DATA, 'home/username/data/avg' or die $!;
then probably you will get a runtime error "File does not exist" which
points to the second problem:
I'm guessing but probably you meant to open '/home/username/data/avg', not
just 'home/username/data/avg'.

jue
 
S

Sam

Jürgen Exner said:
There are two problems in this one line:
Due to precedence rules it will be evaluated as
open DATA, ('home/username/data/avg' || die $!);

If you change this to
open DATA, 'home/username/data/avg' or die $!;
then probably you will get a runtime error "File does not exist" which
points to the second problem:
I'm guessing but probably you meant to open '/home/username/data/avg', not
just 'home/username/data/avg'.

jue

#!/usr/bin/perl
use warnings;
use strict;

my $file = "home/user/data/avg";
open DATA, $file or die $!;
while (<DATA>) {
print $_;
}

when I run it I get
No such file or directory at practice.plx line 6.
 
A

A. Sinan Unur

#!/usr/bin/perl
use warnings;
use strict;

my $file = "home/user/data/avg";
open DATA, $file or die $!;
while (<DATA>) {
print $_;
}

when I run it I get
No such file or directory at practice.plx line 6.

You don't really read the responses apparently. Please read what Jurgen
pointed out as the second problem with the open call, and see if fixing
that solves your issue.

Sinan.
 
S

Sam

A. Sinan Unur said:
You don't really read the responses apparently. Please read what Jurgen
pointed out as the second problem with the open call, and see if fixing
that solves your issue.

Sinan.

--
A. Sinan Unur
(e-mail address removed)
Remove dashes for address
Spam bait: mailto:[email protected]

yes I did read all the posts, sorry that I didn't give that impression

*************************************
There are two problems in this one line:
Due to precedence rules it will be evaluated as
open DATA, ('home/username/data/avg' || die $!);

If you change this to
open DATA, 'home/username/data/avg' or die $!;
then probably you will get a runtime error "File does not exist" which
points to the second problem:
I'm guessing but probably you meant to open '/home/username/data/avg', not
just 'home/username/data/avg'.
********************************************

the suggestion to use the 'or' instead '||' has been tried for no avail, but
knowing the || has a precedence over 'or' so I used 'or' with my second
version of the program (open DATA, $file or die $!;)

the second suggestion 'guess' states (open '/home/username/data/avg'), well
where is the file handler DATA so I didn't understand how to implement that
solution.

thanks
 
A

A. Sinan Unur

A. Sinan Unur said:
You don't really read the responses apparently. Please read what
Jurgen pointed out as the second problem with the open call, and see
if fixing that solves your issue.

Sinan.

yes I did read all the posts, sorry that I didn't give that impression

*************************************
There are two problems in this one line:
Due to precedence rules it will be evaluated as
open DATA, ('home/username/data/avg' || die $!);

If you change this to
open DATA, 'home/username/data/avg' or die $!;
then probably you will get a runtime error "File does not exist" which
points to the second problem:
I'm guessing but probably you meant to open '/home/username/data/avg',
not just 'home/username/data/avg'.
********************************************

the suggestion to use the 'or' instead '||' has been tried for no
avail, but knowing the || has a precedence over 'or' so I used 'or'
with my second version of the program (open DATA, $file or die $!;)

the second suggestion 'guess' states (open '/home/username/data/avg'),
well where is the file handler DATA so I didn't understand how to
implement that solution.

OK, maybe this is an English problem (on your part).

I'm guessing but probably you meant to open '/home/username/data/avg',
not just 'home/username/data/avg'.
</quote>

That is an English sentence, not a Perl statement (notice the 'you meant
to open' part).

#!/usr/bin/perl

use warnings;
use strict;

# BTW, where is the actual file located?
open(DATA, '/home/user/data/avg') || die $!;
while (<DATA>) {
print;
}
 
J

Jürgen Exner

Sam said:
#!/usr/bin/perl
use warnings;
use strict;

my $file = "home/user/data/avg";
open DATA, $file or die $!;
while (<DATA>) {
print $_;
}

when I run it I get
No such file or directory at practice.plx line 6.

Well, yes. Exactly as I predicted.
Does that surprise you in any way? Did you actually read the second part of
my reply?

jue
 
S

Sam

A. Sinan Unur said:
A. Sinan Unur said:
Sam wrote:
when I run the code below I get
readline() on closed filehandle DATA at practice.plx line 6.

use warnings;
use strict;
open DATA, 'home/username/data/avg' || die $!;

<snip>

If you change this to
open DATA, 'home/username/data/avg' or die $!;
then probably you will get a runtime error "File does not exist"
which points to the second problem:
I'm guessing but probably you meant to open
'/home/username/data/avg', not just 'home/username/data/avg'.

<snip>

#!/usr/bin/perl
use warnings;
use strict;

my $file = "home/user/data/avg";
open DATA, $file or die $!;
while (<DATA>) {
print $_;
}

when I run it I get
No such file or directory at practice.plx line 6.

You don't really read the responses apparently. Please read what
Jurgen pointed out as the second problem with the open call, and see
if fixing that solves your issue.

Sinan.

yes I did read all the posts, sorry that I didn't give that impression

*************************************
There are two problems in this one line:
Due to precedence rules it will be evaluated as
open DATA, ('home/username/data/avg' || die $!);

If you change this to
open DATA, 'home/username/data/avg' or die $!;
then probably you will get a runtime error "File does not exist" which
points to the second problem:
I'm guessing but probably you meant to open '/home/username/data/avg',
not just 'home/username/data/avg'.
********************************************

the suggestion to use the 'or' instead '||' has been tried for no
avail, but knowing the || has a precedence over 'or' so I used 'or'
with my second version of the program (open DATA, $file or die $!;)

the second suggestion 'guess' states (open '/home/username/data/avg'),
well where is the file handler DATA so I didn't understand how to
implement that solution.

OK, maybe this is an English problem (on your part).

I'm guessing but probably you meant to open '/home/username/data/avg',
not just 'home/username/data/avg'.
</quote>

That is an English sentence, not a Perl statement (notice the 'you meant
to open' part).

#!/usr/bin/perl

use warnings;
use strict;

# BTW, where is the actual file located?
open(DATA, '/home/user/data/avg') || die $!;
while (<DATA>) {
print;
}


--
A. Sinan Unur
(e-mail address removed)
Remove dashes for address
Spam bait: mailto:[email protected]

the file name is avg and it is located at /home/<username>/data directory
 
J

Jürgen Exner

Sam said:
the file name is avg and it is located at /home/<username>/data
directory

Then why are you trying to open
home/user/data/avg
instead of
/home/user/data/avg

Or did you make sure that your CWD is the root directory?
At least I didn't see any code for that anywhere in your previous code
samples.

jue
 
S

Sam

Jürgen Exner said:
Then why are you trying to open
home/user/data/avg
instead of
/home/user/data/avg

Or did you make sure that your CWD is the root directory?
At least I didn't see any code for that anywhere in your previous code
samples.

jue

I really need help to understand this. as I have been reading, you need a
file handler (DATA) attach to it the file name and pass that to the sub open
inorder to read the file's line by line in the variable $_. what I am
missing?
 
T

Tintin

Sam said:
I really need help to understand this. as I have been reading, you need a
file handler (DATA) attach to it the file name and pass that to the sub open
inorder to read the file's line by line in the variable $_. what I am
missing?

The correct path to the file.
 
A

Anno Siegel

Tintin said:
The correct path to the file.

The poster is missing a lot more than that. Let's look at his last
sentence:

"... you need a file handler (DATA)"

The term is "filehandle", not "file handler"

"attach to it the file name and pass that to the sub open"

The attachment doesn't happen before you pass it to "open", it's the
call to "open" that does the attachment. And "open" isn't a sub, it
is a built-in function.

"inorder to read the file's line by line in the variable $_."

The file's what?

The reading you do doesn't happen in "open". Whether you read it
line-by-line or any other way, and whether you read it in the variable
$_ or another variable is determined later.

This is how the sentence might have read:

"... you need a filehandle (DATA) which you attach to the file name
by passing both to "open" in order to read the files content in any
way you please."

Anno
 
J

Jürgen Exner

Sam said:
I really need help to understand this. as I have been reading, you
need a file handler (DATA) attach to it the file name and pass that
to the sub open inorder to read the file's line by line in the
variable $_. what I am missing?

You are missing the bloody leading slash in your file name!
If (for the sake of argument) your current working directory is
/my/own/directory then you are trying to open
/my/own/directory/home/user/data/avg which of course doesn't exist (or if it
exists by chance then it would not be the file that you want).

And yes, of course open() takes another argument which is a file handle (not
a handler!), but that has nothing to do with your problem.

jue
 
S

Sam

Tintin said:
The correct path to the file.
how do I work out that correct path to this file?
I tried
/home/user/data/avg
home/user/data/avg
/data/avg
data/avg
I am logged in as usr$
 
T

Tony Curtis

how do I work out that correct path to this file? I
tried /home/user/data/avg home/user/data/avg /data/avg
data/avg I am logged in as usr$

Only you can know where the file is on your system. Go
into the directory where the file lives and do "pwd".

If you don't know where the file is, how do you expect to
be able to write a program to open it?
 
S

Sam

Tony Curtis said:
Only you can know where the file is on your system. Go
into the directory where the file lives and do "pwd".

If you don't know where the file is, how do you expect to
be able to write a program to open it?

boxname:/home/username/data# ls
avg
boxname:/home/username/data# pwd
/home/username/data
boxname:/home/username/data#

it is working now, for a reason I still don't understand why. it must be
something with the xemacs set-up. for the fact the file name change from
xxx.plx to xxx.pl
I am puzzled.
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top