$| (undocumented) magic?

M

Michele Dondi

While trying my hand at a new japh[*], I've found that $| can only
hold either 0 or 1 (I guess):

# perl -le 'print $|=10'

Though I can't seem to find any mention of this fact in 'perldoc
perlvar'. BTW: is this behaviour supposed to be supported in the
future too? And is it always been?


[*] FWIW, basically I had a piece of code like s/./1&ord$&/ge; but
then I also needed to have $|=1, and since I didn't want yet another
statement (it revealed to be more of a golfing exercise than a japhing
one) I changed it to s/./++$|&ord$&/ge; which *does* work fine, only
that, thinking of it better I realized that had $| been a "normal"
variable, it shouldn't have!


Michele
 
A

Ala Qumsieh

Michele said:
While trying my hand at a new japh[*], I've found that $| can only
hold either 0 or 1 (I guess):

# perl -le 'print $|=10'

What's more interesting is the magic associated with decrementing $|:

perl -le 'print --$| for 1 .. 10'

--Ala
 
J

Jeff 'japhy' Pinyan

[posted & mailed]

While trying my hand at a new japh[*], I've found that $| can only
hold either 0 or 1 (I guess):

# perl -le 'print $|=10'

Though I can't seem to find any mention of this fact in 'perldoc
perlvar'. BTW: is this behaviour supposed to be supported in the
future too? And is it always been?

The C code implementing $| basically says "if the value assigned to $| is
true, have $| return 1, otherwise have it return 0". This leads to the
coolness that is

print +("this", "that")[--$|] for 1 .. 10;

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

Michele Dondi

What's more interesting is the magic associated with decrementing $|:

perl -le 'print --$| for 1 .. 10'

I know, I've done some experiments myself, "obviously"! ;-) As Jeff
"japhy" Pinyan pointed out, it's really magic associated with the
underlying code implementing $|.


Michele
 
M

Michele Dondi

The C code implementing $| basically says "if the value assigned to $| is
true, have $| return 1, otherwise have it return 0". This leads to the

(Having not seen the actual code, and not being to) I *think* that
basically it should say "if the result of an assignment is true then
actually store 1 in it, otherwise store 0 in it". Isn't this closer?
Thank you for the kind explanation, anyway...


Michele
 
J

Jeff 'japhy' Pinyan

(Having not seen the actual code, and not being to) I *think* that
basically it should say "if the result of an assignment is true then
actually store 1 in it, otherwise store 0 in it". Isn't this closer?
Thank you for the kind explanation, anyway...

Nothing is ever "stored" in $|, just like nothing is ever "stored" in a
tied variable. $| is like a tied variable, but on an internal level.
When you set $| to a true value, the internals turn off buffering for the
currently selected filehandle, and when you read $|'s value, Perl checks
to see if the currently selected filehandle has buffering on or off. $|
is just a placeholder.

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

Michele Dondi

Nothing is ever "stored" in $|, just like nothing is ever "stored" in a
tied variable. $| is like a tied variable, but on an internal level.
When you set $| to a true value, the internals turn off buffering for the
currently selected filehandle, and when you read $|'s value, Perl checks
to see if the currently selected filehandle has buffering on or off. $|
is just a placeholder.

I don't want to argue on this particular topic, especially since I
guess we both know what we're talking about, and I'm still thanking
you for your knowledgeable explanation. However it's obvious that even
if technically "nothing is ever stored" in $|, it maintains a
knowledge of its current status, which is what I meant...


Respectfully thanking you once again,
Michele
 
J

J. Romano

The C code implementing $| basically says "if the value
assigned to $| is true, have $| return 1, otherwise have
it return 0". This leads to the
[/QUOTE]

Michele Dondi said:
(Having not seen the actual code, and not being to) I
*think* that basically it should say "if the result of
an assignment is true then actually store 1 in it,
otherwise store 0 in it". Isn't this closer?


Forgive me for bringing this up one more time, Michele. I've been
thinking about this, and it seems to me that $| may have been
implemented to return 1 (if true) in order to behave like a boolean
variable. This would be an advantage when comparing to boolean
variables, like in this example:

Say that both $| and $a are booleans. $| was set to 5, and $a was
set to 1. If you compare them with the == operator, should it
evaluate to true or false? In other words, should the following code
print "True"?:

$| = 5;
$a = 1;
print "True" if $| == $a;

If you're comparing their boolean states (whether they are both
true or both false), then yes, it should print true. But if you're
comparing actual numerical values, then they should print false. In
this case, it prints "True" because of that quirk you found with $|,
which may have been deliberately put in to make $| behave as if it
only had true/false states. If it weren't for this feature, you could
compare $| and $a's boolean states another way with "not" and "xor,"
like this:

print "True" if not ($| xor $a);

I happen to think that the '==' operator is more readable.
Unfortunately, in order to use '==' for boolean comparisons you have
to know which variables behave like booleans and which don't.

-- Jean-Luc
 
A

Anno Siegel

Michael Slass said:
Isn't that the whole point of a JAPH --- to show how clever the owner
is at writing obscure perl --- and for the puzzle-solving amusement
of the rest of us, figuring out how/why a particular JAPH works?

A japh doesn't *have* to be obfuscated. It is _just another_ way
to print that particular string from four lines of sig.

Anno
 
M

Michele Dondi

Yeah, but what you practice is what you do by habit later. Do you
*really* want JAPH-style code to appear in production scripts?

Not at all, and I don't think I do that. Nor do I think others,
including well known JAPH writers/golfers, do.
Also, JAPHs as sigs tend to give a bad impression of the language to
those who've never seen any other Perl. They promote the idea of an
insular community that delights in writing obscure code that's difficult
for newbies and outsiders to grok.

This is true, though. I must admit it! But it won't stop me from
writing some, every now and again, I guess...

BTW: I've never ever sit down thinking to myself: well, now I'm making
a JAPH. It's always come out of something different. Well, sort of...


Michele
 
M

Michele Dondi

Isn't that the whole point of a JAPH --- to show how clever the owner
is at writing obscure perl --- and for the puzzle-solving amusement
of the rest of us, figuring out how/why a particular JAPH works?

Not at all: it's not necessarily an obfuscation thing. Not necessarily
in the sense of *code* obfuscation: some times it has to do with
*data* obfuscation. And some times it's not about obfuscation at all,
but it's focused on some witty/cool/whatever way to print that
particular string. Think of Abigail's magnificent ones, for example:
often you *do* understand where the data are stored and you can even
grasp more or less what the code does. But you're amazed at *how* it
does it. And in some cases you wonder why the program compiles at all!


Michele
 
J

J. Romano

Michele Dondi said:
Forgive me for this answer that most likely won't
satisfy you, Jean-Luc. I've been thinking about this,
and it seems to me that $| may have been implemented
that way to help me finding the final form for my most
recent JAPH


I'm not sure if your reply was meant to be taken as sarcasm, but
either way... good for you! It's always good to learn something new
when creating new JAPHs!

-- Jean-Luc
 
A

Ala Qumsieh

Sherm said:
Yeah, but what you practice is what you do by habit later. Do you
*really* want JAPH-style code to appear in production scripts?

No, but that doesn't mean that JAPHs and golf promote "bad" habits
(whatever the definition of "bad" might be). I have never spent too much
time on JAPHs, but have done so on perlgolf (over at perlgolf.org during
its earlier days). And, I must admit, I learnt lots of useful techniques
and discovered a lot of interesting properties of Perl's built-in vars
in the process of squeezing just one more character out of my code. As a
matter of fact, I learnt about $|--'s magic when one person used it in
his golf code to alternate between two elements, while everyone else
used some variation of ++$i%2. It was exciting and very insightful to
figure those things out.

I take all of this as a form of edutainment and don't allow it into
production code. Afterall, watching a violent movie doesn't make a
(normal ;) person want to go out on a rampage.
Also, JAPHs as sigs tend to give a bad impression of the language to
those who've never seen any other Perl. They promote the idea of an
insular community that delights in writing obscure code that's difficult
for newbies and outsiders to grok.

They're just jealous because they can't write any valid Python program
in just four lines (/me ducks and runs for cover).

--Ala
 
J

Joe Smith

Michele said:
I don't want to argue on this particular topic, especially since I
guess we both know what we're talking about, and I'm still thanking
you for your knowledgeable explanation. However it's obvious that even
if technically "nothing is ever stored" in $|, it maintains a
knowledge of its current status, which is what I meant...

linux% perl -MDevel::peek -e '$a=2;Dump $a'
SV = IV(0x8139674) at 0x8139b80
REFCNT = 1
FLAGS = (IOK,pIOK)
IV = 2

linux% perl -MDevel::peek -e '$|=2;Dump $|'
SV = PVMG(0x8154fc8) at 0x8139b80
REFCNT = 1
FLAGS = (GMG,SMG,pIOK)
IV = 2
NV = 0
PV = 0
MAGIC = 0x813b028
MG_VIRTUAL = &PL_vtbl_sv
MG_TYPE = PERL_MAGIC_sv(\0)
MG_OBJ = 0x8139bd4
MG_LEN = 1
MG_PTR = 0x813bc68 "|"

linux% perldoc perlguts | grep -i magic


-Joe
 
K

krakle

Well we can't write _any_ valid program in four lines

#!/usr/bin/perl -w
print "What's your name?";
my $name = <STDIN>;
print "Hi $name I just wanted to show you a valid Perl program in 4
lines. Isnt this valid?\n";
 
M

Michele Dondi

I'm not sure if your reply was meant to be taken as sarcasm, but
either way... good for you! It's always good to learn something new

Wasn't it clear enough? ;-)
No offence intended, of course!


Michele
 

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,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top