Front page | perl.fwp | Postings from March 2012

## Re: The sperm secret operator: is it new?

From:
Yitzchak Scott-Thoennes
Date:
March 14, 2012 14:57
Subject:
Re: The sperm secret operator: is it new?
Message ID:
CAN7g7HVEm9mP0+jSojSN4M+atgHuJaqt+COOf20vkVdUvB6tWA@mail.gmail.com
```On Wed, Mar 14, 2012 at 7:48 AM, John Douglas Porter
<johndporter@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

```