why warn on undefined $1?
why warn on undefined $1?
am 29.08.2007 22:49:07 von Jack Tanner
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.
Re: why warn on undefined $1?
am 29.08.2007 23:01:43 von xhoster
"ihok@hotmail.com" 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.
There are limits to DWIM. Perl doesn't know why you stuck the ? in, so
it can't tailor its behavior to this unknown motivation.
> 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 can do that, but I think it would make more sense to change your regex
to explicitly let it match and capture the empty string:
s/or(ed|ing|s|)$/our$1/
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
Re: why warn on undefined $1?
am 29.08.2007 23:13:59 von Mirco Wahab
ihok@hotmail.com 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.
Re: why warn on undefined $1?
am 29.08.2007 23:17:15 von Jack Tanner
On Aug 29, 5:01 pm, xhos...@gmail.com wrote:
>
> You can do that, but I think it would make more sense to change your regex
> to explicitly let it match and capture the empty string:
>
> s/or(ed|ing|s|)$/our$1/
Bingo! I didn't know you could do that. Thanks!
Re: why warn on undefined $1?
am 30.08.2007 01:01:51 von Ben Morrow
Quoth xhoster@gmail.com:
> "ihok@hotmail.com" 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.
>
> There are limits to DWIM. Perl doesn't know why you stuck the ? in, so
> it can't tailor its behavior to this unknown motivation.
>
> > 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 can do that, but I think it would make more sense to change your regex
> to explicitly let it match and capture the empty string:
>
> s/or(ed|ing|s|)$/our$1/
Or (better IMHO), use look-around assertions:
my $str = 'color colored tricolor floor';
$str =~ s/ (?<= col) or (?= (?: ed|ing|s)? \b) /our/gx;
print $str;
# colour coloured tricolour floor
Ben
Re: why warn on undefined $1?
am 30.08.2007 01:11:01 von Jack Tanner
On Aug 29, 7:01 pm, Ben Morrow wrote:
> Or (better IMHO), use look-around assertions:
>
> my $str = 'color colored tricolor floor';
> $str =~ s/ (?<= col) or (?= (?: ed|ing|s)? \b) /our/gx;
> print $str;
> # colour coloured tricolour floor
Whoa. That's heavy.
Next question: how can I store the s/// regexp in a variable? I want
something like qr//, but for s///. In other words, I want this to
work, but it doesn't.
my $re = s/or(ed|ing|s)?$/our$1/;
my $s = 'colored';
$s =~ $re;
print $s;
# coloured
Re: why warn on undefined $1?
am 30.08.2007 01:33:12 von Gunnar Hjalmarsson
ihok@hotmail.com wrote:
> how can I store the s/// regexp in a variable? I want
> something like qr//, but for s///. In other words, I want this to
> work, but it doesn't.
>
> my $re = s/or(ed|ing|s)?$/our$1/;
my $re = 's/or(ed|ing|s)?$/our$1/';
> my $s = 'colored';
> $s =~ $re;
eval "\$s =~ $re";
> print $s;
> # coloured
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Re: why warn on undefined $1?
am 30.08.2007 01:57:44 von Jack Tanner
On Aug 29, 7:33 pm, Gunnar Hjalmarsson wrote:
> i...@hotmail.com wrote:
> > how can I store the s/// regexp in a variable? I want
> > something like qr//, but for s///. In other words, I want this to
> > work, but it doesn't.
>
> > my $re = s/or(ed|ing|s)?$/our$1/;
>
> my $re = 's/or(ed|ing|s)?$/our$1/';
>
> > my $s = 'colored';
> > $s =~ $re;
>
> eval "\$s =~ $re";
>
> > print $s;
> > # coloured
Awesome. Is there any way to precompile the $re, as qr// would do?
Re: why warn on undefined $1?
am 30.08.2007 02:12:07 von Gunnar Hjalmarsson
ihok@hotmail.com wrote:
> On Aug 29, 7:33 pm, Gunnar Hjalmarsson wrote:
>> i...@hotmail.com wrote:
>>> how can I store the s/// regexp in a variable? I want
>>> something like qr//, but for s///. In other words, I want this to
>>> work, but it doesn't.
>>> my $re = s/or(ed|ing|s)?$/our$1/;
>> my $re = 's/or(ed|ing|s)?$/our$1/';
>>
>>> my $s = 'colored';
>>> $s =~ $re;
>> eval "\$s =~ $re";
>>
>>> print $s;
>>> # coloured
>
> Awesome. Is there any way to precompile the $re, as qr// would do?
This is all I can think of:
my $re = qr/or(ed|ing|s)?$/;
my $s = 'colored';
$s =~ s/$re/our$1/;
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Re: why warn on undefined $1?
am 30.08.2007 04:42:23 von Charles DeRykus
On Aug 29, 1:49 pm, "i...@hotmail.com" 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
Re: why warn on undefined $1?
am 30.08.2007 23:37:00 von rvtol+news
ihok@hotmail.com schreef:
> Ben Morrow:
>> Or (better IMHO), use look-around assertions:
>>
>> my $str = 'color colored tricolor floor';
>> $str =~ s/ (?<= col) or (?= (?: ed|ing|s)? \b) /our/gx;
>> print $str;
>> # colour coloured tricolour floor
>
> Whoa. That's heavy.
$str =~ s{ (?<= colo) (?= r(?: ed|ing|s|) \b) }
{u}gx;
--
Affijn, Ruud
"Gewoon is een tijger."