What does this do?

E

Eugene Mikheyev

$|++;
I see that in code but my searches have come up with nothing.
As $| is a boolean variable, I suspect the one who wrote this thought
incrementing is faster than assigning, i.e. when $x is 0, $x++ is faster
than $x = 1. Maybe I am wrong, this is just an assumption.
 
G

gnari

Bob said:
$|++;

I see that in code but my searches have come up with nothing.

$| is explained in
perldoc perlvar

the ++ is just a cute way to set it to a true value. if assumes
the value is not -1

gnari
 
P

Peter J. Acklam

$|++;

I see that in code but my searches have come up with nothing.

perldoc perlvar

It is a slightly obscure way of disable output buffering. The
recommended way is

$| = 1;

Peter
 
S

Sherm Pendley

Bob said:
$|++;

I see that in code but my searches have come up with nothing.

If it helps, the default value of $| is undef, or zero in numeric context,
so incrementing it with ++ makes it non-zero. The results of assigning a
non-zero value to $| are documented in 'perldoc perlvar'.

sherm--
 
B

Brad Baxter

$| is explained in
perldoc perlvar

the ++ is just a cute way to set it to a true value. if assumes
the value is not -1

gnari

The value is NEVER -1. :)

Regards,

Brad
 
P

Peter J. Acklam

Abigail said:
There's no advantage of writing '$| ++' instead of '$| = 1', but
the latter is far less obscure.

And the latter turns on buffering also in the extremely unlikely
event that $| should ever be -1, in which case $|++ would turn it
off.

Peter
 
J

Jeff 'japhy' Pinyan

And the latter turns on buffering also in the extremely unlikely
event that $| should ever be -1, in which case $|++ would turn it
off.

Except that $| can't ever be -1 (unless you've done *| = \-1, in which
case $|++ will throw a "Modification of read-only" error).

$|, as defined by the internal magic supporting it, is set to 0 when given
a false value, and 1 when given ANY true value.

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
RPI Corporation Secretary % have long ago been overpaid?
http://japhy.perlmonk.org/ %
http://www.perlmonks.org/ % -- Meister Eckhart
 
S

Sam Holden

And the latter turns on buffering also in the extremely unlikely
event that $| should ever be -1, in which case $|++ would turn it
off.

$| is a special variable it can never have the value of -1 and hence that
event is not possible at all.

It is either 0 or 1.

perl -le "$|=-1;print $|"
 
G

gnari

Jeff 'japhy' Pinyan said:
$|, as defined by the internal magic supporting it, is set to 0 when given
a false value, and 1 when given ANY true value.

in that case the $|++ does not even have the advantage
of allowing us to 'localize' the value as in:

# unknown $| state
$|++;
... do stuff ...
$|--;
this will alway end by resetting $| to 0, even if it
was originally 1

on the other hand $|-- will always reverse the value.
now I will have to find a use for that ...

gnari
 
A

Ala Qumsieh

P

Peter J. Acklam

Jeff 'japhy' Pinyan said:
Except that $| can't ever be -1 (unless you've done *| = \-1, in
which case $|++ will throw a "Modification of read-only" error).

Darn, then someone has fooled me. What I wrote above is virtually
a citation of something I read somewhere. No big deal, though,
but now I wonder where I got it from.

Peter
 
J

Jeff 'japhy' Pinyan

in that case the $|++ does not even have the advantage
of allowing us to 'localize' the value as in:

# unknown $| state
$|++;
... do stuff ...
$|--;
this will alway end by resetting $| to 0, even if it
was originally 1

Luckily we have 'local' which does exactly what you want.

# unknown $| state
{
local $| = 1;
# ...
}
# $| is restored here

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
RPI Corporation Secretary % have long ago been overpaid?
http://japhy.perlmonk.org/ %
http://www.perlmonks.org/ % -- Meister Eckhart
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top