On Dec 4, 7:25 am, [EMAIL PROTECTED] (Ankur) wrote:
> Hi
>
> I am running the following code and receiving the warning :
>
> Use of uninitialized value in pattern match (m//) at a.pl line 15.

That is to be expected.

> a.pl :
> /
> **************************************************************************************/
> sub CommaFormatted{
>         my $delimiter = ','; # replace comma if desired
>         my($n,$d) = split /\./,shift,2;
>         my @a = ();
>         while($n =~ /\d\d\d\d/)
>         {
>                 $n =~ s/(\d\d\d)$//;
>                 unshift @a,$1;
>         }
>         unshift @a,$n;
>         $n = join $delimiter,@a;
>         $n = "$n\.$d" if $d =~ /\d/;
>         return $n;
>
> }
>
> print CommaFormatted(10000);
>
> /
> **************************************************************************************/
>
> Please help.

You are setting $d to be the second part of '10000' as delimited by
'.'.

There are no periods in '10000' so there's no such thing.

Therefore $d is set to the special undefined value. The pattern
matching operation operates on strings. The special undefined value is
not a string. It can be implicitly converted into a null string but
often needing to so this is a sign that something is wrong. That's
what the warning is saying. Actually the warning says 'uninitialized'
when it means 'undefined'. I have always considered this to be a bug
in Perl.

As it happens, even if there was a '.' as the last character in your
input string then you'd still get the same message. This is counter-
intuitive but is documented in the documentation of the split()
function.

You should explicitly check $d is defined or suppress this warning for
the duration of the pattern match

         $n .= ".$d" if do { no warnings 'uninitialized'; $d =~ /
\d/ };

Actually I can't see why you do the pattern match at all...

         $n .= ".$d" if defined $d;


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to