Skip Menu |

This queue is for tickets about the aliased CPAN distribution.

Report information
The Basics
Id: 48289
Status: resolved
Priority: 0/
Queue: aliased

People
Owner: Nobody in particular
Requestors: perl [...] david-raab.de
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



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";
Hi Sid, I can see your point and it's an interesting idea. Since you already know, per class, what classes you want shortened, why not: my %exception_for; foreach my $type (@types) { $exception_for{$type} = alias("MailVerwaltung::Client::Exception::$type"); } And later: $exception_for{'Rest::Response'}->throw; I think this could solve your immediate need (though it sounds like you've solved it a different way), but it would be annoying to have to deal with the hashref. If I include your solution, I think I'd rename 'shorter' to 'prefix'; my $exception = prefix("MailVerwaltung::Client::Exception"); $exception->('Rest::Response')->throw; In any event, I'll give this some thought. Cheers, Ovid
On Wed Jul 29 04:58:24 2009, SidBurn wrote: Show quoted text
> Your module "aliased" is really good. But if you have a lot of Classes > it is really annyoing to always setup aliased.
I've fixed this in 0.30_01. Let me know if it works for you. The new function is called prefix() and it behaves just a tad differently from your sample code (it attempts to load the packages). Cheers, Ovid
I think three years was long enough to wait for feedback :) This is now released as version 0.31. Cheers, Ovid