Front page | perl.fwp |
Postings from January 2002
Re: even.pl solutions
Thread Previous
|
Thread Next
From:
Jeff 'japhy' Pinyan
Date:
January 29, 2002 10:39
Subject:
Re: even.pl solutions
Message ID:
Pine.GSO.4.21.0201291328320.27903-100000@crusoe.crusoe.net
On Jan 29, Stephen Turner said:
>On Tue, 29 Jan 2002, Jeff 'japhy' Pinyan wrote:
>>
>> Well, because $| is the magical flip-flop variable, so long as you -- it
>> and not ++ it.
>
>This I didn't know. Where is this documented? man perlvar (nope)? Camel book
>page 237 or 670 (NAFAICS)?
Well, it's "documented" in mg.c, around line 786. Here's an excerpt from
Perl_magic_get():
case '|':
sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
break;
We see that it's setting it to (...) != 0, which will be either 1 or 0.
And here's an excerpt from Perl_magic_set() (around line 1987):
case '|':
{
IO *io = GvIOp(PL_defoutgv);
if(!io)
break;
if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
IoFLAGS(io) &= ~IOf_FLUSH;
else {
if (!(IoFLAGS(io) & IOf_FLUSH)) {
PerlIO *ofp = IoOFP(io);
if (ofp)
(void)PerlIO_flush(ofp);
IoFLAGS(io) |= IOf_FLUSH;
}
}
}
break;
So if the value we are setting $| to is ZERO, flushing is turned off; for
all other values, flushing is turned on. THEN, when we look at the code
for Perl_magic_get(), and we notice that it merely returns 1 or 0 based on
whether flushing is off or on. So it retains absolutely nothing about the
value you gave $|.
So how does $|-- work? $| starts at 0. $|-- is like $| = $| - 1, which
is like $| = 0 - 1, which internally turns autoflush ON. When we do $|--
again, it's like $| = $| - 1 again, except now $| returns 1 instead of 0,
so the value is $| = 1 - 1, or 0.
Hooray for flip-flops.
--
Jeff "japhy" Pinyan japhy@pobox.com http://www.pobox.com/~japhy/
RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
Thread Previous
|
Thread Next