Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4071038
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:42:20+00:00 2026-05-20T16:42:20+00:00

My colleagues and I are maintaining and developing a Perl web-project that works via

  • 0

My colleagues and I are maintaining and developing a Perl web-project that works via mod_perl.

Now we are going through a major legacy code refactoring in which we have implemented some sort of an MVC pattern.

Among other things, my task is to make sure that all HTTP response headers are processed and sent back to the browser inside the main controller. For example, if a redirect is required, a page handler throws an exception, then the main controller catches it and generates the corresponding headers.

It all looked well until I started to implement cookie handling. Before that our code just printed cookie headers to output when it was required, like so:

# $response is an instance of the CGI class
print $response->redirect(
    -uri => "/some_uri/",
    -cookie => $response->cookie(
         -name => 'user_id',
         -value => $user->{'id'},
         -path => '/', -expires => '+1M'));

And now I want the $response object to store that information, so I can later send all headers together. I thought that it would go something like that:

sub page_handler {
    # ...
    $response->cookie(-name => 'user_id',
         -value => $user->{'id'},
         -path => '/', -expires => '+1M');
    return;
}

# And then, inside the controller

sub controller {
    # ...
    # the same $response instance 
    print $response->header();
    print $output;
    # ....
    exit();
}

But it seems that the CGI class object doesn’t store all headers that it creates with the header method. Some headers seem to persist, while others do not, here is what I get in re.pl:

$ use CGI;        
$ my $response = CGI->new();
$CGI1 = CGI=HASH(0xa6efba0);
$ $response->header();
Content-Type: text/html; charset=ISO-8859-1

$ $response->header(-type => 'text/plain', -charset => 'UTF-8', -status => '200 OK');
Status: 200 OK
Content-Type: text/plain; charset=UTF-8

$ $response->header();
Content-Type: text/html; charset=UTF-8 

I expected the last output to be either the same as the previous one, or the same as the first one, where I have not yet set any headers. I did not expect it to change partially.

That is why I ask my question: Why does some header information in a CGI.pm object persist while another does not?

Am I using the object incorrectly? Is there a way I could use it the way I intended to?

PS: Sorry for the long question, I wanted to make sure you understand what I want to do.

PPS: Also, I know that many people around here recommend going away from CGI and using Catalyst. This is, I am afraid, not an option right now, because we have too much legacy code, and we are hoping to get away from mod_perl altogether. This is required only for a certain feature.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T16:42:21+00:00Added an answer on May 20, 2026 at 4:42 pm

    To answer your question, the header method doesn’t store any information, nothing is persistent.

    With your example of the header ‘object’ persisting, reading TFM helps:

    The -charset parameter can be used to control the character set sent to the browser. If not provided, defaults to ISO-8859-1. As a side effect, this sets the charset() method as well. [emphasis mine]

    After you call header with some parameters, then call it as default, the only thing to ‘persist’ is the character set.

    For your cookie problem, i think you’d have to store $response->cookie(); somewhere. TFM doesn’t say that the cookie() sub stores the data anywhere, it just says that it creates a cookie.

    I agree with Sinan though – throwing exceptions is crazy talk, especially to cover CGI.pm’s redirect sub. I’d rethink that one. Or go completely the other way and write the whole webapp only using exception handling – there’s be some good laughs along the way :o)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some colleagues of mine have a large Java web app that uses a search
My colleagues are going crazy because I keep on wanting to rewrite code that
I'm going through my colleagues code, he has written several Page.IsValid checks but I
Hello I'm looking at a colleagues web application that is written in C# and
My colleagues are attempting to connect BizTalk 2006 R2 via DB2/MVS adapter to a
Several colleagues and I are faced with an architectural decision that has serious performance
My colleagues are seasoned C++ hackers switching to .Net. One of the mistakes that
Myself and some colleagues have just been told that we are all taking the
colleagues, how can i debug an activity that extends the service class? Its not
Now my colleagues work on logging subsystem and they want to bind separate operations,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.