in reply to retrieving an URL

The CGI specification dictates several environmental variables to be set. The complete list of all CGI environmental variables is located here. The variable you are interested in is HTTP_REFERER. In perl it is accessable as.
$ENV{HTTP_REFERER}