Subject: | A new function for shortening class names |
Your module "aliased" is really good. But if you have a lot of Classes
it is really annyoing to always setup aliased.
An example. I have written an application that uses Exception::Class for
defining Exceptions. The complete namespace for this is
"MailVerwaltung::Client::Exception" and now there are a lot of classes
for errors. Lets say "REST::Response" it enden up to call this exception
with.
MailVerwaltung::Client::Exception::REST::Response->throw()
But now there are many such classes in this Exception Class. And writing
30+ "my $thing = alias()" lines is also not very handy. And not every
Exception will be used everywhere. Now what i think is, a new function
that shortened something, but with an argument you can change what you
get back. to describe it with code.
my $error = short('MailVerwaltung::Client::Exception');
$error->('REST::Response')->throw();
Now "$error" would be Returning the function, that returns
"MailVerwaltung::Client::Exception" but with an argument i can go even
deeper.
It would be even nice when this is possible in the "use" line. Lets say.
Show quoted text
> use aliased 'Some::Package::With::A::Long::Name', error => { class =>
'Some::Package::With::A::Long::Name';
and later
Show quoted text> error('This::That')->new()
Syntax is from "Sub::Exporter". Sure it would not be a problem to write
this function by myself (already did it), but it would be nice to have
it in this module, because i think it extends the functionality, and it
is even a clean way and just uses subroutines.
It would be even nice when only this sub added to create the anonymous
subroutine.
Subject: | alias.pl |
#!/usr/bin/env perl
# Core Modules
use strict;
use warnings;
use utf8;
use open ':encoding(UTF-8)';
use open ':std';
use Encode qw(encode decode is_utf8);
use Carp qw(carp croak);
sub alias {
my ( $class ) = @_;
return sub {
my ( $name ) = @_;
if ( @_ == 0 ) {
return $class;
}
if ( @_ == 1 && defined $name ) {
return $class . '::' . $name;
}
else {
die "Wrong arguments.";
}
}
}
my $error = alias('Program::With::A::Long::Package::Name');
print $error->(), "\n";
print $error->('Now::Longer'), "\n";