artist has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
How do I declare code reference to a subroutine which belongs to a package and hence has properties and methods in that package.
I can do following without any problem.
$codeRef = \&Package::SubRoutine1 $codeRef = \&Package::SubRoutine2


Is there some way, I can 'bless' all my subroutines for a package so that I don't have to put 'Package' in above statements. I can definitely say 'use Package;' in the beginning.
Subroutines will be 'blessed', so that they will know which package they belongs to.
Thanks,
Artist

Replies are listed 'Best First'.
Re: package sub and code reference
by merlyn (Sage) on Sep 28, 2001 at 03:38 UTC
    I probably shouldn't do this, but here goes the deep magic:
    my %codeRefs = map { "Package"->can($_) || sub { die "can't find $_" } } qw(SubRoutine1 SubRoutine2 SubRoutine3);
    and now $codeRefs{SubRoutine1}->(@args) will invoke Package::SubRoutine1(@args), or an inherited version if needed, or die if nothing else.

    Yeah, scary deep magic. And it even works under "use strict". Augh!

    -- Randal L. Schwartz, Perl hacker

      If you are trying to scare people, you should mention that by throwing things like OtherPackage'FunctionName in the list you can have it search for functions starting from an arbitrary package.
Re: package sub and code reference
by runrig (Abbot) on Sep 28, 2001 at 01:32 UTC
    Give'em enough rope, I say...(no blessing involved)
    package MyPackage; sub hello { print "hello\n"; } package AnotherPackage; *hello = \&MyPackage::hello; package main; AnotherPackage::hello();
      Wow that is just so deeply wrong.

      If heresy were truly possible at the Monestary, this would certainly qualify.

      Look away, lest yer eyes be burned from their sockets!

      (grin)

      Gary Blackburn
      Trained Killer

Re: package sub and code reference
by dragonchild (Archbishop) on Sep 28, 2001 at 01:19 UTC
    First off, I'm going to ask why you want to do this. The reason I'm asking is that taking coderefs within other packages falls under the "I need to justify this before I do it" category. Maybe there's another, easier way that'll accomplish what you want to do...

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      Hi,
      Thanks for nice enquiry.
      I have to work the code, where already plent of coderefs are defined for different subroutine.
      Now since, it's 'all in one' file I like to seperate it out and making more modular.
      Another reason is to get rid of Global variables that each these subs are using.

      Now if subs happens to belong a particular package and know that, my problem is solved.
      I may require also a method to initialize the package and pass these 'currently globaled variables'.

      Thanks,
      Artist

        I'm going to assume that this is using some sort of dispatch-table mechanism. If it's not, rip it apart and start over. Seriously.

        If it's using a dispatch, those globals may be appropriate. Globals aren't necessarily bad. In fact, in a dispatch-table setting, they can actually be very useful.

        If you're gung-ho about migrating these functions into a module, I'd look at rebuilding the dispatch with an OO context. How that would happen is very dependent on what exactly you're doing.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.