enoch has asked for the wisdom of the Perl Monks concerning the following question:
That regex sent up a warning signal in my brain.foreach (@pairs) { s/\s*(.*?)\s*/$1/; . . . }
Which, output:push @strings, 'nospace'; push @strings, 'trailingspace '; push @strings, ' leadingspace'; push @strings, 'internal space'; push @strings, ' surroundedbyspace '; push @strings, ' spaces every where '; for my $wtf (@strings) { print "Before: '$wtf'\n"; $wtf =~ s/\s*(.*?)\s*/$1/; print "After: '$wtf'\n\n"; }
Which is, basically, what I expected. The regex is only replacing leading spaces... The things is, this code is located in CGI::Cookie in the raw_fetch subroutine (view the source here). Here is the code in the subroutine:Before: 'nospace' After: 'nospace' Before: 'trailingspace ' After: 'trailingspace ' Before: ' leadingspace' After: 'leadingspace' Before: 'internal space' After: 'internal space' Before: ' surroundedbyspace ' After: 'surroundedbyspace ' Before: ' spaces every where ' After: 'spaces every where '
So, is this regex doing something that I am missing, or is it a broken regex that was placed in a seldom-used subroutine that no one has bother correcting?# Fetch a list of cookies from the environment or the incoming headers + and # return as a hash. The cookie values are not unescaped or altered in +any way. sub raw_fetch { my $class = shift; my $raw_cookie = get_raw_cookie(@_) or return; my %results; my($key,$value); my(@pairs) = split("; ?",$raw_cookie); foreach (@pairs) { s/\s*(.*?)\s*/$1/; if (/^([^=]+)=(.*)/) { $key = $1; $value = $2; } else { $key = $_; $value = ''; } $results{$key} = $value; } return \%results unless wantarray; return %results; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Weird Regex in CGI::Cookie
by davido (Cardinal) on Oct 16, 2003 at 00:33 UTC | |
by graff (Chancellor) on Oct 16, 2003 at 02:30 UTC | |
|
Re: Weird Regex in CGI::Cookie
by enoch (Chaplain) on Oct 16, 2003 at 04:39 UTC | |
|
Re: Weird Regex in CGI::Cookie
by d_i_r_t_y (Monk) on Oct 16, 2003 at 00:59 UTC | |
|
Re: Weird Regex in CGI::Cookie
by Abigail-II (Bishop) on Oct 15, 2003 at 23:40 UTC | |
|
Re: Weird Regex in CGI::Cookie
by hossman (Prior) on Oct 20, 2003 at 05:06 UTC |