Get cookie function in Perl???

Posted on 2001-07-13
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;


Question by:mercia

Accepted Solution

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;    

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

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

Expert Comment

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.


EE Cleanup Volunteer

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Which version of C should I use when Perl is too slow? 13 172
Perl 101 11 72
Perl string replace for refred url 9 59
Perl Frameworks 1 77
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

932 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now