http://www.perlmonks.org?node_id=299293


in reply to use Very::Long::Module::Name as Foo;

Your approach isn't transparent: references will be blessed into the shortname package. I chose to use constants in the other thread for this reason.
$ perl -MO=Deparse -e'Foo::Bar->new'
'Foo::Bar'->new;
-e syntax OK
$ perl -MO=Deparse -e'use constant FB => "Foo::Bar"; FB->new'
use constant ('FB', 'Foo::Bar');
'Foo::Bar'->new;
-e syntax OK

Note how it compiles to exactly the same code. There is absolutely no chance that it will behave any differently than the original code in any case ever.

And while it may seem simple, your approach needs some very heavy weaponry to work. It is in gross violation of the "do the simplest thing that can possibly work" principle.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re: use Very::Long::Module::Name as Foo;
by liz (Monsignor) on Oct 15, 2003 at 08:58 UTC
    As I try not to use indirect object syntax (anymore), I think the approach to define a constant is the most portable way for now. So no argument from me there (anymore).

    It is in gross violation of the "do the simplest thing that can possibly work" principle.

    I don't agree there. I still think my approach is the cleanest from the user's point of view. It doesn't need the definition of any extra variables or constants, which I think are cruft from an API point of view. And therefore, it is the simplest thing that can possibly work in my eyes.

    Under the hood, Perl is already using rather heavy weaponry to DWIM. Constants are one (first create a subroutine, then inline the constant later). Tieing variables is another one. And what about $&? All can be done in other ways, yet everyone likes the fact that they're there if they want to take the performance penalty.

    Personally, I think it would not be a real big issue to implement the "as modulename" feature in the C-code that handles "use" in Perl (which would put the namespace aliasing stuff under the hood there). And I think the effect on compiling would be negligeble (only extra complexity while compiling a "use"). However, the reason I'm not going to pursue this, is the global namespace problem. I don't see a simple solution for making the short name local to the package in which it "defined".

    Liz

Re: Re: use Very::Long::Module::Name as Foo;
by broquaint (Abbot) on Oct 15, 2003 at 09:18 UTC
    Your approach isn't transparent: references will be blessed into the shortname package.
    shell> perl -le '*a::=*b::; print bless [], "a"' b=ARRAY(0x8107f70)
    Or not. As we can see, a is really just like any other glob alias and so is completely transparent.
    HTH

    _________
    broquaint