Perl: the Markov chain saw PerlMonks

### Re: Adding 2 + 2

by GrandFather (Sage)
 on Jan 29, 2007 at 19:57 UTC ( #597155=note: print w/replies, xml ) Need Help??

Look ma, no numbers:

```\$|++;
my \$two = \$| + \$|;

print "\$two + \$two = ", \$two + \$two;

Prints:

```2 + 2 = 4

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Adding 2 + 2 (digit-less 1)
by ikegami (Pope) on Jan 29, 2007 at 20:46 UTC

That's really an interesting way of getting 1, not 2. Other interesting ways to get 1:

```my \$one = !@!;
my \$one = !!\$];
my \$one = /.*/;                # Side-effect: Changes \$1, etc
my \$one = \$\$/\$\$;
my \$one = ()=\$\$;
my \$one = @{[\$\$]};
my \$one = 'a'^'P';             # Character set specific.
my \$one = ord('b')-ord('a');   # Character set specific.

And 2:

```my \$two = \$one+\$one;
my \$two = \$one<<\$one;
my \$two = ()=qw(a a);
my \$two = @{[ qw(a a) ]};
my \$two = ()=/(.*)(.*)/;       # Side-effect: Changes \$1, etc
my \$two = 'a'^'S';             # Character set specific.
my \$two = ord('c')-ord('a');   # Character set specific.

None of these have side effects unless otherwise noted.
All of these run under use strict;.
All of these run silently under use warnings;.

More (although not as neat):

```my \$one = !\$|;
my \$one = cos;        # Given that \$_ == 0.
my \$one = cos \$|;
my \$one = cos atan2 \$|, cos;
my \$one = sin atan2 sin, \$|;
my \$one = length \$/;  # Note: possibly 2 under Windows or DOS.
my \$one = length \$";
my \$one = length \$;;
my \$one = length \$^L;
my \$one = \$^T/\$^T;
my \$one = @ARGV >= 0;
my \$one = grep //, (\$_);
my \$one = !undef;
# Note: the following may be Unix-only. Will not work if run as root,
# as root's UID and GID are (always?) zero.
my \$one = \$</\$<;      # User ID.
my \$one = \$>/\$>;      # Effective user ID.
my \$one = \$(/\$(;      # (Space-separated list of) group ID(s).
my \$one = \$)/\$);      # (Space-separated list of) effective group ID(s
+).

Ways to get 2:

```my \$two = length \$:;
my \$two = \$^F;        # Note: system-dependent.

--

Create A New User
Node Status?
node history
Node Type: note [id://597155]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2017-08-23 11:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Who is your favorite scientist and why?

Results (350 votes). Check out past polls.

Notices?