aliased is now on the CPAN. The syntax is cleaned up from what I originally had.
# implicit alias: use aliased 'Some::Long::Module::Name'; # explicit alias: use aliased 'Some::Long::Module::Name' => 'Name'; # import lists require using explicit aliasing: use aliased 'Some::Long::Module::Name'=> 'Name, qw/foo bar baz/; # and all of the above allow you to call class methods # on the alias: my $new_name = Name->new; my $old_name = Name->search($id); # etc.
-sam
Re:Why not use namespace?
Ovid on 2005-01-07T18:31:02
First, if you just want the last part of the class name, aliased has the simpler syntax:
use aliased "Really::Long::Class::Name::For::Customer";
my $cust = Customer->new;
# versus
use namespace Customer => "Really::Long::Class::Name::For::Customer";
my $cust = Customer->new;Second, compare the implementations. I am just exporting a single subroutine. namespace jumps through a lot of weird hoops, diddles the aliased @ISA array, and has rather coplicated code. Further, it will die if the target namespace is already defined, but if that namespace gets defined later, your code is much more likely to break.
Third, I have tests
:) Maybe I should document this?
Re:Why not use namespace?
samtregar on 2005-01-07T18:40:36
Yes, I think you should document it, particularly since you mention a good reason that I would use namespace: it works for:: in addition to ->. I find the point about tests to be particularly compelling, personally. -sam
Re:Why not use namespace?
Ovid on 2005-01-07T18:53:39
I was mistaken. namespace also does not allow function calls. In trying this with my test suite, I have this:
#!/usr/local/bin/perl
use warnings;
use strict;
use lib 't/lib';
use namespace 'This::Name' => 'Really::Long::Module::Name';
use Data::Dumper;
print Dumper( This::Name::new('This::Name') );It says that This::Name::new is an undefined subroutine. But, it turns out the problem is worse than that!
#!/usr/local/bin/perl
use warnings;
use strict;
use lib 't/lib';
use namespace 'This::Name' => 'Really::Long::Module::Name';
use Data::Dumper;
print Dumper This::Name->new;This results in:
Can't locate object method "Dumper" via package "This::Name"My code, on the other hand, works as expected:
#!/usr/local/bin/perl
use warnings;
use strict;
use lib 't/lib';
use aliased 'Really::Long::Module::Name' => 'This::Name';
use Data::Dumper;
print Dumper This::Name->new;I don't mean this a slam against namespace. The author wrote some really nifty code. Unfortunately there are unwanted side-effects from making it more complicated than it needs to be.