TMTOWTDI in action

ziggy on 2001-03-06T02:50:49

CanSpice writes "The company I work for has just come up with voluntary lunchtime seminars to be put on by fellow employees. I figured that an hour on Perl and its informal slogan "There's more than one way to do it" would be a fine topic. So, I throw a challenge out to all the Perl programmers out there: how would you write a Perl program that would add one plus one?

I've seen this before, but have no idea where. I'm looking for fresh solutions as well, solutions that incorporate potential new features of Perl 6, along with the old diehards (bit mangling, regexps, blatant misuse of special variables, etc.)."

The classic 163-line Perl program to add 1 + 1 is mjd's Perl and the Lambda-Calculus.

Lunchtime seminars can be an interesting way to share ideas about Perl in the office. If you have a story, please tell us about it. :-)


TIMTOWTDI in action and $A++

Jean FORGET on 2001-03-06T06:37:10

We Paris Perl Mongers have not tried to compute 1+1 , but we have tried to add 1 to the variable $A. See the results in http://paris.mongueurs.net/aplusplus.html

use the award-winning Inline module

unimatrix on 2001-03-06T07:32:02

use Inline C => <<'EOC';

int one_plus_one() {
  return 1 + 1;
}

EOC

print one_plus_one();

pid nastiness

james on 2001-03-06T12:50:13

While not particularly obfuscated on its own, encountering this in a large program may be particularly hairy.

$$/$$+$$/$$

Ideas for solutions

$Bob on 2001-03-06T22:52:33

Create a class One and overload the '+' operator for that class.

Write the binary logic by hand using and (&), or (|), xor (^), shifts (<<, and >>), and simple loops.

Go wacky with Roman numerals.

Other wacky code snippets:
"1+"+"+1"

"1+1"+"1+1"

$\=$/;"1+1"=~/(.)[+](.)/; print $++$+;

print print."+".print."=".((print)+(print))."\n";

Gotta love recursion

cees on 2001-03-06T23:40:15

use POSIX qw(ceil);

my $one_plus_one = sum(ceil(rand), cos());

sub sum {
        @_ && (shift)- -sum(@_);
}

btw - this code will fail to produce a correct answer every once in a while :)

store the data in the program

chromatic on 2001-03-08T19:47:46

my $val;

sysseek(DATA, -10, 2);
sysread(DATA, $val, 1);

$val += $val;
print "$val\n";
1
__END__

Oh, for a CODE tag like on PerlMonks...

Re:store the data in the program

pudge on 2001-03-14T19:37:36

CODE? You mean like this?

my $val;

sysseek(DATA, -10, 2);
sysread(DATA, $val, 1);

$val += $val;
print "$val\n";
1
__END__

Re:store the data in the program

chromatic on 2001-03-16T07:18:03

If I were smarter, I would have either consulted the Everything source to verify that there's no special hackery, or the HTML & XHTML book.

pudge, you're my hero once again.