New perl module: Nagios::Plugin::WWW::Mechanize

Max perldork at webwizarddesign.com
Thu Aug 20 07:00:17 CEST 2009


Hi Tom,

If you add this subroutine to the end of your module, any methods in
WWW::Mechanize that do not exist as wrapped methods will have method
proxies created for them in your wrapper module IF they exist in
WWW::Mechanize so that users can use the full range of methods
WWW::Mechanize offers in their plugins without you having to move to
multiple inheritence OR having to manually write wrapper calls for
every method.

Once the method proxy is created, AUTOLOAD will not be called again
for that method as the proxy will then exist :p.

If the method does not exist in WWW::Mechanize, normal perl behavior
for undefined methods will be used and the proxy code will be skipped.

- Max

sub AUTOLOAD {

    my @args = @_;

    # Subroutine the user requested from us that does not exist in our namespace
    my $wanted = $Nagios::Plugin::WWW::Mechanize::AUTOLOAD;
    $wanted =~ s/Nagios::Plugin::WWW::Mechanize:://ms;

    #  We were not passed a reference to ourselves, so exit.
    if (ref($args[0]) ne 'Nagios::Plugin::WWW::Mechanize') {
        return;
    }

    my $self = shift @args;
    my $mech = $self->mech();

    #  If WWW::Mechanize knows this method, we create a proxy for it.
    if ($mech->can($wanted)) {
        my $func = <<EOF;
*Nagios::Plugin::WWW::Mechanize::$wanted = sub {
    my (\$self, \@args) = \@_;
    return \$self->mech->$wanted(\@args);
};
EOF

        #  New scope to limit effect of 'no warnings'
        {
            no warnings 'redefine';
            eval $func;
            $self->die("Can't autocreate WWW::Mechanize::$wanted wrapper: $@")
                if $@;
            #  Call directly via mech this time; future calls will invoke
            #  the proxy method.
            $mech->$wanted(@args);
        }
    }

}
-------------- next part --------------
sub AUTOLOAD {
    #  Subs we proxy to WWW::Mechanize
    my @args = @_;

    # Subroutine the user requested from us that does not exist in our namespace
    my $wanted = $Nagios::Plugin::WWW::Mechanize::AUTOLOAD;
    my $wanted = $Nagios::Plugin::WWW::Mechanize::AUTOLOAD;
    $wanted =~ s/Nagios::Plugin::WWW::Mechanize:://ms;

    #  We were not passed a reference to ourselves, so exit.
    if (ref($args[0]) ne 'Nagios::Plugin::WWW::Mechanize') {
        return;
    }

    my $self = shift @args;
    my $mech = $self->mech();

    #  If WWW::Mechanize knows this method, we create a proxy for it.
    if ($mech->can($wanted)) {
        my $func = <<EOF;
*Nagios::Plugin::WWW::Mechanize::$wanted = sub {
    my (\$self, \@args) = \@_;
    return \$self->mech->$wanted(\@args);
};
EOF

        #  New scope to limit effect of 'no warnings'
        {
            no warnings 'redefine';
            eval $func;
            $self->die("Can't autocreate WWW::Mechanize::$wanted wrapper: $@") 
                if $@;
            #  Call directly via mech this time; future calls will invoke
            #  the proxy method.
            $mech->$wanted(@args);
        }
    }

}
-------------- next part --------------
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
-------------- next part --------------
_______________________________________________
Nagios-users mailing list
Nagios-users at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-users
::: Please include Nagios version, plugin version (-v) and OS when reporting any issue. 
::: Messages without supporting info will risk being sent to /dev/null


More information about the Users mailing list