Referencing modules nested in a directory structure

Referencing modules nested in a directory structure

am 14.01.2008 22:24:17 von David Smith

Pardon the rookie question, but I haven't been able to find an answer
in either the Camel book or a web search.

I'm coming to Perl from a Java background... where you import classes
using a fully-qualified name, and then generally make use of it using
the base class name:


import myapp.dataojects.User;
....
User myUser = new User();


I've been emulating the same convention as far as organizing my Perl
modules in a directory structure based on functionality. However, I
find that when using them I have to always use the fully-qualified name:


use MyApp::DataObjects::User;
....
my $myUser = MyApp::DataObjects::User->new();


Is there any syntax by which I could access Perl objects by using the
object name alone, rather than the fully-qualified name? Such as this:


use MyApp::DataObjects::User;
....
my $myUser = User->new();

Re: Referencing modules nested in a directory structure

am 15.01.2008 00:24:00 von Ben Morrow

Quoth David Smith :
> Pardon the rookie question, but I haven't been able to find an answer
> in either the Camel book or a web search.
>
> I'm coming to Perl from a Java background... where you import classes
> using a fully-qualified name, and then generally make use of it using
> the base class name:
>
> import myapp.dataojects.User;
> ...
> User myUser = new User();
>
> I've been emulating the same convention as far as organizing my Perl
> modules in a directory structure based on functionality. However, I
> find that when using them I have to always use the fully-qualified name:
>
> use MyApp::DataObjects::User;
> ...
> my $myUser = MyApp::DataObjects::User->new();

Yup.

> Is there any syntax by which I could access Perl objects by using the
> object name alone, rather than the fully-qualified name? Such as this:
>
> use MyApp::DataObjects::User;
> ...
> my $myUser = User->new();

Nope, sorry :(. This is one of the flaws in Perl's package system, IMHO.
One thing that may allieviate the pain is that a package (class) name is
simply a string in Perl, so you can use a variable:

use MyApp::DataObjects::User;

my $USER = 'MyApp::DataObjects::User';

my $myUser = $USER->new;

If you were feeling evil you could even export this variable from
MA::DO::U by default.

Ben

Re: Referencing modules nested in a directory structure

am 15.01.2008 17:29:59 von Andreas Puerzer

Ben Morrow schrieb:
> Quoth David Smith :
>>
>> I'm coming to Perl from a Java background... where you import classes
>>using a fully-qualified name, and then generally make use of it using
>>the base class name:
>>
>>import myapp.dataojects.User;
>>...
>>User myUser = new User();
>>
>> I've been emulating the same convention as far as organizing my Perl
>>modules in a directory structure based on functionality. However, I
>>find that when using them I have to always use the fully-qualified name:
>>
>>use MyApp::DataObjects::User;
>>...
>>my $myUser = MyApp::DataObjects::User->new();
>
>
> Yup.
>
>
>> Is there any syntax by which I could access Perl objects by using the
>>object name alone, rather than the fully-qualified name? Such as this:
>>
>>use MyApp::DataObjects::User;
>>...
>>my $myUser = User->new();
>
>
> Nope, sorry :(. This is one of the flaws in Perl's package system, IMHO.
> One thing that may allieviate the pain is that a package (class) name is
> simply a string in Perl, so you can use a variable:
>
> use MyApp::DataObjects::User;
>
> my $USER = 'MyApp::DataObjects::User';
>
> my $myUser = $USER->new;
>
> If you were feeling evil you could even export this variable from
> MA::DO::U by default.
>

Yet Another Approach (for OO-Modules at least):

use aliased 'MyApp::DataObjects::User';
my $myUser = User->new();

http://search.cpan.org/~ovid/aliased-0.21/lib/aliased.pm

Andreas Puerzer

--
Have Fun,
and if you can't have fun,
have someone else's fun.
The Beautiful South