Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

example in book of Programming Perl does not work.

by yangy (Novice)
on May 30, 2013 at 05:15 UTC ( #1035962=perlquestion: print w/replies, xml ) Need Help??

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

#!/usr/bin/env perl use v5.14; tee("/tmp/foo", "/tmp/bar", "/tmp/glarch"); while (<>) { print "$ARGV at line $. => $_"; } close(STDOUT) || die "can't close STDOUT: $!"; sub tee { my @output = @_; my @handles = (); for my $path (@output) { my $fh; # open will fill this in unless (open ($fh, ">", $path)) { warn "cannot write to $path: $!"; next; } push @handles, $fh; } # reopen STDOUT in parent and return return if my $pid = open(STDOUT, "|"); print "\$pid is : $pid"; die "cannot fork: $!" unless defined $pid; # process STDIN in child while (<STDIN>) { for my $fh (@handles) { print $fh $_ || die "tee output failed: $!"; } } for my $fh (@handles) { close($fh) || die "tee closing failed: $!"; } exit; # don't let the child return to main! }
# page 535 in book Programming Perl

when I run this in ubuntu shell but there is an error:

cannot fork: no such file or directory at ./ line 27.

why? somebody give some advice.thanks.

Replies are listed 'Best First'.
Re: example in book of Programming Perl does not work.
by aitap (Curate) on May 30, 2013 at 06:07 UTC
    You wrote a wrong "-" symbol:
    $ LC_ALL=C perl -E'my $pid = open(STDOUT, "|") // die $!' # your quot +e No such file or directory at -e line 1. $ perl -E'my $pid = open(STDOUT, "|-") // die $!' # entered via keyboa +rd $
    Perl does not consider "" (chr(8211)) to be a dash ("-", chr(45)), so special open behaviour does not work in this case.
      thanks, I copy that char from pdf, now I change it to -,it work perfect now.

        I wonder if there is a way to submit an errata to the publisher. Is there source code included along with the PDF file that would be more appropriate to use?

        <muse>I understand that typesetting has its own needs, but I wonder if there is any way to have the best of both worlds in a case like this.</muse>


      That blasted unicode 'en-dash' is the bane of my existence. I know I should pray for patience but most of the time I pray for the hastening schadenfreude that *will* befall the designers who think their dashes deserve to be special.

Re: example in book of Programming Perl does not work.
by farang (Chaplain) on May 30, 2013 at 06:09 UTC

    Greetings yangy,

    You have an en dash in your open statement.

    return if my $pid = open(STDOUT, "|");
    Changing it to a regular hyphen minus makes it work for me.
    return if my $pid = open(STDOUT, "|-");

      thank you.
Re: example in book of Programming Perl does not work.
by vinoth.ree (Monsignor) on May 30, 2013 at 05:30 UTC

    Hi yangy

    I have tried the same, in my systems its works correctly. I am also using ubuntu only.

    What PID is printing in the code?

    print "\$pid is : $pid"

    I am getting 0.

    This line raises the error for you

    die "cannot fork: $!" unless defined $pid;
    All is well

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1035962]
Approved by vinoth.ree
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (1)
As of 2022-07-01 08:26 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (98 votes). Check out past polls.