You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@spamassassin.apache.org by Per Jessen <pe...@computer.org> on 2010/06/10 08:36:35 UTC

perl experts - \1 better written as $1 ?

I have a bit of SA code where I strip leading and trailing whitespace - 

foreach (@addrs) { s/^\s*([^\s]+)\s*$/\1/; }

Whenever I run this I get the warning "\1 better written as $1" which I
understand to be perl telling me that the right side of s/// should use
$digit, not \digit.  I tried changing it to $1, but that didn't produce
the expected result.  What would be the correct way to write this?


/Per Jessen, Zürich


Re: perl experts - \1 better written as $1 ?

Posted by Bowie Bailey <Bo...@BUC.com>.
Per Jessen wrote:
> I have a bit of SA code where I strip leading and trailing whitespace - 
>
> foreach (@addrs) { s/^\s*([^\s]+)\s*$/\1/; }
>
> Whenever I run this I get the warning "\1 better written as $1" which I
> understand to be perl telling me that the right side of s/// should use
> $digit, not \digit.  I tried changing it to $1, but that didn't produce
> the expected result.  What would be the correct way to write this?

I don't see any problems with changing /1 to $1 in that regex.  It gives
the same results either way when I try it.  Of course, it fails
completely if there is whitespace in the middle of the string.

What I always do to strip leading and trailing whitespace is this:

s/^\s+|\s+$//g

-- 
Bowie

Re: perl experts - \1 better written as $1 ?

Posted by Per Jessen <pe...@computer.org>.
Mark Martinec wrote:

> Per,
> 
>> >> There are no spaces in the string, it's an email address.
> 
> An email address can legitimately contain a space, see RFC 2822,
> e.g.
>   "some x user"@example.com

Yeah I know, but I'm quietly ignoring that possibility.  I haven't seen
one the last four-five years.  Makes me wonder if e.g. blacklist_from
is able to deal with an address like that?


Thanks for all of your suggestions.


/Per Jessen, Zürich


Re: perl experts - \1 better written as $1 ?

Posted by Mark Martinec <Ma...@ijs.si>.
Per,

> >> There are no spaces in the string, it's an email address.

An email address can legitimately contain a space, see RFC 2822,
e.g.
  "some x user"@example.com
 
> >> I did try using $1 on the right side of the s///, but it didn't work.

Most weird. Which version of perl?

Try this:

$ perl -le '@a=@ARGV; s/^\s*(.*?)\s*$/$1/ for @a; print "/$_/" for @a' \
"x " " y" " z z "

should produce:
/x/
/y/
/z z/


> Well, as the \1 variation worked apart from the warning, I didn't bother
> with going any further.  I have a feeling the $1 might be getting
> substituted first?  so maybe I should escape it?  Anyway, thanks for
> your suggestion, it's much better.

No escaping, the $1 should work as show.

  Mark

Re: perl experts - \1 better written as $1 ?

Posted by Per Jessen <pe...@computer.org>.
Karsten Bräckelmann wrote:

> On Thu, 2010-06-10 at 12:08 +0200, Per Jessen wrote:
>> > > I have a bit of SA code where I strip leading and trailing
>> > > whitespace
>> > > 
>> > >   foreach (@addrs) { s/^\s*([^\s]+)\s*$/\1/; }
>> > > 
>> > > Whenever I run this I get the warning "\1 better written as $1"
>> > > which I understand to be perl telling me that the right side of
>> > > s/// should
>> > > use $digit, not \digit.  I tried changing it to $1, but that
>> > > didn't
>> > > produce the expected result.  What would be the correct way to
>> > > write this?
> 
>> There are no spaces in the string, it's an email address.
> 
> So you don't actually want to strip leading and trailing whitespace,
> but any whitespace. Makes it way easier and faster. :)  Like
> 
>   tr/ //d;

Thanks!

>> I did try using $1 on the right side of the s///, but it didn't work.
> 
> The infamous "doesn't work" description... How so? What does it do?
> What about injecting some dbg() lines before and after the
> substitution?

Well, as the \1 variation worked apart from the warning, I didn't bother
with going any further.  I have a feeling the $1 might be getting
substituted first?  so maybe I should escape it?  Anyway, thanks for
your suggestion, it's much better. 


/Per Jessen, Zürich


Re: perl experts - \1 better written as $1 ?

Posted by Karsten Bräckelmann <gu...@rudersport.de>.
On Thu, 2010-06-10 at 12:08 +0200, Per Jessen wrote:
> > > I have a bit of SA code where I strip leading and trailing whitespace
> > > 
> > >   foreach (@addrs) { s/^\s*([^\s]+)\s*$/\1/; }
> > > 
> > > Whenever I run this I get the warning "\1 better written as $1" which
> > > I understand to be perl telling me that the right side of s/// should
> > > use $digit, not \digit.  I tried changing it to $1, but that didn't
> > > produce the expected result.  What would be the correct way to write
> > > this? 

> There are no spaces in the string, it's an email address.

So you don't actually want to strip leading and trailing whitespace, but
any whitespace. Makes it way easier and faster. :)  Like

  tr/ //d;


> I did try using $1 on the right side of the s///, but it didn't work.

The infamous "doesn't work" description... How so? What does it do? What
about injecting some dbg() lines before and after the substitution?


-- 
char *t="\10pse\0r\0dtu\0.@ghno\x4e\xc8\x79\xf4\xab\x51\x8a\x10\xf4\xf4\xc4";
main(){ char h,m=h=*t++,*x=t+2*h,c,i,l=*x,s=0; for (i=0;i<l;i++){ i%8? c<<=1:
(c=*++x); c&128 && (s+=h); if (!(h>>=1)||!t[s+h]){ putchar(t[s]);h=m;s=0; }}}


Re: perl experts - \1 better written as $1 ?

Posted by Per Jessen <pe...@computer.org>.
Mark Martinec wrote:

> Per,
> 
>> I have a bit of SA code where I strip leading and trailing whitespace
>> -
>>   foreach (@addrs) { s/^\s*([^\s]+)\s*$/\1/; }
>> Whenever I run this I get the warning "\1 better written as $1" which
>> I understand to be perl telling me that the right side of s/// should
>> use $digit, not \digit.  I tried changing it to $1, but that didn't
>> produce the expected result.  What would be the correct way to write
>> this? 
> 
> The above assumes there are no spaces withing the string - and
> does nothing if there are. 

There are no spaces in the string, it's an email address.  I did try
using $1 on the right side of the s///, but it didn't work. 


/Per Jessen, Zürich


Re: perl experts - \1 better written as $1 ?

Posted by Mark Martinec <Ma...@ijs.si>.
Per,

> I have a bit of SA code where I strip leading and trailing whitespace - 
>   foreach (@addrs) { s/^\s*([^\s]+)\s*$/\1/; }
> Whenever I run this I get the warning "\1 better written as $1" which I
> understand to be perl telling me that the right side of s/// should use
> $digit, not \digit.  I tried changing it to $1, but that didn't produce
> the expected result.  What would be the correct way to write this?

The above assumes there are no spaces withing the string - and
does nothing if there are. Try the:

  s/^\s*(.*?)\s*$/$1/


Mark

Re: perl experts - \1 better written as $1 ?

Posted by "Stefan Hornburg (Racke)" <ra...@linuxia.de>.
On 06/10/2010 08:36 AM, Per Jessen wrote:
> I have a bit of SA code where I strip leading and trailing whitespace -
>
> foreach (@addrs) { s/^\s*([^\s]+)\s*$/\1/; }
>
> Whenever I run this I get the warning "\1 better written as $1" which I
> understand to be perl telling me that the right side of s/// should use
> $digit, not \digit.  I tried changing it to $1, but that didn't produce
> the expected result.  What would be the correct way to write this?
>

s/^\s+//; s/\s+$//;

Your regex doesn't match "  foo bar  " at all.

Regards
          Racke



-- 
LinuXia Systems => http://www.linuxia.de/
Expert Interchange Consulting and System Administration
ICDEVGROUP => http://www.icdevgroup.org/
Interchange Development Team