Referencing modules nested in a directory structure

D

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();
 
B

Ben Morrow

Quoth David Smith said:
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
 
A

Andreas Pürzer

Ben said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top