Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Overloading CGI::redirect

by esskar (Deacon)
on Apr 09, 2004 at 23:05 UTC ( [id://344054]=perlquestion: print w/replies, xml ) Need Help??

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

Hi friends,

i have something like this
package lib::CGI; use strict; use warnings; require CGI; use vars qw(@ISA); @ISA = qw( CGI ); sub redirect { my ($self, $uri) = @_; # print "Location: $uri\n\n"; # works but is nor nice # CGI::redirect($self, $uri); # does not work # $self->redirect($uri); # endless loop # i need help exit(0); }
How do i overload the redirect function so it is working?

Replies are listed 'Best First'.
Re: Overloading CGI::redirect
by nothingmuch (Priest) on Apr 10, 2004 at 01:04 UTC
    See perltoot and perlboot for tutorialish object oriented Perl guides, and perlobj for a reference, and lastly perlbot for more complex stuff, lots of which has to do with inheritence.

    The short answer is that $self->SUPER::method() solves your problem.

    Also, I noticed that you subclass in 3 lines. Why don't you

    use base 'CGI';

    instead, it's more fun ;-)

    -nuffin
    zz zZ Z Z #!perl
Re: Overloading CGI::redirect
by iburrell (Chaplain) on Apr 10, 2004 at 00:10 UTC
    The way to call the superclass method is like this:
    sub redirect { my ($self, $uri) = @_; $self::SUPER->redirect($uri); exit(0); }
      Where'd you learn $self::SUPER->method? I don't think that works.
        I got the syntax wrong. It should be $self->SUPER::method(),
Re: Overloading CGI::redirect
by perlmonkey (Hermit) on Apr 10, 2004 at 05:23 UTC
    As others have said, use SUPER. A more obscure option is to alias the CGI::redirect function, something like:
    use base 'CGI'; BEGIN { *cgi_redirect = \&CGI::redirect; } sub redirect { my($self,$uri) = @_; $self->cgi_redirect($uri); }
    Update:
    I forgot to mention that instead of SUPER, you can also always use the full class name. You were close, but instead of CGI::redirect($self, $uri) you should use $self->CGI::redirect($uri).

    This is useful (necessary) if you use multiple inheritance and both parent classes defined the same subroutine. So if you had:
    package BaseA; sub func {} package BaseB; sub func {}; package Derived; use base qw(BaseA BaseB); sub func { my $self = shift; $self->SUPER::func(); # calls BaseA::func $self->BaseA::func(); # same $self->BaseB::func(); # only way to access BaseB::func }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-26 01:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found