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

I have several hundred different misc cgis that I want to override CGI->new in. Basically I want to stash a ref to the original CGI::new coderef, call it, then run some routines after it finishes.

These scripts are all very different, but do use some custom libraries (e.g. use Company::CustomLib;). So I want to put the override code in CustomLib.pm.

I got my code working in the body of a cgi using the Sub::Override module. However when I move that to CustomLib.pm it doesn't override, I assume some namespace issue I don't understand.

I've been experimenting a lot with symbol tables and what not, as well as googling around still haven't stumbled arcross how to make it be overridden if it's in CustomLib.pm. I'm about ready to just hack CGI.pm but really dont' want to have to do that when I know there's some better way.

thanks for help

cheers to the all!
  • Comment on over ride CGI::new from existing package

Replies are listed 'Best First'.
Re: over ride CGI::new from existing package
by Your Mother (Archbishop) on Jun 25, 2010 at 07:12 UTC

    And one for the road-

    BEGIN { # External package in real practice. package MyCGI; use parent "CGI"; sub new { my $self = +shift->SUPER::new(@_); $self->param(OH => "HAI"); $self; } sub DESTROY { my $self = shift; warn "doing my own thing now..."; warn "parental destruction next..."; $self->SUPER::DESTROY; } } # use MyCGI; <-- If in another/real package file. use strict; use warnings; use Data::Dumper; print Dumper( MyCGI->new ); __END__ $VAR1 = bless( { '.parameters' => [ 'OH' ], 'use_tempfile' => 1, '.charset' => 'ISO-8859-1', '.fieldnames' => {}, 'param' => { 'OH' => [ 'HAI' ] }, 'escape' => 1 }, 'MyCGI' );

    (Update: added DESTROY. Not quite sure it's the way to go but I am sure someone will chime in if it's not a good idea or quite right.

Re: over ride CGI::new from existing package
by Anonymous Monk on Jun 25, 2010 at 03:58 UTC
    #!/usr/bin/perl -- use CGI; BEGIN { my $orig = \&CGI::new; *CGI::new = sub { my $bla = $orig->(@_); $bla->param( blah => 1 ); return $bla; } } use strict; use warnings; use Data::Dumper; print Dumper( CGI->new ); __END__ $VAR1 = bless( { '.parameters' => [ 'blah' ], 'use_tempfile' => 1, '.charset' => 'ISO-8859-1', '.fieldnames' => {}, 'param' => { 'blah' => [ 1 ] }, 'escape' => 1 }, 'CGI' );
A reply falls below the community's threshold of quality. You may see it by logging in.