in reply to avoid warnings in Copying and Substituting Simultaneously

($totcalls += $layer3) =~ s/^[\s+]([0-9]) Active.*/$1/;

I think you're confused with your order of operations ... you code will do the addition first, then the substitution (which fails, because it's operating on an integer)

ccn's solution will get rid of the warning by fixing the addition, but there's then no reason to do the substitution. I'm guessing you were probably trying to do:

$totcalls += [ $layer3 =~ m/^\s*([0-9]+) Active/ ]->[0];

... which has the advantage that it'll throw some warnings when there's bad values in $layer3:

use strict; use warnings; my $totcalls = 0; my @data = ( '2 Active Layer 3 Call(s)',' 4 Active', '12 Active', 'bla +h', undef, '' ); foreach my $layer3 (@data) { print "INPUT : $layer3 \n"; $totcalls += [ $layer3 =~ m/^\s*([0-9]+) Active/ ]->[0]; print "TOTAL : $totcalls \n"; }

Replies are listed 'Best First'.
Re^2: avoid warnings in Copying and Substituting Simultaneously
by ikegami (Patriarch) on Oct 22, 2008 at 17:58 UTC

    $totcalls += [ $layer3 =~ m/^\s*([0-9]+) Active/ ]->[0];
    needlessly creates an array and a reference. Use
    $totcalls += ( $layer3 =~ m/^\s*([0-9]+) Active/ )[0];

      And that needlessly performs a regexp. And since perl still has to do the ascii to numeric conversion, you might as well do it right away.
      { no warnings 'numeric'; $totcalls += $layer3; }
      And that will even work if $layer3 starts with a negative number or a float. It's a design feature that Perl works this way.