[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [Fun With Perl] Package Retrofit



At 17.57 -0400 1999.06.10, John Kristian wrote:
>This is a good thing, I think:
>
>    if (not can Mozilla::LDAP::Entry 'getLDIFrecords') {
>        eval q{
>            package Mozilla::LDAP::Entry;
>            sub getLDIFrecords
>            {
>                [snip]
>            }
>        }; # eval
>    }
>
>In brief, "If that package doesn't have the method I need, add it."


I would do this instead, FWIW:

    if (not can Mozilla::LDAP::Entry 'getLDIFrecords') {
        local $SIG{__WARN__} = sub {
            warn @_ unless $_[0] =~ /Subroutine getLDIFrecords redefined/
        };

        *Mozilla::LDAP::Entry::getLDIFrecords = sub {
            [snip]
        };
    }

It does the same thing, but avoids the eval.  You don't need a separate
package declaration, you can just put it in the sub declaration.  Since you
are redefining a sub, you probably also want to catch the warning.  Or
instead of that whole thing with $SIG{__WARN__}, you could silence all
warnings with:

    if (not can Mozilla::LDAP::Entry 'getLDIFrecords') {
        local $^W;

--
Chris Nandor          mailto:pudge@pobox.com         http://pudge.net/
%PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10  1FF77F13 8180B6B6'])

==== Want to unsubscribe from this list? (Don't you love us anymore?)
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org