I don't think the specific technique is discussed in perldoc (all of the
parts are there, you just have to put them together), but I do think it is
discussed in the Perl Cookbook and probably Programming Perl.

Stuff about the how Perl handles symbol tables is covered in

perldoc perlmod

http://perldoc.perl.org/perlmod.html#Symbol-Tables

You can see an example of mucking about with the current module's symbol
table in perlref:

perldoc perlref

http://perldoc.perl.org/perlref.html#Function-Templates

But it doesn't mention how to do it to other modules.

You might also search for information on typeglobs, as that is what we are
using to manipulate the symbol table. The basics are that
*Fully::Qualified::sub_name{CODE}
holds a code ref to the function sub_name in Fully::Qualified and assigning
a code ref to *Fully::Qualified::sub_name will replace that function with
the one pointed to by the code ref.

A common pattern is to save off the old code ref, assign an anonymous sub
to the typeglob, and put the code you want in that anonymous sub calling
the old code ref through a closure:

{

no warnings "redefine";

my $old = *Fully::Qualified::sub_name{CODE}

*Fully::Qualified::sub_name = sub {

    #do stuff before

    $old->(@_);

    #do stuff after

};

}

On Sat, Aug 20, 2016, 05:44 hw <h...@gc-24.de> wrote:

> Chas. Owens schrieb:
> > If you want to get rid of ALLO completely, it looks like you just need
> to monkeypatch Net::FTP::_ALLO to return 1:
> >
> > use Net::FTP;
> > BEGIN {
> >      no warnings "redefine";
> >      *Net::FTP::_ALLO = sub { 1 };
> > }
> >
> > This replaces the _ALLO method of Net::FTP with a new method that just
> returns 1.  I set it up like that because the actual code is:
> >
> > sub _ALLO { shift->command("ALLO", @_)->response() == CMD_OK }
> >
> > So it looks like it expects the return value to be either true or
> false.  None of the calls seemed to check the value, but better to be safe
> than sorry.
>
> Perfect, thank you very much!  That´s exactly what I´d have done if I knew
> how to.
>
> BTW, it turned out that the rmdir method goes crazy when you try to remove
> a
> directory recursively that doesn´t exist, so I added a check and only
> remove
> one when it does exist.
>
> Is there a good documentation about how to do this kind of
> advising/redefining?
>
>

Reply via email to