Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Alias for object namespace

by eXile (Priest)
on Jan 28, 2005 at 17:40 UTC ( #426052=perlquestion: print w/ replies, xml ) Need Help??
eXile has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have some Class::DBI modules that I want to provide an additional name for. To play nice and not clutter up the global namespace I've made Class::DBI classes with names like OrganizationName::ProjectName::Object , but I'd also like these objects (preferably in a configurable way) to be available under shorter names like for instance ProjectName::Object (to avoid typo's, overly long lines of code when using these classes, etc.). Initially I thought that would be as simple as:
package OrganizationName::ProjectName::Object; *ProjectName::Object = *OrganizationName
but that doesn't work, at least not for my Class::DBI Object-modules: When I try to use an object by its shorter name, it's methods are not defined. Another try that failed is using inheritance, but somehow this doesn't work either:
package OrganizationName::ProjectName::Object; ... package code here ... package ProjectName::Object; use base "OrganizationName::ProjectName::Object"; 1;
(complains about not being able to find object methods via the short package name). I have tried to find information on this, but I think I'm using the wrong search terms. Can anybody tell me if what I want is possible, and where to find information about it?

Comment on Alias for object namespace
Select or Download Code
Re: Aliasing a namespace/object
by stvn (Monsignor) on Jan 28, 2005 at 17:48 UTC

    Ovid recently released aliased for just this type of problem.

    -stvn
Re: Aliasing a namespace/object
by dragonchild (Archbishop) on Jan 28, 2005 at 17:49 UTC
    I just added a similar feature to IOC this past month, along with configuration files that make it much easier to create them.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: Aliasing a namespace/object
by Ovid (Cardinal) on Jan 28, 2005 at 17:53 UTC

    Try my aliased module. It only works for objects, but that's what you wanted, even though I wasn't entirely clear how you were setting things up.

    # implicit aliasing takes the last part of the package name use aliased 'Really::Long::Class::Name'; my $name = Name->new; # explicit aliasing allows a rename use aliased 'Some::Long::Name::That::I::Hate' => 'Name';

    You can also try Package::Alias, but that requires you use the package prior to aliasing it to another namespace.

    Cheers,
    Ovid

    New address of my CGI Course.

Re: Alias for object namespace
by broquaint (Abbot) on Jan 29, 2005 at 01:34 UTC
    To achieve such aliasing in code simply qualify the package name with a trailing :: e.g
    { package foo; sub meth { printf "I was declared in %s, but called with %s\n", __PACKAGE__, $_[0]; } } *bar:: = *foo::; bar->meth; __output__ I was declared in foo, but called with bar
    HTH

    _________
    broquaint

Re: Alias for object namespace
by gaal (Parson) on Jan 29, 2005 at 14:01 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://426052]
Approved by stvn
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (12)
As of 2014-10-30 13:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (208 votes), past polls