Magius_AR has asked for the wisdom of the Perl Monks concerning the following question:

OK, I'm trying to code the functionality of the UNIX < operator

Here is the code I have so far that doesnt work:

# command < infile open (IN,"$infile"); @data = <IN>; close(IN); $pid = open(READ_FROM_CHILD,"-|"); unless (defined $pid) { die "cannot fork: $!"; } if ($pid) { @result = <READ_FROM_CHILD>; close(READ_FROM_CHILD); } else { $pid = open(WRITE_TO_CHILD, "|-"); unless (defined $pid) { die "cannot fork: $!"; } if ($pid) { print WRITE_TO_CHILD @data; close(WRITE_TO_CHILD); } else { exec "$path/$command"; } }

This doesn't work...@result is always empty. Also, the "print WRITE_TO_CHILD @data" line returns output to the screen when it should not be doing that (is STDOUT not getting redirected as it should?)

This problem has been plaguing me for days now...any help would be much appreciated.

Replies are listed 'Best First'.
Re: Followup to redirecting filehandles
by ChOas (Curate) on Oct 23, 2000 at 13:07 UTC
    Okay, couple of pointers, 'xcuse me if I'm wrong

    Are you using strict ?, -w ?...
    @result is supposed to be empty I think, because it
    goes out of scope the second that 'if ($Pid)' closure ends.
    Maybe you pre-declared it in real life but not in the
    code I see here...
    or maybe it really IS empty in that closure
    But wouldn't know that ;))

    I'll try some stuff...
Re: Followup to redirecting filehandles
by Magius_AR (Sexton) on Oct 23, 2000 at 13:12 UTC
    Actually, i was wrong...@result does have what I was looking for...but I'm still getting that annoying output thing on the print @data

    And I don't quite understand the concept at all, so I don't know what you mean by closure.

    I found this stuff at http://sunsite.ualberta.ca/Documentation/Misc/perl-5.005_03/pod/perlipc/Safe_Pipe_Opens.html

    So I guess my largest problem is keeping the thing from printing to screen...how would I go about that?

Re: Followup to redirecting filehandles
by Magius_AR (Sexton) on Oct 23, 2000 at 13:15 UTC
    oh, damnit...it was a stray print statement... now I feel dumb ;P hehe...well thanx anyways...its working good now