Module loading order

Discussion in 'Perl Misc' started by newtan@gmail.com, Feb 13, 2006.

  1. Guest

    Hello,

    Description:

    package A;
    my $instance;
    sub new {
    my $class = shift;
    my %args = @_;
    return $instance if $instance;
    . . . stuff appropriate args into $instance ...
    return bless $instance, $class;
    }
    .. . .

    package B;
    use A;
    our @ISA = qw(A);
    __PACKAGE_->SUPER::new(some args);
    .. . .


    package XYZ;
    use A;
    my $o = new A;
    .. . .


    Now using them


    1 - This will work
    #!/usr/bin/perl -w
    use strict;
    use B;
    use XYZ;
    .....


    2 - This won't work
    #!/usr/bin/perl -w
    use strict;
    use XYZ;
    use B; <= this won't work
    .....


    Since in XYZ the singleton $o is actually instantiated with appropriate
    parameters by the line __PACKAGE__->SUPER::new in B, the order of
    module loading in 1) will work while that in 2) won't as 'use' implies
    BEGIN blocks in FIFO.


    Question: is there a way to ensure that a certain module is always
    loaded before other modules no matter where it is with "use". In this
    case, how can B be assured to be loaded before other aforementioned
    modules? Thanks.
     
    , Feb 13, 2006
    #1
    1. Advertising

  2. Todd W Guest

    <> wrote in message
    news:...
    > Hello,
    >
    > Description:
    >
    > package A;
    > my $instance;
    > sub new {
    > my $class = shift;
    > my %args = @_;
    > return $instance if $instance;
    > . . . stuff appropriate args into $instance ...
    > return bless $instance, $class;
    > }
    > . . .
    >
    > package B;
    > use A;
    > our @ISA = qw(A);
    > __PACKAGE_->SUPER::new(some args);
    > . . .
    >
    >
    > package XYZ;
    > use A;
    > my $o = new A;
    > . . .
    >
    >
    > Now using them
    >
    >
    > 1 - This will work
    > #!/usr/bin/perl -w
    > use strict;
    > use B;
    > use XYZ;
    > ....
    >
    >
    > 2 - This won't work
    > #!/usr/bin/perl -w
    > use strict;
    > use XYZ;
    > use B; <= this won't work
    > ....
    >
    >
    > Since in XYZ the singleton $o is actually instantiated with appropriate
    > parameters by the line __PACKAGE__->SUPER::new in B, the order of
    > module loading in 1) will work while that in 2) won't as 'use' implies
    > BEGIN blocks in FIFO.
    >
    >
    > Question: is there a way to ensure that a certain module is always
    > loaded before other modules no matter where it is with "use". In this
    > case, how can B be assured to be loaded before other aforementioned
    > modules? Thanks.
    >


    Instead of use()ing A in XYZ, "use B;" Then you won't need to say "use B;"
    in your driver.

    Todd W.
     
    Todd W, Feb 13, 2006
    #2
    1. Advertising

  3. Guest

    Todd W wrote:
    > <> wrote in message
    > news:...
    > > Hello,
    > >
    > > Description:
    > >
    > > package A;
    > > my $instance;
    > > sub new {
    > > my $class = shift;
    > > my %args = @_;
    > > return $instance if $instance;
    > > . . . stuff appropriate args into $instance ...
    > > return bless $instance, $class;
    > > }
    > > . . .
    > >
    > > package B;
    > > use A;
    > > our @ISA = qw(A);
    > > __PACKAGE_->SUPER::new(some args);
    > > . . .
    > >
    > >
    > > package XYZ;
    > > use A;
    > > my $o = new A;
    > > . . .
    > >
    > >
    > > Now using them
    > >
    > >
    > > 1 - This will work
    > > #!/usr/bin/perl -w
    > > use strict;
    > > use B;
    > > use XYZ;
    > > ....
    > >
    > >
    > > 2 - This won't work
    > > #!/usr/bin/perl -w
    > > use strict;
    > > use XYZ;
    > > use B; <= this won't work
    > > ....
    > >
    > >
    > > Since in XYZ the singleton $o is actually instantiated with appropriate
    > > parameters by the line __PACKAGE__->SUPER::new in B, the order of
    > > module loading in 1) will work while that in 2) won't as 'use' implies
    > > BEGIN blocks in FIFO.
    > >
    > >
    > > Question: is there a way to ensure that a certain module is always
    > > loaded before other modules no matter where it is with "use". In this
    > > case, how can B be assured to be loaded before other aforementioned
    > > modules? Thanks.
    > >

    >
    > Instead of use()ing A in XYZ, "use B;" Then you won't need to say "use B;"
    > in your driver.


    No, that would defeat the purpose of having the polymorphic behavior. A
    is the parent of all subsequent children inherited from it, thus it's
    able to act appropriately upon the type of the child package. B can't
    be used in this case because there would be potentially more inherited
    classes such as C, D, E etc..so hard-wired B there wouldn't work.



    >
    > Todd W.
     
    , Feb 13, 2006
    #3
  4. Guest

    Ah, sorry that i didn't make it clearer. The intention of A being used
    as a base class in XYZ is polymorphic. Depending on what type of child
    class (such as B, C etc..), it will act appropriately. However, it
    order for it to be somewhat useful, its child classes' constructor
    (__PACKAGE_->SUPER::new) should be invoked first so that the object is
    instantiated with appropriate attributes. Of course, the base class A
    does provide some defaults, but in this case, it does need info of the
    inherited classes. I do not want to restrict users to always place
    "use ChildOfA" before other use's as that would make it unintuitive to
    other programmers. Hope that's clearer and thanks for the suggestions.
     
    , Feb 13, 2006
    #4
  5. Guest

    wrote:
    > Hello,
    >
    > Description:
    >
    > package A;
    > my $instance;
    > sub new {
    > my $class = shift;
    > my %args = @_;
    > return $instance if $instance;
    > . . . stuff appropriate args into $instance ...


    ## Don't say it in English with ". . ." when you can say it in Perl.

    $instance={%args};

    ## It is shorter, clearer, and allows us to run the code.


    > return bless $instance, $class;
    > }
    > . . .


    1;

    >
    > package B;
    > use A;
    > our @ISA = qw(A);
    > __PACKAGE_->SUPER::new(some args);


    You are missing an "_" from __PACKAGE__.
    There is already a module named B. Choose something
    else for your example.


    > . . .
    >
    > package XYZ;
    > use A;
    > my $o = new A;
    > . . .
    >
    > Now using them
    >
    > 1 - This will work
    > #!/usr/bin/perl -w
    > use strict;
    > use B;
    > use XYZ;
    > ....
    >
    > 2 - This won't work
    > #!/usr/bin/perl -w
    > use strict;
    > use XYZ;
    > use B; <= this won't work


    It does "work", it just doesn't read your mind.


    > ....
    >
    > Since in XYZ the singleton $o is actually instantiated with appropriate
    > parameters by the line __PACKAGE__->SUPER::new in B, the order of
    > module loading in 1) will work while that in 2) won't as 'use' implies
    > BEGIN blocks in FIFO.
    >
    > Question: is there a way to ensure that a certain module is always
    > loaded before other modules no matter where it is with "use".


    I'm sure there is, but I doubt it is a very good way. perls needs to know
    which module you wanted loaded first--it can't just read your mind. I'm
    sure you could come up with some ackward way of telling it that using
    source filters or such, but why not use "use" to tell it this?

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Feb 13, 2006
    #5
  6. Guest

    wrote:
    > >
    > > Instead of use()ing A in XYZ, "use B;" Then you won't need to say "use
    > > B;" in your driver.

    >
    > No, that would defeat the purpose of having the polymorphic behavior.


    How can you have polymorphic behavior without having any "poly" to morph?
    Your example only has one object. By design, apparently.

    > A
    > is the parent of all subsequent children inherited from it, thus it's
    > able to act appropriately upon the type of the child package. B can't
    > be used in this case because there would be potentially more inherited
    > classes such as C, D, E etc.


    What is *supposed* to happen when you have these classes?

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Feb 13, 2006
    #6
  7. Ch Lamprecht Guest

    wrote:
    > Hello,
    >
    > Description:
    >
    > package A;
    > my $instance;
    > sub new {
    > my $class = shift;
    > my %args = @_;
    > return $instance if $instance;
    > . . . stuff appropriate args into $instance ...
    > return bless $instance, $class;
    > }
    > .. . .


    Hi,

    are you aware of the fact, that this method will return the very same
    blessed reference each time you call it (assuming that you assign a
    reference to $instance in the part of your code you didn't show - before
    calling bless).
    You will have one 'instance' ...
    This 'instance' will be shared by all your classes objects.
    If your derived classes call this new-method for instantiation they will
    share the same 'instance' too.
    Is that what you want?
    It does not look very much OO ...??

    Christoph


    --

    perl -e "print scalar reverse q//"
     
    Ch Lamprecht, Feb 14, 2006
    #7
    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. Soren Kuula
    Replies:
    2
    Views:
    545
    Soren Kuula
    Feb 1, 2004
  2. cspoh
    Replies:
    0
    Views:
    270
    cspoh
    Jul 31, 2003
  3. Stephan Kämper
    Replies:
    2
    Views:
    259
    Stephan Kämper
    Jan 18, 2004
  4. Vito Corleone

    Loading module in another module

    Vito Corleone, Jan 10, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    145
    Shawn Corey
    Jan 10, 2005
  5. ofir
    Replies:
    0
    Views:
    201
Loading...

Share This Page