Re: why warn on undefined $1?



ihok@xxxxxxxxxxx wrote:
Consider:

# colors -> colours
$token = 'colors';
$token =~ s/or(ed|ing|s)?$/our$1/;

But if $token == 'color', Perl emits a warning: Use of uninitialized
value in concatenation (.) or string. True enough, $1 is undefined,
but why bother warning? I mean, my regexp has a '?' in it because I
expect that sometimes 'color' will not have an ending.

xhoster did already point to a solution,
but you could also be more explicit about
your search term, e.g.

my $token = 'color';
$token =~ s/ \B or(ed | ing | s | $)? /our$1/x;

by simply putting the $ (or a \b) into the
capturing parenthesis. I added another \B
in front of the term in order to make
clear that we start the search in the
middle of a word.


.



Relevant Pages

  • Re: Identify source of warning
    ... Suddenly I cannot recreate the warning. ... Use of uninitialized value $OutputFile in concatenation or ... string at ../keywa/kywpl line 67. ...
    (perl.beginners)
  • Newbie: Testing for Null
    ... Use of uninitialized value in concatenation or string at ... To suppress this warning assign a defined value to your variables. ... To help you figure out what was undefined, perl tells you what ...
    (comp.lang.perl.misc)
  • Re: why warn on undefined $1?
    ... Perl emits a warning: ... value in concatenation or string. ... but why bother warning? ...
    (comp.lang.perl.misc)
  • Re: why warn on undefined $1?
    ... Perl emits a warning: ... value in concatenation or string. ... but why bother warning? ...
    (comp.lang.perl.misc)
  • Re: Function Warning - Null Reference
    ... Warning 1 Function 'Dec2hms' doesn't return a value on all code paths. ... These include String, Integer, DateTime and Boolean ...
    (microsoft.public.dotnet.languages.vb)