in reply to Call sub first or later

It makes sense to move the conditional inside the subroutine as it shouldn't be operating on $str unless it is in the valid format. A small addition that could be made is some additional dwimmery to the subroutine such as modification of the string inplace and working on multiple values e.g
sub fraction { return @_ unless @_ == grep /\d+_\d+/, @_; my $args = defined wantarray ? [@_] : \@_; $_ = convert_num_here($_) for @$args; return wantarray ? @$args : $args[-1]; }
Also, if you're working with fractions davorg's Number::Fraction might be of interest.
HTH

_________
broquaint

Replies are listed 'Best First'.
Re: Re: Call sub first or later
by kiat (Vicar) on Apr 26, 2004 at 16:57 UTC
    Thanks, broquaint!

    I had been wondering why there wasn't a module for fractions. Will take a look at davorg's Number::Fraction.

Re: Re: Call sub first or later
by tilly (Archbishop) on Apr 27, 2004 at 01:06 UTC
    Might..or might not.

    Before recommending modules that aren't widely used, I think it is good to glance at the source-code and look for obvious problems. In this case the module is clearly "interesting proof of concept with significant limitations." Most significantly, it doesn't handle negative numbers.

    (I'll send a message to davorg about that.)

      Version 1.03 of Number::Fraction fixes the problem with negative numbers.

      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg