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

Wise monks,

I'm confused because I cannot figure out myself something as (supposedly:) simple as exporting names. I want to declare a method write as such:

use subs qw(write); sub write(&) { ... } write { 42 }

This works until I want to define write(&) in another package, at which stage I get problems. Basically, I tried combinations along this code:

package A; sub write(&) {} package B; use subs qw(write); *write = \&A::write; write { 42 }
with and without Exporter (I know it does the same thing, but just in case), with and without use subs, but the net result is that I cannot find what is the proper syntax. Anyone knows the magic spell?

Replies are listed 'Best First'.
Re: redefining CORE:: names
by almut (Canon) on Dec 10, 2007 at 13:37 UTC

    Not sure if I'm understanding correctly what you're intending to do, but if the idea is to force the argument to write (i.e. { 42 }) to be handled as a coderef (without having to say sub {...}), using Exporter works for me:

    ( A.pm )

    package A; use subs qw(write); # only needed if you want to call write {...} wit +hin package A itself use Exporter 'import'; @EXPORT_OK = qw(write); sub write (&) { printf "write(): $_[0] -> %s\n", $_[0]->() }

    used as

    package B; use A 'write'; write { 42 };

    would treat { 42 } as a sub, and print out

    write(): CODE(0x65eb80) -> 42
      Indeed use Exporter 'import' works, -- as expected. Now I'm confused how could I make that simple code *not* working. Apparently what has happened is that I assumed that direct glob import without Exporter would be exactly the same what Exporter does, and I was wrong then.

      Duh. Sorry for the confusion.

Re: redefining CORE:: names
by lodin (Hermit) on Dec 10, 2007 at 15:05 UTC

    You get all sorts of warnings when you compile the A and B packages. Mismatching prototypes, ambiguous calls, etc.

    subs.pm doesn't do anything but put something in the code slot in the glob. The prototype mismatch is because subs assigns a subroutine with no prototype to &write and then you redefine that. If you do the glob assignment first (in a BEGIN block) and have the subs statement after the glob assignment, then everything works. I was intrigued by why subs does the trick with its glob assignment, but simplly having

    BEGIN { *write = \&A::write } # Doesn't work!
    didn't. I've boiled it down to that the assignment has to be done by some other package (not necessarily subs, e.g. Exporter). So this works:
    # Perl 5.8.8 { package X; # I change to X and Y because B is taken. sub write (&) { print $_[0]->() } } { package Y; BEGIN { package Something::Else; *Y::write = \&X::write; } write { 'OK' }; } __END__ OK
    I have no idea why you have to switch package. I'd appreciate if someone could point me to any perldoc that explains this. I haven't found anything about it.

    lodin

      the prototype mismatch is because subs assigns a subroutine with no prototype

      Aha! that was it. Thank you!