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

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

Hello friends, is there any way to abbreviate code like this:
$obj = Very::Long::Class::Name->new;
$result = Very::Long::Class::Name->other_class_method;

In Python you can do

import Very.Long.Class.Name as Name
obj = Name()
Name.other_class_method()

Does Perl have an analogous shortcut?
Thanks for any suggestions.

Replies are listed 'Best First'.
Re: The Very::Long::Class::Name problem
by Aristotle (Chancellor) on Oct 13, 2003 at 20:32 UTC
    use constant VLCN => 'Very::Long::Class::Name'; $obj = VLCN->new(); $result = VLCN->other_class_method();

    Makeshifts last the longest.

Re: The Very::Long::Class::Name problem
by dragonchild (Archbishop) on Oct 13, 2003 at 20:29 UTC
    use Very::Long::Class::Name; my $Name = "Very::Long::Class::Name"; my $obj = $Name->new; my $result = $Name->other_class_method.

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

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    ... strings and arrays will suffice. As they are easily available as native data types in any sane language, ... - blokhead, speaking on evolutionary algorithms

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: The Very::Long::Class::Name problem
by broquaint (Abbot) on Oct 14, 2003 at 00:37 UTC
    For something a little closer to python's import Class as Alias set up the import() routine to create an alias e.g
    sub import { my($pkg, %args) = @_; return unless exists $args{as} and length $args{as}; croak "Glob '$args{as}' already exists" if defined *{$args{as}}; *{"$args{as}\::"} = *{"$pkg\::"}; } ## usage use Very::Long::Class::Name ( as => 'Name' ); my $obj = Name->new;
    Or, you could take the modular approach and use Package::Alias e.g
    use Very::Long::Class::Name; use Package::Alias ( Name => 'Very::Long::Class::Name' );
    HTH

    _________
    broquaint

Re: The Very::Long::Class::Name problem
by bart (Canon) on Oct 14, 2003 at 12:15 UTC
    As I can't imagine this to be useful for anything but for OO, you might reconsider making a dummy subclass of that module.
    use Very::Long::Class::Name; @Name::ISA = 'Very::Long::Class::Name'; $obj = Name->new; $result = $obj->some_object_method;