Module loading order

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

  1. newtan

    newtan Guest



    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.
    newtan, Feb 13, 2006
    1. Advertisements

  2. newtan

    Todd W Guest

    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
    1. Advertisements

  3. newtan

    newtan Guest

    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 hard-wired B there wouldn't work.
    newtan, Feb 13, 2006
  4. newtan

    newtan 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.
    newtan, Feb 13, 2006
  5. newtan

    xhoster Guest

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


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

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

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

    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?

    xhoster, Feb 13, 2006
  6. newtan

    xhoster Guest

    How can you have polymorphic behavior without having any "poly" to morph?
    Your example only has one object. By design, apparently.
    What is *supposed* to happen when you have these classes?

    xhoster, Feb 13, 2006
  7. newtan

    Ch Lamprecht Guest


    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 ...??

    Ch Lamprecht, Feb 14, 2006
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.