Re: why warn on undefined $1?



On Aug 29, 1:49 pm, "i...@xxxxxxxxxxx" <i...@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.

I suspect that the answer is "it's simpler to just warn whenever an
undefined variable occurs in a string, and it's just not worth it to
detect the case when such a warning is vacuous. Try 'no warnings'.' I
can deal with that.

You could add an empty alternative to cause
$1 to match an empty string thus preventing
a warning:

$token =~ s/or(ed|ing|s|)?$/our$1/;

--
Charles DeRykus




.



Relevant Pages

  • 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)
  • Re: Function Warning - Null Reference
    ... Warning 1 Function 'Dec2hms' doesn't return a value on all code paths. ... null reference exception could occur at run time when the result is used. ... default value for a String is String.Empty, the default value for an Integer ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Visual Studio 2005 - Warning in the Error List Window
    ... I'm receiving the same warning, and although this has been brought up ... Public sBillingAddress1 As String ... Private Sub LoadAddress() ... Dim Address As New addressEntry ...
    (microsoft.public.dotnet.languages.vb)
  • 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)