Skip to content

Instantly share code, notes, and snippets.

@scottwalters
Created September 9, 2014 03:21
Show Gist options
  • Save scottwalters/8965a4db3d4487de82d0 to your computer and use it in GitHub Desktop.
Save scottwalters/8965a4db3d4487de82d0 to your computer and use it in GitHub Desktop.
#!/bin/perl
use 5.010;
use strict;
use warnings;
use B::Generate;
use Opcode;
#Asking to enter a number.
print "Enter a number!\n";
#Setting a variable and waiting for user input.
my $choice1 = <STDIN>;
#Now geting the 2nd number
print "Enter a second number!\n";
my $choice2 = <STDIN>;
say "This is the addition result: " . all($choice1, $choice2, 'add');
say "This is the multiplication result: " . all($choice1, $choice2, 'multiply');
say "This is the subtract result: " . all($choice1, $choice2, 'subtract');
say "This is the divided result: " . all($choice1, $choice2, 'divide');
#This is the script fo the math.
sub all
{
my ($x, $y, $opname) = @_;
no warnings 'redefine';
*B::OP::x = sub { my $op = shift; if( grep $op->name eq $_, 'multiply', 'add', 'subtract', 'divide' ) {
my $opnum; my $mask = Opcode::opset($opname); for(1..2000) { $opnum = $_ if vec($mask, $_, 1); }
$op->type( $opnum || die );
} };
B::walkoptree_slow( B::svref_2object(\&all)->ROOT, 'x' );
my $times = $x * $y;
return $times ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment