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

Mmmkay...

import $my_package qw/some_subroutine/;
fails with

Can't call method "import" on unblessed reference at sources/ZDisplay. +pm line 436.

How do I bless $my_package to refer to the package I want, which has the same name as the string contained in $my_package?

Much thanks.

dave hj~

Replies are listed 'Best First'.
Re: importing from variable packages
by davorg (Chancellor) on Mar 14, 2001 at 20:30 UTC

    No need to bless $my_package, simply slightly reorder your code.

    $my_package->import(qw/some_routine/);

    Note that the first parameter that import gets will be the string that's in $my_package.

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      Excellent. Thanks. So how come that works, and the indirect object style doesn't? My tiny brain hurts.

      dave hj~

        From my quick testing, these two yield the same results:

        import $pack 'hi'; $pack->import( 'hi' );
        with each of them only producing the error you quoted when $pack is a reference and is not blessed. I don't have a ton of versions of Perl handy and these results were with Perl 5.6.0. Using Perl 5.004_01, I got results too weird to mention.

        So I suspect this is a Perl 5.005 bug you are experiencing.

                - tye (but my friends call me "Tye")