Ask a Question related to PERL Miscellaneous, Design and Development.
-
Sam #1
closed filehandle
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 $_;
}
Sam Guest
-
FileHandle::Unget 0.13 released
Description: - FileHandle::Unget is a drop-in replacement for FileHandle which allows more than one byte to be placed back on the input. It... -
FILEHANDLE to print to nothing
Howdy, I am benchmarking some stuff that does multiple print staements. What I'd like to do is print to a file handel so ethat the stuff I'm... -
How to put a filehandle into a hash array?
I need to build an array of filehandles so different records go into different output files depending on a value. This is the code: $dt =... -
first.pl | second.pl with ARGV filehandle ?
>>>>> "s" == stu7 <stuseven@hotmail.com> writes: s> + perl first.pl | perl second.pl something, something ARGV fh ? s> This is what I've seen... -
Using filehandle many times
On 29 Jun 2003 23:43:42 -0700 cdcer@hongkong.com (ABC) wrote: In such cases, 'use diagnostics' might help you with what exactly you need to... -
John Bokma #2
Re: closed filehandle
Sam wrote:
line numbers added> Hello
> when I run the code below I get
> readline() on closed filehandle DATA at practice.plx line 6.
>
1 #!/usr/bin/perl
2
3 use warnings;
4
5 use strict;
6
7 open DATA, 'home/username/data/avg' || die $!;
8
9 while (<DATA>) {
10
11 print $_;
12
13 }
Doubt it.
--
Kind regards, feel free to mail: mail(at)johnbokma.com (or reply)
virtual home: [url]http://johnbokma.com/[/url] ICQ: 218175426
John web site hints: [url]http://johnbokma.com/websitedesign/[/url]
John Bokma Guest
-
ko #3
Re: closed filehandle
Sam wrote:
You need to be specific when passing file/path names to open(). Unless> 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 $_;
>
> }
>
>
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
ko Guest
-
Jürgen Exner #4
Re: closed filehandle
Sam wrote:
There are two problems in this one line:> 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 $!;
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
> while (<DATA>) {
> print $_;
> }
Jürgen Exner Guest
-
John J. Trammell #5
Re: closed filehandle
On Sun, 14 Sep 2003 09:36:27 GMT, Sam <samj@austarmetro.com.au> wrote:
[snip]I don't think this line does what you want it to do, because> open DATA, 'home/username/data/avg' || die $!;
>
of precedence issues. Try instead (untested):
open(DATA, 'home/username/data/avg') || die $!;
or:
open DATA, 'home/username/data/avg' or die $!;
and read e.g.:
[url]http://www.foo.be/docs/tpj/issues/vol4_3/tpj0403-0002.html[/url]
John J. Trammell Guest
-
Sam #6
Re: closed filehandle
"Jürgen Exner" <jurgenex@hotmail.com> wrote in message
news:Cj_8b.11126$NX3.9947@nwrddc01.gnilink.net...#!/usr/bin/perl> 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 $!;
> 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
>
>
>
>>> > while (<DATA>) {
> > print $_;
> > }
>
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.
Sam Guest
-
A. Sinan Unur #7
Re: closed filehandle
"Sam" <samj@austarmetro.com.au> wrote in
news:3f64b27b@news.comindico.com.au:
<snip>>
> "Jürgen Exner" <jurgenex@hotmail.com> wrote in message
> news:Cj_8b.11126$NX3.9947@nwrddc01.gnilink.net...>> 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'.You don't really read the responses apparently. Please read what Jurgen>
> #!/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.
pointed out as the second problem with the open call, and see if fixing
that solves your issue.
Sinan.
--
A. Sinan Unur
[email]asu1@c-o-r-n-e-l-l.edu[/email]
Remove dashes for address
Spam bait: mailto:uce@ftc.gov
A. Sinan Unur Guest
-
Sam #8
Re: closed filehandle
"A. Sinan Unur" <asu1@c-o-r-n-e-l-l.edu> wrote in message
news:Xns93F6A46C9676Easu1cornelledu@132.236.56.8.. .yes I did read all the posts, sorry that I didn't give that impression> "Sam" <samj@austarmetro.com.au> wrote in
> news:3f64b27b@news.comindico.com.au:
>>> >
> > "Jürgen Exner" <jurgenex@hotmail.com> wrote in message
> > news:Cj_8b.11126$NX3.9947@nwrddc01.gnilink.net...> >> 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.
>
> --
> A. Sinan Unur
> [email]asu1@c-o-r-n-e-l-l.edu[/email]
> Remove dashes for address
> Spam bait: mailto:uce@ftc.gov
[Quote]*************************************
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
Sam Guest
-
A. Sinan Unur #9
Re: closed filehandle
"Sam" <samj@austarmetro.com.au> wrote in
news:3f64d3da@news.comindico.com.au:
[quote]
>
> "A. Sinan Unur" <asu1@c-o-r-n-e-l-l.edu> wrote in message
> news:Xns93F6A46C9676Easu1cornelledu@132.236.56.8.. .>>> "Sam" <samj@austarmetro.com.au> wrote in
>> news:3f64b27b@news.comindico.com.au:
>>>>>> >
>> > "Jürgen Exner" <jurgenex@hotmail.com> wrote in message
>> > news:Cj_8b.11126$NX3.9947@nwrddc01.gnilink.net...
>> >> 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
>
>OK, maybe this is an English problem (on your part).*************************************
> 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.
<quote author="Jurgen"></quote>> I'm guessing but probably you meant to open '/home/username/data/avg',
> not just 'home/username/data/avg'.
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
[email]asu1@c-o-r-n-e-l-l.edu[/email]
Remove dashes for address
Spam bait: mailto:uce@ftc.gov
A. Sinan Unur Guest
-
Jürgen Exner #10
Re: closed filehandle
Sam wrote:
Well, yes. Exactly as I predicted.> "Jürgen Exner" <jurgenex@hotmail.com> wrote in message
> news:Cj_8b.11126$NX3.9947@nwrddc01.gnilink.net...>>> 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 $!;
>> 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'.
>>>>>>> while (<DATA>) {
>>> print $_;
>>> }
>>
> #!/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.
Does that surprise you in any way? Did you actually read the second part of
my reply?
jue
Jürgen Exner Guest
-
Sam #11
Re: closed filehandle
"A. Sinan Unur" <asu1@c-o-r-n-e-l-l.edu> wrote in message
news:Xns93F6B553F2AD2asu1cornelledu@132.236.56.8.. .[quote]
> "Sam" <samj@austarmetro.com.au> wrote in
> news:3f64d3da@news.comindico.com.au:
>the file name is avg and it is located at /home/<username>/data directory> >
> > "A. Sinan Unur" <asu1@c-o-r-n-e-l-l.edu> wrote in message
> > news:Xns93F6A46C9676Easu1cornelledu@132.236.56.8.. .> >> >> "Sam" <samj@austarmetro.com.au> wrote in
> >> news:3f64b27b@news.comindico.com.au:
> >>
> >> >
> >> > "Jürgen Exner" <jurgenex@hotmail.com> wrote in message
> >> > news:Cj_8b.11126$NX3.9947@nwrddc01.gnilink.net...
> >> >> 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).
>
> <quote author="Jurgen">> </quote>> > I'm guessing but probably you meant to open '/home/username/data/avg',
> > not just 'home/username/data/avg'.
>
> 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
> [email]asu1@c-o-r-n-e-l-l.edu[/email]
> Remove dashes for address
> Spam bait: mailto:uce@ftc.gov
Sam Guest
-
Jürgen Exner #12
Re: closed filehandle
Sam wrote:
Then why are you trying to open> the file name is avg and it is located at /home/<username>/data
> directory
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
Jürgen Exner Guest
-
Sam #13
Re: closed filehandle
"Jürgen Exner" <jurgenex@hotmail.com> wrote in message
news:tcc9b.9890$1D5.4139@nwrddc02.gnilink.net...I really need help to understand this. as I have been reading, you need a> Sam wrote:>> > 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
>
>
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?
Sam Guest
-
Tintin #14
Re: closed filehandle
"Sam" <samj@austarmetro.com.au> wrote in message
news:3f6568df$1@news.comindico.com.au...open>
> "Jürgen Exner" <jurgenex@hotmail.com> wrote in message
> news:tcc9b.9890$1D5.4139@nwrddc02.gnilink.net...>> > Sam wrote:> >> > > 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
> >
> >
> 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 subThe correct path to the file.> inorder to read the file's line by line in the variable $_. what I am
> missing?
Tintin Guest
-
Anno Siegel #15
Re: closed filehandle
Tintin <me@privacy.net> wrote in comp.lang.perl.misc:
The poster is missing a lot more than that. Let's look at his last>
> "Sam" <samj@austarmetro.com.au> wrote in message
> news:3f6568df$1@news.comindico.com.au...> open> >
> > "Jürgen Exner" <jurgenex@hotmail.com> wrote in message
> > news:tcc9b.9890$1D5.4139@nwrddc02.gnilink.net...> >> > > Sam wrote:
> > > > 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
> > >
> > >
> > 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>> > inorder to read the file's line by line in the variable $_. what I am
> > missing?
> The correct path to the file.
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
Anno Siegel Guest
-
Jürgen Exner #16
Re: closed filehandle
Sam wrote:
You are missing the bloody leading slash in your file name!> "Jürgen Exner" <jurgenex@hotmail.com> wrote in message
> news:tcc9b.9890$1D5.4139@nwrddc02.gnilink.net...>>> Sam wrote:>>>>> 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.
> 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?
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
Jürgen Exner Guest
-
Sam #17
Re: closed filehandle
"Tintin" <me@privacy.net> wrote in message
news:bk3u2l$n3ch2$1@ID-172104.news.uni-berlin.de...a>
> "Sam" <samj@austarmetro.com.au> wrote in message
> news:3f6568df$1@news.comindico.com.au...> >
> > "Jürgen Exner" <jurgenex@hotmail.com> wrote in message
> > news:tcc9b.9890$1D5.4139@nwrddc02.gnilink.net...> >> > > Sam wrote:
> > > > 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
> > >
> > >
> > I really need help to understand this. as I have been reading, you needhow do I work out that correct path to this file?> open> > file handler (DATA) attach to it the file name and pass that to the sub>> > inorder to read the file's line by line in the variable $_. what I am
> > missing?
> The correct path to the file.
>
>
I tried
/home/user/data/avg
home/user/data/avg
/data/avg
data/avg
I am logged in as usr$
Sam Guest
-
Tony Curtis #18
Re: closed filehandle
>> On Mon, 15 Sep 2003 16:30:51 GMT,
>> "Sam" <samj@austarmetro.com.au> said:> "Tintin" <me@privacy.net> wrote in messageOnly you can know where the file is on your system. Go> how do I work out that correct path to this file? I>> The correct path to the file.
>>
> tried /home/user/data/avg home/user/data/avg /data/avg
> data/avg I am logged in as usr$
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?
Tony Curtis Guest
-
Sam #19
Re: closed filehandle
"Tony Curtis" <tony_curtis32@yahoo.com> wrote in message
news:87ad96nh4p.fsf@limey.hpcc.uh.edu...boxname:/home/username/data# ls>> >> On Mon, 15 Sep 2003 16:30:51 GMT,
> >> "Sam" <samj@austarmetro.com.au> said:>> > "Tintin" <me@privacy.net> wrote in message>> > how do I work out that correct path to this file? I> >> The correct path to the file.
> >>
> > 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?
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.
Sam Guest



Reply With Quote

