>The suggestion was the open() return a filehandle object, so may
>I suggest

>   $fh = open("| cat -v | sort | lpr") or die ....;
>   $pid = $fh->pid;

Let's see how that plays out with pipe open:

    if ($fh = open("| cat -v | sort | lpr")) {
        print $fh "stuff\n";
    } else {
        die "can't start pipeline: $!";
    } 

or more simply

    $fh = open("| cat -v | sort | lpr") || die;
    print $fh "stuff\n";

or even 

    for $fh (open("| cat -v | sort | lpr")) {
        print $fh "stuff\n";
        close $fh;
    } 

Now with fork open:

    if ($fh = open("|-")) {
        if ($fh->pid) {
            print $fh "stuff\n";
        } 
        else {
            $data = readline $fh;
            exit;
        } 
    } else {
        die "can't start pipeline: $!";
    } 

Except that I hate the tiny case at the bottom, as it requires
deep nesting.  Factoring it out reduces the McCabe number.

    $fh = open("|-") || die "can't start pipeline: $!";

    if ($fh->pid) {
        print $fh "stuff\n";
    } else {
        $data = readline $fh;
    } 

I think I'd prefer that ->pid return 0 only to the child of a fork,
but undef on a handle that were not the product of a fork, irrespective
of direction.

Some related issues:

    1) Did Larry or did he not mention something about dealing with
       indirect object more cleanly?

    2) What if any reasonable use could be put to a list context
       return from open(), or from ->pid()?

    3) Isn't it about time to start saving the string argument to open?
            $fh = open("< /etc/motd");
            print $fh->filename;
            print $fh->mode;
            print $fh->fileno;

    4) This may become interesting for fdopens and dups.

            $fh = open("<=STDIN");
            $fh = open("<&=STDIN");

        Oops, what about

            $fh2 = open("<=$fh1")

        that fails.   Maybe

            $fh2 = open("<=", $fh1)

        or 

            $fh2 = open($fh1->mode . "=", $fh1)

        or more clearly

            $fh2 = $fh1->dup;

--tom

Reply via email to