Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: hijacking a module constructor

by broquaint (Abbot)
on Nov 03, 2003 at 16:43 UTC ( [id://304146]=note: print w/replies, xml ) Need Help??


in reply to hijacking a module constructor

Assuming nothing too sneaky is going on under the hood of GD::Graph::Bar you should simply be able to alias GD's symbol table to SVG::GD e.g
*GD:: = *SVG::GD:: if $opts->{SVG};
So any use of the GD namespace will automagically be using the SVG::GD namespace due to the glob alias.
HTH

_________
broquaint

Replies are listed 'Best First'.
Re: Re: hijacking a module constructor
by hackmare (Pilgrim) on Nov 03, 2003 at 16:54 UTC
    Ah. Right. Most useful. Thank you.

    Except now I'm in a quandry.

    My SVG::GD::Image code also imvokes GD::Image in order to continue to support existing GD functionality (all calls to the SVG::GD module are echoed to the GD module so that in the end the user can get an SVG output or a bitmap.

    Is there any way to affect the symbol table everywhere except when GD::Image is called from within the SVG::GD::Image namespace?

    Further over my head

      Simply save yourself a copy of the GD:: namespace:

      *hackmare::GD:: = *GD::; # now use hackmare::GD instead of GD in your own library
      perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
        This does not seem to work.

        The GD:: namespace is not copying over... The Hackmare::GD namespace is getting pointed to the GD::Image namespace when the GD::Image copy takes place. I tried both:

        *Hackmare::GD::Image::new = \&GD::Image::new; and *Hackmare::GD::Image:: = GD::Image::;
        . Here is the debug output if it helps at all...
        *Hackmare::GD::Image::new = \&GD::Image::new; *GD::Image:: = *SVG::GD::Image::; package SVG::GD::Image; #constructor sub SVG::GD::Image::new { my $class = shift; my $self = {}; bless $self, $class; $self->{_GD_} = new Hackmare::GD::Image(@_) || print STDERR "Quitting. Unable to construct new GD::Image object using GD: $!\n"; ....stuff.... return $self }
        Here is the debugging info: Notice the infinite loop for the constructor:
        main::(test/18-loadsvggd.pl:6): print "Creating an SVG::GD Image\n"; DB<0> Creating an SVG::GD Image main::(test/18-loadsvggd.pl:7): my ($width,$height) = (200,200); DB<0> main::(test/18-loadsvggd.pl:8): my $dwg = new GD::Image($width,$height +); DB<0> s SVG::GD::Image::new(SVG/GD.pm:79): my $class = shift; DB<0> s SVG::GD::Image::new(SVG/GD.pm:80): my $self = {}; DB<0> SVG::GD::Image::new(SVG/GD.pm:81): bless $self, $class; DB<0> SVG::GD::Image::new(SVG/GD.pm:82): $self->{_GD_} = new Hackma +re::GD::Image(@_) SVG::GD::Image::new(SVG/GD.pm:83): || print STDERR " +Quitting. Unable to construct new GD::Image SVG::GD::Image::new(SVG/GD.pm:84): object using GD: $!\n" +; DB<0> s GD::Image::new(SVG/GD.pm:79): my $class = shift; DB<0> GD::Image::new(SVG/GD.pm:80): my $self = {}; DB<0> GD::Image::new(SVG/GD.pm:81): bless $self, $class; DB<0> GD::Image::new(SVG/GD.pm:82): $self->{_GD_} = new Hackmare:: +GD::Image(@_) GD::Image::new(SVG/GD.pm:83): || print STDERR "Quitt +ing. Unable to construct new GD::Image GD::Image::new(SVG/GD.pm:84): object using GD: $!\n"; DB<0> GD::Image::new(SVG/GD.pm:79): my $class = shift; DB<0> GD::Image::new(SVG/GD.pm:80): my $self = {}; DB<0> GD::Image::new(SVG/GD.pm:81): bless $self, $class;
        Is anything glaringly obvious here?

        hackmare.
      This can be done, but I think it may be saner to go down another route as suggested by Corion and perrin ...
      $GD::{$_} = $SVG::GD::{$_} for grep !/::$/, keys %SVG::GD::;
      This has a similar effect to the symbol table glob aliasing, but only affects the GD:: symbol table. Again, use with caution as this is even more hackish than the glob alias.
      HTH

      _________
      broquaint

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-03-28 17:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found