Referencing modules nested in a directory structure

Discussion in 'Perl Misc' started by David Smith, Jan 14, 2008.

  1. David Smith

    David Smith Guest

    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();
     
    David Smith, Jan 14, 2008
    #1
    1. Advertising

  2. David Smith

    Ben Morrow Guest

    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
     
    Ben Morrow, Jan 14, 2008
    #2
    1. Advertising

  3. 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
     
    Andreas Pürzer, Jan 15, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris
    Replies:
    0
    Views:
    1,956
    Chris
    Jan 24, 2006
  2. john
    Replies:
    4
    Views:
    712
    Jim Higson
    Jun 15, 2006
  3. SK101
    Replies:
    0
    Views:
    2,816
    SK101
    Oct 11, 2006
  4. Tagore

    forward referencing a structure

    Tagore, Aug 14, 2010, in forum: C Programming
    Replies:
    1
    Views:
    265
    Eric Sosman
    Aug 14, 2010
  5. Replies:
    1
    Views:
    230
Loading...

Share This Page