You need the e modifier:
$text =~ s/(\d+)/munge($1)/eg; ^ |

Also, it should be:

my $number = shift;

Update: Or, if you prefer engineering notation, you could try the "eng_not" sub below instead of your "munge". It's just something I have lying around:

sub eng_not { # Format a numeric value using engineering notation. # Examples: # 1234 --> 1.23k # 5e-5 --> 50u my $num = shift; my $mult; my $prefix; my $val; my $sign = ($num<0) ? '-' : ''; $num = abs $num; if ( ($num > 1e15) || ($num < 1e-15) ) { return sprintf '%3.2e', $n +um } if ($num > 1e12) { $prefix = 'T'; $mult = 1e12; } elsif ($num > 1e9) { $prefix = 'G'; $mult = 1e9; } elsif ($num > 1e6) { $prefix = 'M'; $mult = 1e6; } elsif ($num > 1e3) { $prefix = 'k'; $mult = 1e3; } elsif ($num > 1e0) { $prefix = '' ; $mult = 1e0; } elsif ($num > 1e-3) { $prefix = 'm'; $mult = 1e-3; } elsif ($num > 1e-6) { $prefix = 'u'; $mult = 1e-6; } elsif ($num > 1e-9) { $prefix = 'n'; $mult = 1e-9; } elsif ($num > 1e-12) { $prefix = 'p'; $mult = 1e-12; } $val = sprintf '%f', $num/$mult; $val = substr($val,0,4); # only want 3 digits and decimal point $val =~ s/0$//; # delete 1st trailing 0, if any $val =~ s/0$//; # delete 2nd trailing 0, if any $val =~ s/\.$//; # delete trailing decimal point, if any return $sign . $val . $prefix; }

In reply to Re: substitute with sub result? by toolic
in thread substitute with sub result? by pileofrogs

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.