Instantly share code, notes, and snippets.

Embed
What would you like to do?
calc_string.pl sample code of twice eval simple_replace option. see: https://github.com/perl-entrance-org/workshop-2013-05/blob/master/slide.md
#!/usr/bin/env perl
# https://github.com/perl-entrance-org/workshop-2013-05/blob/master/slide.md#%E7%B7%B4%E7%BF%92%E5%95%8F%E9%A1%8C-1
use strict;
use warnings;
while(my $str = <STDIN>) {
chomp $str;
my $res = calc_strings($str);
if ( defined $res ) {
print "$res\n";
} else {
print "Input Error: $str\n";
}
}
sub calc_strings {
my $str = shift;
# 文字クラス [...] の中での - は文字コード範囲になるので端っこに置く
$str =~ s|^(\d+)\s*([-+/*])\s*(\d+)$| "$1 $2 $3" |ee
or return undef;
return $str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment