closed filehandle

Discussion in 'Perl Misc' started by Sam, Sep 14, 2003.

  1. Sam

    Sam Guest

    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, Sep 14, 2003
    #1
    1. Advertising

  2. Sam

    ko Guest

    Sam wrote:
    > 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
    ko, Sep 14, 2003
    #2
    1. Advertising

  3. 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 $_;
    > }
    Jürgen Exner, Sep 14, 2003
    #3
  4. On Sun, 14 Sep 2003 09:36:27 GMT, Sam <> wrote:
    [snip]
    > open DATA, 'home/username/data/avg' || die $!;
    >


    I don't think this line does what you want it to do, because
    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.:

    http://www.foo.be/docs/tpj/issues/vol4_3/tpj0403-0002.html
    John J. Trammell, Sep 14, 2003
    #4
  5. Sam

    Sam Guest

    "Jürgen Exner" <> wrote in message
    news:Cj_8b.11126$...
    > 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 $_;
    > > }

    >
    >


    #!/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.
    Sam, Sep 14, 2003
    #5
  6. "Sam" <> wrote in
    news::

    >
    > "Jürgen Exner" <> wrote in message
    > news:Cj_8b.11126$...
    >> 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

    Remove dashes for address
    Spam bait: mailto:
    A. Sinan Unur, Sep 14, 2003
    #6
  7. Sam

    Sam Guest

    "A. Sinan Unur" <> wrote in message
    news:Xns93F6A46C9676Easu1cornelledu@132.236.56.8...
    > "Sam" <> wrote in
    > news::
    >
    > >
    > > "Jürgen Exner" <> wrote in message
    > > news:Cj_8b.11126$...
    > >> 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
    >
    > Remove dashes for address
    > Spam bait: mailto:


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

    Sam, Sep 14, 2003
    #7
  8. "Sam" <> wrote in
    news::

    >
    > "A. Sinan Unur" <> wrote in message
    > news:Xns93F6A46C9676Easu1cornelledu@132.236.56.8...
    >> "Sam" <> wrote in
    >> news::
    >>
    >> >
    >> > "Jürgen Exner" <> wrote in message
    >> > news:Cj_8b.11126$...
    >> >> 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
    >
    >
    A. Sinan Unur, Sep 14, 2003
    #8
  9. Sam wrote:
    > "Jürgen Exner" <> wrote in message
    > news:Cj_8b.11126$...
    >> 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.


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

    jue
    Jürgen Exner, Sep 15, 2003
    #9
  10. Sam

    Sam Guest

    "A. Sinan Unur" <> wrote in message
    news:Xns93F6B553F2AD2asu1cornelledu@132.236.56.8...
    > "Sam" <> wrote in
    > news::
    >
    > >
    > > "A. Sinan Unur" <> wrote in message
    > > news:Xns93F6A46C9676Easu1cornelledu@132.236.56.8...
    > >> "Sam" <> wrote in
    > >> news::
    > >>
    > >> >
    > >> > "Jürgen Exner" <> wrote in message
    > >> > news:Cj_8b.11126$...
    > >> >> 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
    > >
    > >
    Sam, Sep 15, 2003
    #10
  11. 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
    Jürgen Exner, Sep 15, 2003
    #11
  12. Sam

    Sam Guest

    "Jürgen Exner" <> wrote in message
    news:tcc9b.9890$...
    > 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 open
    inorder to read the file's line by line in the variable $_. what I am
    missing?
    Sam, Sep 15, 2003
    #12
  13. Sam

    Tintin Guest

    "Sam" <> wrote in message
    news:3f6568df$...
    >
    > "Jürgen Exner" <> wrote in message
    > news:tcc9b.9890$...
    > > 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

    open
    > inorder to read the file's line by line in the variable $_. what I am
    > missing?


    The correct path to the file.
    Tintin, Sep 15, 2003
    #13
  14. Sam

    Anno Siegel Guest

    Tintin <> wrote in comp.lang.perl.misc:
    >
    > "Sam" <> wrote in message
    > news:3f6568df$...
    > >
    > > "Jürgen Exner" <> wrote in message
    > > news:tcc9b.9890$...
    > > > 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

    > open
    > > inorder to read the file's line by line in the variable $_. what I am
    > > missing?

    >
    > 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
    Anno Siegel, Sep 15, 2003
    #14
  15. Sam wrote:
    > "Jürgen Exner" <> wrote in message
    > news:tcc9b.9890$...
    >> 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?


    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
    Jürgen Exner, Sep 15, 2003
    #15
  16. Sam

    Sam Guest

    "Tintin" <> wrote in message
    news:bk3u2l$n3ch2$-berlin.de...
    >
    > "Sam" <> wrote in message
    > news:3f6568df$...
    > >
    > > "Jürgen Exner" <> wrote in message
    > > news:tcc9b.9890$...
    > > > 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

    > open
    > > inorder to read the file's line by line in the variable $_. what I am
    > > missing?

    >
    > 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$
    Sam, Sep 15, 2003
    #16
  17. Sam

    Tony Curtis Guest

    >> On Mon, 15 Sep 2003 16:30:51 GMT,
    >> "Sam" <> said:


    > "Tintin" <> wrote in message


    >> 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$


    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?
    Tony Curtis, Sep 15, 2003
    #17
  18. Sam

    Sam Guest

    "Tony Curtis" <> wrote in message
    news:...
    > >> On Mon, 15 Sep 2003 16:30:51 GMT,
    > >> "Sam" <> said:

    >
    > > "Tintin" <> wrote in message

    >
    > >> 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$

    >
    > 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.
    Sam, Sep 15, 2003
    #18
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eduard W. Lohmann
    Replies:
    1
    Views:
    629
  2. Matt Kruse
    Replies:
    5
    Views:
    298
    Richard Cornford
    Sep 9, 2003
  3. Rex Gustavus Adolphus
    Replies:
    17
    Views:
    206
    Rex Gustavus Adolphus
    Mar 7, 2004
  4. Jim

    prematurely closed filehandle

    Jim, Jun 29, 2006, in forum: Perl Misc
    Replies:
    9
    Views:
    148
    Sherm Pendley
    Jul 7, 2006
  5. Russ

    Read on closed filehandle

    Russ, Feb 1, 2007, in forum: Perl Misc
    Replies:
    12
    Views:
    185
Loading...

Share This Page