in reply to Weird Regex in CGI::Cookie

I just verified that CGI::Cookie v1.24 still contains that ineffectual regexp. Per the CGI::Cookie POD, "Address bug reports and comments to: lstein@cshl.org". Go for it. You might want to submit your report along with a recommended patch, if you want to get it implemented quicker.

I know that Lincoln is pretty aggressive about stamping bugs out of his modules.

The problem you're seeing is that the regexp engine is acting lazy. The following regexp seems to work for your test strings:

s/\s*\b(.+)\b\s*/$1/

But it's probably better to anchor the regexp to the start and end of string, to draw it out to match the entire string. Even then it still just is a slightly ambiguous way to do things; to rely on greed outweighing non-greed. Especially when there are better ways to do it.

It is faster and more reliable to do it this way:

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

Alternation could be used along with the /g modifier, but then you slow things down just a little.


Dave


"If I had my life to do over again, I'd be a plumber." -- Albert Einstein

Replies are listed 'Best First'.
Re: Re: Weird Regex in CGI::Cookie
by graff (Chancellor) on Oct 16, 2003 at 02:30 UTC
    You might want to submit your report along with a recommended patch, if you want to get it implemented quicker.

    I second that, whole-heartedly. In fact, here's my suggestion for a better version (hoping I understand the intent correctly):

    sub raw_fetch { my $class = shift; my $raw_cookie = get_raw_cookie(@_) or return; my %results; $raw_cookie =~ s/^\s+//; $raw_cookie =~ s/\s+$//; my(@pairs) = split(/\s*;\s*/,$raw_cookie); foreach (@pairs) { my ($key,$val) = (/^([^=]+)=?(.*)/); $results{$key} = $val; } return \%results unless wantarray; return %results; }
    I've only tested it to the extent that the assignments to %results match the original code (except for the trailing whitespace glitch) -- that is, a "raw_cookie" string like this:
    one=1 ;two=2; three=the third ; four=  ; five  ; six = the sixth ;
    
    produces these key/value pairs:
    'one'=>'1',
    'two'=>'2',
    'three'=>'the third',
    'four'=>'',
    'five'=>'',
    'six '=>' the sixth'
    
    (note that spaces next to "=" are not deleted)