The example is incorrect.
The correct syntax should be
$pid = fork() or die "Fork ERROR $!\n";)
Using "or" instead of "||" is crucial.
The || form of the or operator has higher precedence than the "or"
version, and consequently causes both sides of the conditional to be
evaluated -- that is, executed.
John
On 8 May 2008, at 9:29, Dukelow, Don wrote:
> Going through the book "Perl by Example" by Ellie Quigley, I was
> reading chapter 20 about sockets. The first server example is below
> from the book (example 20.17) with a few minor changes. As it is
> written from the book the fork command always die's when the client
> script is started. ($pid = fork() || die "Fork ERROR $!\n";) But
> when I take out the "|| die" part it works.
> My question is why does the die command always get executed when
> part of the fork line.
>
> #! /usr/local/bin/perl
>
> use warnings;
> use Socket;
>
> print "\n\tServer startrd\n\n";
>
> $AF_UNIX = 1;
> $SOCK_STREAM = 1;
> $PROTOCOL = 0;
>
> socket (SERVERSOCKET, $AF_UNIX, $SOCK_STREAM, $PROTOCOL) ||
> die "Socket $!\n";
> print "Socket OK!\n";
>
> $name = "./greetings";
> unlink "./greetings" || warn "$name: $!\n";
>
> bind (SERVERSOCKET, $name) || die "Bind $!\n";
> print "Bind OK!\n";
>
> listen(SERVERSOCKET, 5) || die "Listem $!\n";
> print "Listren OK!\n";
>
> while (1) {
> accept (NEWSOCKET, SERVERSOCKET);
> sleep 1;
> # $pid = fork() || die "Fork ERROR $!\n"; # Always dies
> $pid = fork(); # Works fine
> if ($pid == 0) {
> print NEWSOCKET "Greetings from your server!!\n";
> close (NEWSOCKET);
> exit (0);
> }
> else {
> close (NEWSOCKET);
> }
> }
> -----
> Don Dukelow
> HP License Team
> Hewlett-Packard Company
> Tel: 810-728-3388
> e-mail: dukelow@...
>
>
[Non-text portions of this message have been removed]