Skip to content

Instantly share code, notes, and snippets.

@note103
Created December 21, 2013 06:02
Show Gist options
  • Select an option

  • Save note103/8066003 to your computer and use it in GitHub Desktop.

Select an option

Save note103/8066003 to your computer and use it in GitHub Desktop.
calc_string.pl
#!/usr/bin/env perl
use strict;
use warnings;
while (my $form = <STDIN>) {
calc_string($form)
}
sub calc_string {
my $str = shift;
if ($str =~ /^(\d+)\s([\+\-\*\/\%])\s(\d+)$/) {
if ($2 eq '+') {
print $1 + $3."\n";
} elsif ($2 eq '-') {
print $1 - $3."\n";
} elsif ($2 eq '*') {
print $1 * $3."\n";
} elsif ($2 eq '/') {
print $1 / $3."\n";
} elsif ($2 eq '%') {
print $1 % $3."\n";
}
} else {
print "undef\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment