From: John Kelly on
On Sat, 26 Jun 2010 19:07:48 +0100, Ben Morrow <ben(a)morrow.me.uk> wrote:

>Quoth John Kelly <jak(a)isp2dial.com>:
>> On Sat, 26 Jun 2010 18:23:21 +0200, "Dr.Ruud" <rvtol+usenet(a)xs4all.nl>
>> wrote:
>>
>> >An alternative is to seek to the end:
>> >
>> > seek STDIN, 0, SEEK_END;
>> > <>;
>>
>> I posted, hoping for some magical Perl incantation. After all, there
>> are so many of them! But seek should be just as good. However, it also
>> needs a while loop that tests EOF.

>Err... no. Pipes are not seekable, so the seek will simply fail. (Ruud
>should have checked the return value of seek for exactly this reason.)


I see there's no rabbit in that hat:


>man lseek

>ERRORS
> EBADF fd is not an open file descriptor.
>
> EINVAL whence is not one of SEEK_SET, SEEK_CUR, SEEK_END; or the
> resulting file offset would be negative, or beyond the end of a
> seekable device.
>
> EOVERFLOW
> The resulting file offset cannot be represented in an off_t.
>
> ESPIPE fd is associated with a pipe, socket, or FIFO.
>

It was too good to be true. What was I thinking ...


--
Web mail, POP3, and SMTP
http://www.beewyz.com/freeaccounts.php

From: Xho Jingleheimerschmidt on
John Kelly wrote:
> On Sat, 26 Jun 2010 18:23:21 +0200, "Dr.Ruud" <rvtol+usenet(a)xs4all.nl>
> wrote:
>
>> An alternative is to seek to the end:
>>
>> seek STDIN, 0, SEEK_END;
>> <>;
>
> I posted, hoping for some magical Perl incantation. After all, there
> are so many of them! But seek should be just as good. However, it also
> needs a while loop that tests EOF.

On my system it just doesn't work at all, setting $! to "Illegal seek".

> Otherwise, the pipe writer could race with you, and write more data
> after you seek, but before you read. Without a while loop testing for
> EOF, you may falsely assume EOF, and close STDIN while the writer is
> still sending more data, thus breaking the pipe.

But a broken pipe needn't be a problem. It is merely a condition, not
an error, unless the program decides to turn it into an error. Can you
instruct the other end of the pipe to just behave gracefully on SIGPIPE?
(Alas, bzcat can't be so instructed, as far as I can determine.) This
would be the ultimate in efficiency.

Xho
From: C.DeRykus on
On Jun 26, 7:15 am, Ben Morrow <b...(a)morrow.me.uk> wrote:
> Quoth "C.DeRykus" <dery...(a)gmail.com>:
>
>
>
> ...
> > whereas, with just scalar context:
>
> >   perl -we '$SIG{INT}=sub{exit};undef $/; 1 while <> ; END{print}'
> >   foo
> >   bar
> >   ^Cfoo
>
> That's not 'just scalar context'. <>-within-while is special-cased to
> assign to $_ (and check 'defined', rather than simply truth). Try
>
>     perl -we '$SIG{INT}=sub{exit}; undef $/; $x = <>; END{print}'
>

Yes, there's more magic to it than "just" implies but
the special-casing does include an assignment to $_
in scalar context.


--
Charles DeRykus
From: John Kelly on
On Sat, 26 Jun 2010 12:14:50 -0700, Xho Jingleheimerschmidt
<xhoster(a)gmail.com> wrote:

>But a broken pipe needn't be a problem. It is merely a condition, not
>an error, unless the program decides to turn it into an error.

I prefer using:

set -e -u -o pipefail

in my bash scripts.


>instruct the other end of the pipe to just behave gracefully on SIGPIPE?

To me it's like compiling with -Wall -Werror. I just do it, and fix
the warnings. I feel better knowing (or at least thinking) that my code
is clean, not sloppy.


>(Alas, bzcat can't be so instructed, as far as I can determine.) This
>would be the ultimate in efficiency.



--
Web mail, POP3, and SMTP
http://www.beewyz.com/freeaccounts.php

From: Willem on
Tad McClellan wrote:
) I almost called Willem on the "usually" part, until I re-read
) the "Context" section in
)
) perldoc perldata
)
) ...
)
) User-defined subroutines may choose to care whether they are being
) called in a void, scalar, or list context.
)
) void context always translates to scalar context for built-in functions.
)
) void context usually translates to scalar context for user-defined functions.

What about map ?

AFAIK, when you call <map> in void context, it turns into a <for> internally.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT