Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Problem with header() method when extending CGI.pm

by thinker (Parson)
on Jan 09, 2002 at 17:58 UTC ( [id://137444]=perlquestion: print w/replies, xml ) Need Help??

thinker has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I have been trying to 'extend' CGI.pm, to customise it to a site I am working on. (btw. Are there many other monks who approach this problem in this way?)

While most of it does what I want, when I call the header() method, my instance does not behave as I expect.

Here is a simple example of what is going wrong. My module looks like

#!/usr/bin/perl -w package IsaCGI; use strict; use CGI; use vars qw(@ISA); @ISA=qw(CGI); sub new{ my $this={}; bless $this,__PACKAGE__; $this->SUPER::new({}); return $this; } sub my_method{ return "hello from isa_cgi\n"; } 1;

I am using the following code to test the module

#!/usr/bin/perl -w use strict; use IsaCGI; use diagnostics; my $q=new IsaCGI; print $q->header; print $q->start_html; print $q->my_method;

when I call the header() method, it prints

Use of uninitialized value at (eval 4) line 28 (#1) (W) An undefined value was used as if it were already defined. It + was interpreted as a "" or a 0, but maybe it was a mistake. To suppre +ss this warning assign an initial value to your variables. Content-Type: text/html; charset=

whereas, if I change use IsaCGI and new IsaCGI to use CGIand new CGI I get

Content-Type: text/html; charset=ISO-8859-1

Can someone enlighten me as to why this is happening? Where has the charset info gone?

Thank you

thinker

Replies are listed 'Best First'.
Re: Problem with header() method when extending CGI.pm
by derby (Abbot) on Jan 09, 2002 at 18:42 UTC
    thinker,

    works fine for me (redhat 7.2, perl 5.6.1, cgi 2.752). Looking at CGI.pm's header method in 2.752, it appears to not append charset to the header if the type is blank, starts with "text" or all ready contains "charset". Double check your versions.

    On a side note, IsaCGI is not a very helpful name.

    -derby

Re: Problem with header() method when extending CGI.pm
by Hrunting (Pilgrim) on Jan 09, 2002 at 19:11 UTC
    Can I ask why you're doing all that work in the new() method? Why not just something like:
    package IsaCGI; use strict; use CGI; use vars qw(@ISA); @ISA=qw(CGI); sub my_method{ return "hello from isa_cgi\n"; }
    A CGI instance is blessed like:
    sub new { my($class,$initializer) = @_; my $self = {}; bless $self,ref $class || $class || $DefaultClass; ...
    which means that in your code, if you do:
    my $q = new IsaCGI;
    it will be blessed into the IsaCGI class. Is there a reason you're not doing it that way? Even in more complicated scenarios of inheritance, one gets the base object first, then tinkers with it. You seem to be doing it the other way around.
Re: Problem with header() method when extending CGI.pm
by kwoff (Friar) on Jan 09, 2002 at 23:57 UTC
    You put $this->SUPER::new() in void context (not assigned to anything).
      Hi kwoff

      You put $this->SUPER::new() in void context (not assigned to anything).

      Thank you kwoff, I changed the line to  $this=$this->SUPER::new({}); and it works fine. I guess you knew it would :-)

      Thanks

      thinker

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://137444]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-25 15:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found