circular import doesn't behave as expected?

Discussion in 'Perl Misc' started by asokoloski@gmail.com, Nov 4, 2005.

  1. Guest

    So I have two modules:

    === a.pm ===
    package a;
    use b;

    sub import {
    print "import in $_[0] called from ". (caller(1))[3] ." \n";
    }

    1;
    === b.pm ===
    package b;
    use a;

    sub import {
    print "import in $_[0] called from ". (caller(1))[3] ." \n";
    }

    1;
    ==========

    and a script:

    === test.pl ===
    #!/usr/bin/perl

    use strict;
    use warnings;

    use a;
    ==========

    Running this script prints:
    --------------
    import in b called from a::BEGIN
    import in a called from main::BEGIN
    --------------
    Whereas I would expect there to be another "import in a called from
    b::BEGIN" at the beginning, from the "use a;" in b.pm. I know that
    each module is only compiled once, but import should be called each
    time, and there are three "use" statements, so shouldn't there be three
    imports?

    What am i missing here?

    Thanks,
    Aaron
     
    , Nov 4, 2005
    #1
    1. Advertising

  2. Bob Walton Guest

    wrote:

    > So I have two modules:
    >
    > === a.pm ===
    > package a;
    > use b;
    >
    > sub import {
    > print "import in $_[0] called from ". (caller(1))[3] ." \n";
    > }
    >
    > 1;
    > === b.pm ===
    > package b;
    > use a;
    >
    > sub import {
    > print "import in $_[0] called from ". (caller(1))[3] ." \n";
    > }
    >
    > 1;
    > ==========
    >
    > and a script:
    >
    > === test.pl ===
    > #!/usr/bin/perl
    >
    > use strict;
    > use warnings;
    >
    > use a;
    > ==========
    >
    > Running this script prints:
    > --------------
    > import in b called from a::BEGIN
    > import in a called from main::BEGIN
    > --------------
    > Whereas I would expect there to be another "import in a called from
    > b::BEGIN" at the beginning, from the "use a;" in b.pm. I know that
    > each module is only compiled once, but import should be called each
    > time, and there are three "use" statements, so shouldn't there be three
    > imports?
    >
    > What am i missing here?


    Try defining your sub import{} first, as in for a.pm:

    package a;
    sub import {
    print "import in $_[0] called from ". (caller(1))[3] ." \n";
    }
    use b;
    1;

    Does that help? When I run it that way, sub import for package a
    prints twice. It is a question of whether your sub import is
    defined at the time of the call to import. Recognize that the
    calls to import() made by use() are occuring at compile time, and
    not everything has necessarily been compiled yet.
    >

    ....
    > Aaron
    >


    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
     
    Bob Walton, Nov 4, 2005
    #2
    1. Advertising

  3. Guest

    Yup, it does. Thanks everyone!

    I fixed the real problem, by the way, by putting the initialization for
    "@EXPORT" and such in a BEGIN block. But I wouldn't have thought of it
    by myself.
     
    , Nov 4, 2005
    #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. Joe Burnett
    Replies:
    0
    Views:
    2,048
    Joe Burnett
    Sep 7, 2003
  2. Kiuhnm
    Replies:
    16
    Views:
    746
    Jonathan Mcdougall
    Jan 3, 2005
  3. Stephan Aspridis

    Loop doesn't behave the way it's supposed to.

    Stephan Aspridis, Jan 3, 2004, in forum: C Programming
    Replies:
    19
    Views:
    525
    Stephan Aspridis
    Jan 11, 2004
  4. Alexey Verkhovsky
    Replies:
    8
    Views:
    163
    Hal Fulton
    Jul 18, 2004
  5. Ben Dong

    focus() does not behave as expected

    Ben Dong, Jul 8, 2003, in forum: Javascript
    Replies:
    0
    Views:
    94
    Ben Dong
    Jul 8, 2003
Loading...

Share This Page