Solved

Get cookie function in Perl???

Posted on 2001-07-13
2
201 Views
Last Modified: 2008-03-04
I have been look enough to find a get cookie function that will allow me to retrieve a specific session cookie based on its name.  It has been created in Java script which I believe should be able to be run in Perl. However I do not seem to be able to place it within Perl can someone show me how to change these few lines in to something Perl will accept????  

 
function get_cookie(Name) {
  var search = Name + "="
  var returnvalue = "";
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
   
    if (offset != -1) {
      offset += search.length
     
      end = document.cookie.indexOf(";", offset);
     
      if (end == -1) end = document.cookie.length;
      returnvalue=unescape(document.cookie.substring(offset, end))
      }
   }
  return returnvalue;

}

Thanks
0
Comment
Question by:mercia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Accepted Solution

by:
mh2 earned 50 total points
ID: 6280439
I do it using the CGI module. Try... ( just typed out my head tho! )

use CGI qw/:standard/;
use CGI::Cookie;

sub get_cookie()
{
  my($name) = @_;
  my %cookies = fetch CGI::Cookie;    
  return $cookie{$name};
}

Becareful, the value $cookie{'name'} may not be simply the cookies value, it also contains expiry info etc, in a semi-colon delimited list of assignments.

I use the funtion below to take a hash ref and convert the values into pure values.


my %cookies = fetch CGI::Cookie;    
MakeCookiesUseful(\%cookies);

sub MakeCookiesUseful()
{
     my($cookies) = @_;
     my $n;

     while (($name,$value) = each(%$cookies))
     {
          my @fields = split(/;/,$value);
          ($n,$$cookies{$name}) = split(/=/,$fields[0]);
     }
}
0
 
LVL 20

Expert Comment

by:jmcg
ID: 9482445
Nothing has happened on this question in over 12 months.
I will leave a recommendation in the Cleanup topic area that
the answer by mh2 be accepted.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question