IPC::Open2 - Bad File Descriptor

Discussion in 'Perl Misc' started by Bernard Chan, Mar 6, 2007.

  1. Bernard Chan

    Bernard Chan Guest

    Dear All,

    I am pretty new to the area of pipes and IPC in general. I'm currently
    experimenting with it in different ways trying to understand how it works.

    I tried the following simple command:

    $ LANG=C perl -MIPC::Open2 -e 'open2(">&1", "<&0", "ls", "-l",
    "/dev/shm")'
    total 4
    -rw-rw-r-- 1 bernardchan bernardchan 3204 Mar 6 16:00 test.txt
    open2: close(0) failed: Bad file descriptor at -e line 1


    As you can see, what I would like to do is simple: to run an "ls -l" and
    pipe the output directly to STDOUT and have any input (of course none for
    this case of "ls") read from STDIN. I can get back the desired output, but
    I also receive the error that open2() cannot close the read filehandle.

    This is actually a simplified version - my original version used a socket
    (IO::Socket::INET6) in place of STDIN and STDOUT. The problem was similar.

    Anyone has some insights about what I am doing wrong?

    TIA.

    Regards,
    Bernard Chan.

    --
    Posted via a free Usenet account from http://www.teranews.com
     
    Bernard Chan, Mar 6, 2007
    #1
    1. Advertising

  2. Bernard Chan

    Ben Morrow Guest

    Quoth Bernard Chan <>:
    >
    > Dear All,
    >
    > I am pretty new to the area of pipes and IPC in general. I'm currently
    > experimenting with it in different ways trying to understand how it works.
    >
    > I tried the following simple command:
    >
    > $ LANG=C perl -MIPC::Open2 -e 'open2(">&1", "<&0", "ls", "-l",
    > "/dev/shm")'
    > total 4
    > -rw-rw-r-- 1 bernardchan bernardchan 3204 Mar 6 16:00 test.txt
    > open2: close(0) failed: Bad file descriptor at -e line 1
    >
    >
    > As you can see, what I would like to do is simple: to run an "ls -l" and
    > pipe the output directly to STDOUT and have any input (of course none for
    > this case of "ls") read from STDIN. I can get back the desired output, but
    > I also receive the error that open2() cannot close the read filehandle.
    >
    > This is actually a simplified version - my original version used a socket
    > (IO::Socket::INET6) in place of STDIN and STDOUT. The problem was similar.
    >
    > Anyone has some insights about what I am doing wrong?


    The correct syntax is

    open2('>&STDOUT', '<&STDIN', 'ls', '-l', '/dev/shm');

    You are getting that error because open2 is attempting to use the
    filehandle called '0' (as opposed to fd 0): as this filehandle isn't
    open, you get 'bad file descriptor'. The redirection happens to work
    because open2 uses the '<&FILEHANDLE' open syntax internally, which
    treats numbers as fds rather than filehandles.

    You may find IPC::Run easier to work with, not least because open2 seems
    to require you to use bareword filehandles, which are not considered
    good practice nowadays. (Is it even possible to create a bareword
    filehandle to a socket, except by using the raw socket function?)

    Ben

    --
    I must not fear. Fear is the mind-killer. I will face my fear and
    I will let it pass through me. When the fear is gone there will be
    nothing. Only I will remain.
    Frank Herbert, 'Dune'
     
    Ben Morrow, Mar 6, 2007
    #2
    1. Advertising

  3. On Mar 6, 12:22 am, Bernard Chan <> wrote:
    > Dear All,
    >
    > I am pretty new to the area of pipes and IPC in general. I'm currently
    > experimenting with it in different ways trying to understand how it works.
    >
    > I tried the following simple command:
    >
    > $ LANG=C perl -MIPC::Open2 -e 'open2(">&1", "<&0", "ls", "-l",
    > "/dev/shm")'
    > total 4
    > -rw-rw-r-- 1 bernardchan bernardchan 3204 Mar 6 16:00 test.txt
    > open2: close(0) failed: Bad file descriptor at -e line 1
    > ...


    You were close...

    open2('>&=0', '<&=0', ... )

    >From 'perldoc -f open' :


    If you specify '<&=X', where "X" is a file
    descriptor number or a filehandle, then Perl
    will do an equivalent of C's "fdopen" of that
    file

    --
    Charles DeRykus
     
    comp.llang.perl.moderated, Mar 6, 2007
    #3
  4. Bernard Chan

    Uri Guttman Guest

    >>>>> "BM" == Ben Morrow <> writes:

    BM> You may find IPC::Run easier to work with, not least because open2 seems
    BM> to require you to use bareword filehandles, which are not considered
    BM> good practice nowadays. (Is it even possible to create a bareword
    BM> filehandle to a socket, except by using the raw socket function?)

    you could always store in a lexical a Symbol::gensym for any handle
    thing for ages, even open2. and recent versions of perl will autovivify
    the handles in open2 (and open3) if you use lexicals. for some reason in
    open3, the stderr handle still needs to be passed in but again, you can
    use gensym in a lexical and never go near bareword handles.

    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, Mar 6, 2007
    #4
  5. Bernard Chan

    Ben Morrow Guest

    Quoth "comp.llang.perl.moderated" <>:
    > On Mar 6, 12:22 am, Bernard Chan <> wrote:
    > > Dear All,
    > >
    > > I am pretty new to the area of pipes and IPC in general. I'm currently
    > > experimenting with it in different ways trying to understand how it works.
    > >
    > > I tried the following simple command:
    > >
    > > $ LANG=C perl -MIPC::Open2 -e 'open2(">&1", "<&0", "ls", "-l",
    > > "/dev/shm")'
    > > total 4
    > > -rw-rw-r-- 1 bernardchan bernardchan 3204 Mar 6 16:00 test.txt
    > > open2: close(0) failed: Bad file descriptor at -e line 1
    > > ...

    >
    > You were close...
    >
    > open2('>&=0', '<&=0', ... )


    Did you try it?

    ~% perl -MIPC::Open2 -e 'open2(">&=1", "<&=0", "ls", "-l", "/dev/shm")'
    total 0
    open2: close(=0) failed: Bad file descriptor at -e line 1

    Still get the warning, for the same reason: open2 is explicitly trying
    to close \*{"=0"}, which isn't open.

    > >From 'perldoc -f open' :

    >
    > If you specify '<&=X', where "X" is a file
    > descriptor number or a filehandle, then Perl
    > will do an equivalent of C's "fdopen" of that
    > file


    open2 ne open.

    Ben

    --
    2 + 2 = 5 for sufficiently large values of 2
    []
     
    Ben Morrow, Mar 6, 2007
    #5
  6. Bernard Chan

    Ben Morrow Guest

    Quoth Uri Guttman <>:
    > >>>>> "BM" == Ben Morrow <> writes:

    >
    > BM> You may find IPC::Run easier to work with, not least because open2 seems
    > BM> to require you to use bareword filehandles, which are not considered
    > BM> good practice nowadays. (Is it even possible to create a bareword
    > BM> filehandle to a socket, except by using the raw socket function?)
    >
    > you could always store in a lexical a Symbol::gensym for any handle
    > thing for ages,


    Of course.

    > even open2.


    Perhaps I wasn't clear: I was specifically talking about the '>&FOO'
    syntax for open2 which performs a dup instead of a pipe. In this case I
    can't see any way to use a lexical FH, as open2 (well,
    IPC::Open3::_open3) explicitly refers to \*{$dad_rdr}, where $dad_rdr is
    ">&FOO" =~ s/^[<>]&// .

    Ben

    --
    Although few may originate a policy, we are all able to judge it.
    Pericles of Athens, c.430 B.C.
     
    Ben Morrow, Mar 6, 2007
    #6
  7. Bernard Chan

    Uri Guttman Guest

    >>>>> "BM" == Ben Morrow <> writes:

    BM> Perhaps I wasn't clear: I was specifically talking about the '>&FOO'
    BM> syntax for open2 which performs a dup instead of a pipe. In this case I
    BM> can't see any way to use a lexical FH, as open2 (well,
    BM> IPC::Open3::_open3) explicitly refers to \*{$dad_rdr}, where $dad_rdr is
    BM> ">&FOO" =~ s/^[<>]&// .

    perhaps you can first call open to dup the handle and have it autovivify
    it and pass that to open2/3? i haven't followed this thread closely so i
    don't know what the dup is being used for, etc. i am sure there are ways
    to keep it all lexical as those still have the handle name as a string
    in their globs. the only diff is that there is no global name for the
    handle. Symbol::gensym does the same thing - it actually creates a new
    unique handle name (with magic ++) in its namespace, gets a ref to it
    and then deletes the symbol itself so it becomes private.

    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, Mar 6, 2007
    #7
  8. On Mar 6, 10:10 am, Ben Morrow <> wrote:
    > Quoth "comp.llang.perl.moderated" <>:
    >
    >
    >
    > >> ...

    >
    > > You were close...

    >
    > > open2('>&=0', '<&=0', ... )

    >
    > Did you try it?
    >
    > ~% perl -MIPC::Open2 -e 'open2(">&=1", "<&=0", "ls", "-l", "/dev/shm")'
    > total 0
    > open2: close(=0) failed: Bad file descriptor at -e line 1
    >
    > Still get the warning, for the same reason: open2 is explicitly trying
    > to close \*{"=0"}, which isn't open.
    >
    > > >From 'perldoc -f open' :

    >
    > > If you specify '<&=X', where "X" is a file
    > > descriptor number or a filehandle, then Perl
    > > will do an equivalent of C's "fdopen" of that
    > > file

    >
    > open2 ne open.


    Thanks, I did try it but lost the error. It is interesting though
    that IPC::Open3 (but not IPC::Open2) says:

    ...
    The filehandles may also be integers, in which case they are
    understood as file descriptors.

    which, for reasons I didn't follow in the code, avoids the error:

    $ perl -MIPC::Open2 -e 'open2(">&=1", ">&=0" , "ls", "-l", "/dev/
    null")'
    lrwxrwxrwx 1 root other 27 Oct 9 2001 /dev/null ->
    ../devices/pseudo/mm@0:null
    open2: close(=0) failed: Bad file number at -e line 1

    vs.

    $ perl -MIPC::Open2 -e 'open2(">&=1", 0 , "ls", "-l", "/dev/null")'
    lrwxrwxrwx 1 root other 27 Oct 9 2001 /dev/null ->
    ../devices/pseudo/mm@0:null

    --
    Charles DeRykus
     
    comp.llang.perl.moderated, Mar 7, 2007
    #8
  9. Bernard Chan

    -berlin.de Guest

    Ben Morrow <> wrote in comp.lang.perl.misc:
    > Quoth Bernard Chan <>:


    [...]

    > You may find IPC::Run easier to work with, not least because open2 seems
    > to require you to use bareword filehandles, which are not considered
    > good practice nowadays.


    That's no problem:

    use IPC::Open2;

    my $pid = open2( my( $read, $write), 'sort');
    print $write "$_\n" for qw( ccc bbb aaa);
    close $write;
    print while <$read>;

    Anno
     
    -berlin.de, Mar 8, 2007
    #9
  10. Bernard Chan

    Ben Morrow Guest

    Quoth -berlin.de:
    > Ben Morrow <> wrote in comp.lang.perl.misc:
    > > Quoth Bernard Chan <>:

    >
    > [...]
    >
    > > You may find IPC::Run easier to work with, not least because open2 seems
    > > to require you to use bareword filehandles, which are not considered
    > > good practice nowadays.

    >
    > That's no problem:
    >
    > use IPC::Open2;
    >
    > my $pid = open2( my( $read, $write), 'sort');
    > print $write "$_\n" for qw( ccc bbb aaa);
    > close $write;
    > print while <$read>;


    Yes, but the OP's case was more like

    use IPC::Open2;

    open FOO, '>', 'foo';
    open BAR, '<', 'bar';

    my $pid = open2 '>&FOO', '<&BAR', 'sort';

    which requires bareword FHs to dup.

    Ben

    --
    You poor take courage, you rich take care:
    The Earth was made a common treasury for everyone to share
    All things in common, all people one.
    'We come in peace'---the order came to cut them down. []
     
    Ben Morrow, Mar 8, 2007
    #10
    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. Antonio
    Replies:
    0
    Views:
    661
    Antonio
    May 17, 2004
  2. Replies:
    9
    Views:
    739
    Abhijit Soman
    Jan 6, 2005
  3. grocery_stocker

    IPC::Open2 and sort

    grocery_stocker, Dec 9, 2006, in forum: Perl Misc
    Replies:
    12
    Views:
    191
    Joe Smith
    Dec 15, 2006
  4. Jerry Krinock
    Replies:
    9
    Views:
    272
    Jerry Krinock
    Sep 19, 2009
  5. Justin C
    Replies:
    4
    Views:
    680
    Rainer Weikusat
    Jul 3, 2012
Loading...

Share This Page