Implementation of file input operator <>

Discussion in 'Perl Misc' started by himanshu.garg@gmail.com, Oct 24, 2007.

  1. Guest

    Hello,

    Could you tell me which file in the perl source distro I should
    look at for the implementation of the file input operators <>.

    I suspect that one 'call' of <> is leading to two read system
    calls.

    Thank You,
    HG
     
    , Oct 24, 2007
    #1
    1. Advertising

  2. Uri Guttman Guest

    >>>>> "hg" == himanshu garg <> writes:

    hg> Could you tell me which file in the perl source distro I should
    hg> look at for the implementation of the file input operators <>.

    hg> I suspect that one 'call' of <> is leading to two read system
    hg> calls.

    do you think that is a bug? or why would 2 read calls bother you? it is
    entirely possible for one readline call (what <> really is) could make
    many read calls if the next input record (i didn't say line) overlaps
    i/o buffer boundaries. perl's i/o subsystem (or stdio if you are using
    that) manages read calls so it can satisfy your <> call. you want it to
    make multiple read calls as needed.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Oct 24, 2007
    #2
    1. Advertising

  3. Ben Morrow Guest

    Quoth :
    >
    > Could you tell me which file in the perl source distro I should
    > look at for the implementation of the file input operators <>.


    <> is implemented as pp_readline in pp_hot.c, which calls
    pp_hot.c:perl_do_readline, which calls sv.c:perl_sv_gets, which calls
    PerlIO_read to perform the actual read. Note that understanding the Perl
    IO system is not easy.

    > I suspect that one 'call' of <> is leading to two read system
    > calls.


    As Uri pointed out, this is expected and desired. If you want to do your
    own buffering, push a :unix layer or use sysread.

    Ben
     
    Ben Morrow, Oct 24, 2007
    #3
  4. Guest

    On Oct 25, 1:58 am, Ben Morrow <> wrote:
    > Quoth :
    >
    >
    >
    > > Could you tell me which file in the perl source distro I should
    > > look at for the implementation of the file input operators <>.

    >
    > <> is implemented as pp_readline in pp_hot.c, which calls
    > pp_hot.c:perl_do_readline, which calls sv.c:perl_sv_gets, which calls
    > PerlIO_read to perform the actual read. Note that understanding the Perl
    > IO system is not easy.
    >
    > > I suspect that one 'call' of <> is leading to two read system
    > > calls.

    >
    > As Uri pointed out, this is expected and desired. If you want to do your
    > own buffering, push a :unix layer or use sysread.
    >


    Thanks Uri and Ben,

    There was a race condition in which the child process died during
    the second read call and the parent kept waiting. Using a signal
    handler for SIGCHLD required many code changes. Using sysread, I no
    longer see the error.

    Since multiple reads are entirely possible I will not go into the
    source :)

    Thank You,
    HG
     
    , Oct 25, 2007
    #4
  5. Uri Guttman Guest

    >>>>> "hg" == himanshu garg <> writes:


    hg> There was a race condition in which the child process died during
    hg> the second read call and the parent kept waiting. Using a signal
    hg> handler for SIGCHLD required many code changes. Using sysread, I no
    hg> longer see the error.

    this sounds like you are reading from a pipe connected to the child. in
    general i always say use sysread on pipes and sockets and bypass perlIO
    or stdio. managing buffering can be more work but you gain total control
    over things which is more important. using an event loop also makes it
    much easier to manage i/o on sockets and pipes.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Oct 25, 2007
    #5
  6. Guest

    Uri Guttman <> wrote:
    > >>>>> "hg" == himanshu garg <> writes:

    >
    > hg> There was a race condition in which the child process died
    > during hg> the second read call and the parent kept waiting. Using a
    > signal hg> handler for SIGCHLD required many code changes. Using sysread,
    > I no hg> longer see the error.
    >
    > this sounds like you are reading from a pipe connected to the child.


    But in that case the parent end of the pipe should be set eof when the
    child dies, causing read to unblock. Or at least that is the why the pipes
    I've used behaved. It sounds like something more weird than a simple pipe.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
     
    , Oct 26, 2007
    #6
    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. Peter Koch Larsen

    new operator implementation

    Peter Koch Larsen, Jun 3, 2004, in forum: C++
    Replies:
    5
    Views:
    5,434
    Prateek R Karandikar
    Jun 4, 2004
  2. Mikhail N. Kupchik
    Replies:
    7
    Views:
    456
    Glen Low
    Aug 23, 2004
  3. Michael Tsang
    Replies:
    32
    Views:
    1,149
    Richard Bos
    Mar 1, 2010
  4. Michael Tsang
    Replies:
    54
    Views:
    1,228
    Phil Carmody
    Mar 30, 2010
  5. sanket
    Replies:
    7
    Views:
    1,080
    Tsung
    Nov 3, 2011
Loading...

Share This Page