General discussion

Locked

Perl Reg Exp "=~ s/\A" Explain

By Craig_Carman ·
These lines are used to begin a sub for parsing form data by prepping this env var for confirmation. I understan line one.
$S = lc $ENV{HTTP_REFERER};
$S =~ s/\A.*\/\///;
$S =~ s/\Awww\.//;
$S =~ s/\A(.*?)(\/.*)/$1/;
Will someone explain to me or even comment the lines to help me understand the significance of these substitutions in line 2 thru 4?

This conversation is currently closed to new comments.

2 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Perl Reg Exp "=~ s/\A" Explain

by Shanghai Sam In reply to Perl Reg Exp "=~ s/\A" Ex ...

Hi,

For starters "=~" means do the substitution on and assign back to.

\A means Matches at the beginning of the string.

Taken literally, the whole thing is...

Line 1 : Sets up a lowercase version of the variable to work on from a hash -that seems to be some sort of URL.

Line 2 : Substitute out (ie. replace with with nothing) everything up to and including the last // - its a greedy match.

Line 3 : Substitute out starting with www., the www.

Line 4 : This is a bit trickier. The Substitution goes... Starting with (ie. the \A) zero or one anythings non-greedily up to but not including the /rest-of-the-URL with the first bit.

Its a URL being worked on, so essentially its taking off the http:// then the www. and then dropping everything off after next /

So... it produces the the domain name.

eg. http://www.microsoft.com
would yield microsoft.com

eg www.microsoft.com/products/default.html
would still yield microsoft.com

If you're having problems with it, it could be that the following substition would occur - due to the first greedy match...

www.microsoft.com/question.jhtml?product=slashmaker//
Would yield a blank string (I think - without having actually tested it...)

...but I dont know if that syntax would be a possible "HTTP_REFERER"?

Hope this helps,
Cheers,
Tim.

Collapse -

by Craig_Carman In reply to Perl Reg Exp "=~ s/\A" Ex ...

Poster rated this answer.

Back to Web Development Forum
2 total posts (Page 1 of 1)  

Related Discussions

Related Forums