Help for this page

Select Code to Download


  1. or download this
    chomp(my $left = <STDIN>);
    chomp(my $op = <STDIN>);
    ...
          $op eq '/' ? $left / ($right||1) :
          "dunno operator '$op'",
    , "\n";
    
  2. or download this
    chomp( my $expr = <STDIN> );
    # and to parse it with simple split
    my ($left,$op,$right) = split /\s+/,$expr
    # next step is code of above
    
  3. or download this
    my %operations = (
      '+' => sub { (shift) + (shift) },
    ...
        $operations{$op}->($left,$right) :
        "dunno operator '$op'",
    "\n";
    
  4. or download this
    chomp(my $expr = <STDIN>);
    $expr =~ /(\d+)\s+([+-/*])\s+(\d+)/ or
       die "invalid input\n";
    my ($left,$op,$right) = ($1,$2,$3);
    # ...