Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

The sperm secret operator: is it new?

7 views
Skip to first unread message

Philippe Bruhat (BooK)

unread,
Mar 14, 2012, 7:36:10 AM3/14/12
to f...@perl.org
Hi,

While doing some tests/research on secret operators, I stumbled upon this
one in my one-liners:

~~<>

Obviously, this should be named "the sperm operator". It's only useful
in list context. Just like sperm cells work better when there are many
of them. ;-)


Now, if you want to get a number from that line you just read, you
could use this variation:

~~<>+0

which we could name the fertilisation, conception, fecundation or
syngamy operator, except the flagella (~~) has become useless, since
the +0 operator (ovum?) already provides scalar context. So it's just
the same as <>+0.

I have no idea if this operator is really new, or if someone else found
and named it already before today. My Google-fu is too weak to make
useful searches on it.

--
Philippe Bruhat (BooK)

No matter who you may be, there is always someone who is a little worse
because he thinks he is a little better.
(Moral from Groo The Wanderer #3 (Epic))

Philippe Bruhat (BooK)

unread,
Mar 14, 2012, 8:29:08 AM3/14/12
to f...@perl.org
On Wed, Mar 14, 2012 at 12:36:10PM +0100, Philippe Bruhat (BooK) wrote:
> Hi,
>
> While doing some tests/research on secret operators, I stumbled upon this
> one in my one-liners:
>
> ~~<>
>
> Obviously, this should be named "the sperm operator". It's only useful
> in list context. Just like sperm cells work better when there are many
> of them. ;-)
>

Example usage:

# the first three lines constitute the header
@header = ( ~~<>, ~~<>, ~~<> );

--
Philippe Bruhat (BooK)

The only way to get a better government is to get better voters.
(Moral from Groo The Wanderer #109 (Epic))

John Douglas Porter

unread,
Mar 14, 2012, 10:48:35 AM3/14/12
to f...@perl.org

So is that the Perl 6 smart match operator? or something else?
In any case... How does it work here? It looks like it's functionally
equivalent to scalar()... but why?
--
jdporter


--- On Wed, 3/14/12, Philippe Bruhat (BooK) <philipp...@free.fr> wrote:

Sebastian Schmidt

unread,
Mar 14, 2012, 10:51:09 AM3/14/12
to f...@perl.org
On Wed, Mar 14, 2012 at 07:48:35AM -0700, John Douglas Porter wrote:
[ ~~ vs. scalar ]
>
> So is that the Perl 6 smart match operator? or something else?
> In any case... How does it work here? It looks like it's functionally
> equivalent to scalar()... but why?

It's twice bitwise not, and bitwise not enforces scalar context.
Think ~(~(foo)).

Andrew Savige

unread,
Mar 14, 2012, 4:46:34 PM3/14/12
to Sebastian Schmidt, f...@perl.org
> [ ~~ vs. scalar ]

The ~~ secret operator is old hat, good ol' inchworm:

 http://www.catonmat.net/blog/secret-perl-operators/#inchworm

BooK's innovation is to add <> and <>+0 to the end of it.

BTW, in addition to inchworm-on-a-stick ~- to subtract one,
I often use the converse -~ to add one (though only in Ruby
and Python, not usually Perl). For example, -~1 produces 2

in Ruby and Python, but -4,294,967,294 in Perl.


/-\

Yitzchak Scott-Thoennes

unread,
Mar 14, 2012, 5:57:09 PM3/14/12
to f...@perl.org
On Wed, Mar 14, 2012 at 7:48 AM, John Douglas Porter
<johnd...@yahoo.com> wrote:
> So is that the Perl 6 smart match operator? or something else?

The latter.

> In any case... How does it work here?  It looks like it's functionally
> equivalent to scalar()... but why?

Perl's ~ is operand sensitive; if its operand has a numeric value
(either because it was assigned a number, the result of a numeric
operation, or had been used in numeric context), it is a numeric
bitwise or (implicitly converting to UV, or under the scope of use
integer, IV, first); otherwise it is a string bitwise or.

For most inputs, in either case it can be repeated to reproduce the
original value and acts just like scalar().

Examples of exceptions:

# floating point

$x = 1.23; print ~~$x; # 1

# string used in numeric context

$x = "1.23"; print ~~$x if $x != 0; # 1

# integer out of range

use Config '%Config';

$x=2**(8*$Config{uvsize}); print ~~$x; # max uv

$x = -1; print ~~$x; # max uv

$x = 2**(8*$Config{uvsize}-1); use integer; print ~~$x; # min iv

$x = -2**(8*$Config{uvsize}-1)-1; use integer; print ~~$x; # min iv

Philippe Bruhat (BooK)

unread,
Mar 14, 2012, 6:50:45 PM3/14/12
to f...@perl.org
It works in C too. I was doing some research on secret operators today,
and I discovered the effects of the other inchworm-on-a-stick, and the
fact that both operators are broken for half the integers in Perl.

~- only decrements integers greater than 0 in Perl.
-~ only increments integers lesser than 0 in Perl.

According to Abigail and rgs, it's probably because ~ must also handle
strings. Abigail and I looked at the source of pp_negate, and it seems
like it does the right thing, so ~ seems to be the culprit. (I see that
tzchak Scott-Thoennes has provided a thorough answer in another mail.)

Frankly, I think this could be considered a bug. Both the left-facing
and right-facing versions of the inchworm on a stick should work on
all integers in Perl. Complement two arithmetics demand it! Now, the
question is, how long has this been broken in Perl? Forever?

--
Philippe Bruhat (BooK)

The learned man makes a mistake but once... but the truly stupid keep
practicing until they get it right.
(Moral from Groo The Wanderer #75 (Epic))

Yitzchak Scott-Thoennes

unread,
Mar 14, 2012, 7:02:41 PM3/14/12
to f...@perl.org
On Wed, Mar 14, 2012 at 3:50 PM, Philippe Bruhat (BooK)
<philipp...@free.fr> wrote:
> Frankly, I think this could be considered a bug. Both the left-facing
> and right-facing versions of the inchworm on a stick should work on
> all integers in Perl. Complement two arithmetics demand it! Now, the
> question is, how long has this been broken in Perl? Forever?

I don't consider it a bug. ~ has to either assume signed or unsigned.

You get a choice by whether you do it under the scope of use integer
(implicit: comma signed) or not.

Dmitry Karasik

unread,
Mar 15, 2012, 1:25:49 AM3/15/12
to f...@perl.org
>> Hi,
>>
>> While doing some tests/research on secret operators, I stumbled upon this
>> one in my one-liners:
>>
>> ~~<>
>>
>> Obviously, this should be named "the sperm operator".


Looks more like a "kite" operator to me.


--
Sincerely,
Dmitry Karasik

Philippe Bruhat (BooK)

unread,
Mar 15, 2012, 4:22:58 AM3/15/12
to f...@perl.org
On Thu, Mar 15, 2012 at 06:25:49AM +0100, Dmitry Karasik wrote:
> >>
> >> While doing some tests/research on secret operators, I stumbled upon this
> >> one in my one-liners:
> >>
> >> ~~<>
> >>
> >> Obviously, this should be named "the sperm operator".
>
>
> Looks more like a "kite" operator to me.
>

Does that mean I have a dirty mind?

--
Philippe Bruhat (BooK)

Law is the best deterrent to Justice.
(Moral from Groo The Wanderer #90 (Epic))

Daniel Cutter

unread,
Mar 15, 2012, 5:53:05 AM3/15/12
to f...@perl.org, Philippe Bruhat (BooK)
I would think it means you live in an area where it's not very windy.

*Daniel Cutter*
s/\b[^a]/\u$&/g,s;$;,;,print for join$,,map{('acehjklnoprstu'
=~m(.)g,$")[/\d/?$_:hex]}q(4dbce078c32ae92a6e30152aff)=~m(.)g

Dmitry Karasik

unread,
Mar 15, 2012, 5:40:17 AM3/15/12
to f...@perl.org

> >> While doing some tests/research on secret operators, I stumbled upon
>> this >> one in my one-liners:
>> >> ~~<>
>> >> Obviously, this should be named "the sperm operator".
>> Looks more like a "kite" operator to me.
> Does that mean I have a dirty mind?

At least not dirtier than the inventor of the =()= (unless that was you ;)
... anyway I think it'll be adopted better as "kite", and well also, where
did you see a four-squared sperm? :)

--
Sincerely,
Dmitry Karasik

Dmitry Karasik

unread,
Mar 15, 2012, 10:45:47 AM3/15/12
to f...@perl.org
>>> ~~<>
>> Looks more like a "kite" operator to me.
> Does that mean I have a dirty mind?

That means that there's no such thing as a four-squared sperm!

(ps my previous answer apparently didn't pass to the list, this is a shorter version of it anyway :)

--
Sincerely,
Dmitry Karasik

Philippe Bruhat (BooK)

unread,
Mar 15, 2012, 11:35:23 AM3/15/12
to f...@perl.org
On Thu, Mar 15, 2012 at 03:45:47PM +0100, Dmitry Karasik wrote:
> >>> ~~<>
> >> Looks more like a "kite" operator to me.
> > Does that mean I have a dirty mind?
>
> That means that there's no such thing as a four-squared sperm!
>

Not that =()= resembles much to a... Hrmpf.

See also http://en.wikipedia.org/wiki/Sperm.
The side view is four-squared enough for me.

The limitations of ASCII shouldn't limit our imaginations!

--
Philippe Bruhat (BooK)

Too many believe only in the belief.
(Moral from Groo The Wanderer #58 (Epic))

Philippe Bruhat (BooK)

unread,
Mar 16, 2012, 7:43:42 AM3/16/12
to f...@perl.org
On Thu, Mar 15, 2012 at 10:40:17AM +0100, Dmitry Karasik wrote:
>
> > Does that mean I have a dirty mind?
>
> At least not dirtier than the inventor of the =()= (unless that was you ;)

I think the first person using =()= (Randal?) is not the same as
the first person naming it in a Perl context. Both probably have been
invented independently, as goatse was a popular meme at some point, and
people collected references to it. It's not suprising that a minimalist
ascii-art version appeared. And that it looked like Perl code.

--
Philippe Bruhat (BooK)

The man who most obeys the king is the man who gets crowned.
(Moral from Groo The Wanderer #13 (Epic))
0 new messages