Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > why warn on undefined $1?

Reply
Thread Tools

why warn on undefined $1?

 
 
ihok@hotmail.com
Guest
Posts: n/a
 
      08-29-2007
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.

 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      08-29-2007
"(E-Mail Removed)" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Mirco Wahab
Guest
Posts: n/a
 
      08-29-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.


 
Reply With Quote
 
ihok@hotmail.com
Guest
Posts: n/a
 
      08-29-2007
On Aug 29, 5:01 pm, (E-Mail Removed) 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!

 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      08-29-2007

Quoth (E-Mail Removed):
> "(E-Mail Removed)" <(E-Mail Removed)> 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
 
Reply With Quote
 
ihok@hotmail.com
Guest
Posts: n/a
 
      08-29-2007
On Aug 29, 7:01 pm, Ben Morrow <(E-Mail Removed)> 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

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-29-2007
(E-Mail Removed) 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
 
Reply With Quote
 
ihok@hotmail.com
Guest
Posts: n/a
 
      08-29-2007
On Aug 29, 7:33 pm, Gunnar Hjalmarsson <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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?

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-30-2007
(E-Mail Removed) wrote:
> On Aug 29, 7:33 pm, Gunnar Hjalmarsson <(E-Mail Removed)> wrote:
>> (E-Mail Removed) 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
 
Reply With Quote
 
comp.llang.perl.moderated
Guest
Posts: n/a
 
      08-30-2007
On Aug 29, 1:49 pm, "(E-Mail Removed)" <(E-Mail Removed)> 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




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does "warn" print a extra \n ? IƱaki Baz Castillo Ruby 3 01-05-2010 01:29 PM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Why GCC does warn me when I using gets() function for accessing file Cuthbert C Programming 89 09-11-2006 02:59 PM
Is there some reason why warn and die would be very very slow? Sara Perl Misc 0 07-11-2003 03:10 PM



Advertisments