CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Override certain package subs, but let other resolve normally

 



raxip
Novice

Jun 10, 2009, 7:42 PM

Post #1 of 3 (947 views)
Override certain package subs, but let other resolve normally Can't Post

I've read a variety of man pages and perl books, but for some reason, I'm still having a difficult time understanding how to accomplish something -like- the following:

package Foo;
use Exporter qw( import );
our @EXPORT = qw( foo bar );
sub foo { print "Foo::foo()\n" }
sub bar { print "Foo::bar()\n" }
1;
---
package Foo::Bar;
no warnings 'redefine';
use Exporter qw( import );
our @EXPORT_OK = qw( bar );
sub bar { print "Foo::Bar::bar()\n" }
1;
---
#!/usr/bin/perl -w
use strict;
use Foo::Bar qw( bar );
bar();
foo();
---
The output:
Foo::Bar::bar()
Foo::foo()

Essentially, I'm trying to override only a couple of the routines in someone else's module.. but leave the rest intact without having to necessarily create wrapper functions around every single sub that's inside the base module.

Instead, let Perl's subroutine discovery mechanism find the one that has _not_ been overridden in the base module. What man pages should I consult to help understand more about that discovery process, and could someone lend a helpful hand?

Thank you


raxip
Novice

Jun 10, 2009, 9:28 PM

Post #2 of 3 (943 views)
Re: [raxip] Override certain package subs, but let other resolve normally [In reply to] Can't Post

After a bit of playing around.. I came up with a method of accomplishing this. It probably isn't the best way.. but it is -a- way. Your comments for accomplishing this in a more clean manner is appreciated.

P.S. I'm simply using @EXPORT (instead of 'OK' or 'TAGS' for simplicity of the example).

package Foo;
use Exporter qw( import );
our @EXPORT = qw( foo bar );
sub foo { print "Foo::foo()\n" }
sub bar { print "Foo::bar()\n" }
1;
---
package Foo::Bar;
no warnings 'redefine';
use Foo;
use Exporter qw( import );
our @EXPORT = @Foo::EXPORT;
sub bar { print "Foo::Bar::bar()\n" }
1;
---
#!/usr/bin/perl -w
use strict;
use Foo::Bar;
bar();
foo();
---
The output:
Foo::Bar::bar()
Foo::foo()


1arryb
User

Jun 11, 2009, 7:56 AM

Post #3 of 3 (937 views)
Re: [raxip] Override certain package subs, but let other resolve normally [In reply to] Can't Post

Hi raxip,

You need to tell the Foo::Bar package that it's a sub-package of Foo. This is done with the "use base" pragma or with the @ISA array. You can read all about this in the perlobj perldoc.

UPDATE: I was guilty of a little disinformation in this post. There is no requirement that a sub-module needs to appear in a directory under it's supermodule; that's purely a function of the name of the package and how Perl finds package files in the library path. Foo::Bar could have been called "Donald::Duck" and still could be a sub-module of Foo as long as it includes Foo in its @ISA array.

Foo.pm:

Code
#!/usr/bin/perl 

package Foo;

use strict;
use warnings;
use Exporter qw( import );
our @EXPORT = qw( foo bar );
sub foo { print "Foo::foo()\n" }
sub bar { print "Foo::bar()\n" }

1;


Foo/Bar.pm # NOTE that Perl finds sub-modules in a filesystem subdirectory named for the super-module.

Code
#!/usr/bin/perl 

package Foo::Bar;

use strict;
use warnings;
no warnings "redefine";

use Foo;
use base qw(Foo);
use Exporter qw( import );
our @EXPORT = @Foo::EXPORT;
sub bar { print "Foo::Bar::bar()\n" }
1;


fubar.pl

Code
#!/usr/bin/perl 

use strict;
use warnings;

use Foo::Bar;

bar();
foo();


Output of 'perl fubar.pl':

Code
Foo::Bar::bar() 
Foo::foo()


Cheers,

Larry


(This post was edited by 1arryb on Jun 11, 2009, 8:33 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives