Professional Web Applications Themes

circular use bug? - PERL Miscellaneous

I'm strongly leaning towards declaring this a bug and being done with it, but I wanted to ask here to see if anyone can explain it. I have 2 packages, which use each other. ====== package a ====== package a; use strict; use warnings; use b; sub foo_a {print "a in foo\n"}; 1; ====== package b ====== package b; use lib '.'; use a qw(from b); sub foo_b {print "b in foo\n"}; 1; === All well and good. a uses b. b uses a. So, here's my connundrum: [kalel:~] jim% perl -c a.pm Subroutine foo_a redefined at a.pm line 13. ...

  1. #1

    Default circular use bug?

    I'm strongly leaning towards declaring this a bug and being done with
    it, but I wanted to ask here to see if anyone can explain it. I have 2
    packages, which use each other.

    ======
    package a
    ======

    package a;

    use strict;
    use warnings;
    use b;

    sub foo_a {print "a in foo\n"};


    1;


    ======
    package b
    ======

    package b;

    use lib '.';
    use a qw(from b);

    sub foo_b {print "b in foo\n"};

    1;

    ===


    All well and good. a uses b. b uses a.

    So, here's my connundrum:

    [kalel:~] jim% perl -c a.pm
    Subroutine foo_a redefined at a.pm line 13.
    a.pm syntax OK

    But:

    [kalel:~] jim% perl -Ma -e '1'
    [kalel:~] jim%

    Further testing has demonstrated that in all cases I can throw at it, if
    I -c a module with a circular use, I get a warning about a redefined
    subroutine. However, if I use the module as part of a script, I get no
    warning.

    Additionally, one thing that I've isolated is that if I use a script,
    then %INC is populated immediately. Here:

    =====
    new package a
    =====

    package a;

    BEGIN {print "BEGINNING IMPORT OF a\n"};
    BEGIN {print map {"a> $_ => $INC{$_}\n"} keys %INC};
    use strict;
    use warnings;
    use lib '.';
    use b;

    sub foo_a {print "a in foo\n"};

    1;


    #!/usr/bin/perl

    use strict;
    use warnings;
    BEGIN {print map {"w> $_ => $INC{$_}\n"} keys %INC};
    use a;


    [kalel:~] jim% ./test.pl
    w> Exporter.pm => /System/Library/Perl/Exporter.pm
    w> Carp.pm => /System/Library/Perl/Carp.pm
    w> strict.pm => /System/Library/Perl/strict.pm
    w> warnings.pm => /System/Library/Perl/warnings.pm
    BEGINNING IMPORT OF a
    a> Exporter.pm => /System/Library/Perl/Exporter.pm
    a> Carp.pm => /System/Library/Perl/Carp.pm
    a> strict.pm => /System/Library/Perl/strict.pm
    a> warnings.pm => /System/Library/Perl/warnings.pm
    a> a.pm => a.pm

    Note that %INC comes in with values.

    However,

    [kalel:~] jim% perl -c a.pm
    BEGINNING IMPORT OF a
    BEGINNING IMPORT OF a
    a> Exporter.pm => /System/Library/Perl/Exporter.pm
    a> Carp.pm => /System/Library/Perl/Carp.pm
    a> b.pm => b.pm
    a> strict.pm => /System/Library/Perl/strict.pm
    a> Config.pm => /System/Library/Perl/darwin/Config.pm
    a> warnings.pm => /System/Library/Perl/warnings.pm
    a> a.pm => a.pm
    a> lib.pm => /System/Library/Perl/lib.pm
    Subroutine foo_a redefined at a.pm line 14.
    a.pm syntax OK

    The first time that a.pm is imported, %INC is unpopulated in any form.
    Only once b.pm uses a, does %INC get populated.

    Incidentally, best as I can tell, a is used, then b is used, which uses
    a. This subsequent use of a compiles the subroutine. b then finishes
    compiling and the stack reverts back to compiling a, which re-compiles
    the same subroutine. moving the 'use b' line in a to the end of the file
    only succeeds in changing the order in which the subroutine is
    re-defined (i.e., it's first compiled as part of 'a', then 'b' uses 'a'
    and re-compiles it)


    Now, to me, this is all rather buggy behavior. Especially considering
    the fact that it warns me with -c a.pm but not with my test script (or
    -Ma -e '1'

    So, is there a clear explanation of this phenomenon? Or should I just
    declare it a bug and be done with it?

    FWIW, I see the same problem under 5.6.0 on OS X (10.2.6) and 5.8.0
    under linux (dunno the distro)

    Thanks,

    -Jim......
    Jim Thomason Guest

  2. #2

    Default Re: circular use bug?

    On Tue, 02 Sep 2003 23:46:16 GMT, Jim Thomason <jt_60004> wrote:
    [ snip code which is essentially:

    a.pm:
    package a;
    use b;
    #...stuff...

    b.pm:
    package b;
    use a;
    #...stuff...

    ]
    > So, here's my connundrum:
    >
    > [kalel:~] jim% perl -c a.pm
    > Subroutine foo_a redefined at a.pm line 13.
    > a.pm syntax OK
    >
    > But:
    >
    > [kalel:~] jim% perl -Ma -e '1'
    > [kalel:~] jim%
    [snippage]
    >
    > Now, to me, this is all rather buggy behavior. Especially considering
    > the fact that it warns me with -c a.pm but not with my test script (or
    > -Ma -e '1'
    >
    > So, is there a clear explanation of this phenomenon? Or should I just
    > declare it a bug and be done with it?
    When you do "perl -c a.pm" you are telling perl to run (well check in this
    case) the script a.pm. Perl does that. b.pm gets loaded by the "use b",
    then a.pm gets loaded by the "use a". a.pm the "module" and a.pm the
    "script", even though they are the same file, are not related to perl.

    When you do "perl -Ma -e '1'" a.pm and b.pm both get loaded as modules,
    and hence there is no problem.

    Perl does not populate %INC with the scipt it is told to run.

    The solution is "don't do that". Don't run the a.pm module as a script. If
    it doubles as a script then create another file containing "use a;" and use
    that as the script.

    You will get the same problem without the circular inclusion with:

    $ cat c.pm
    package c;
    sub foo_c { }
    1;
    $ perl -w -Mc c.pm
    Subroutine foo_c redefined at c.pm line 2.

    --
    Sam Holden

    Sam Holden Guest

  3. Moderated Post

    Default Re: circular use bug?

    Removed by Administrator
    Bob Walton Guest
    Moderated Post

Similar Threads

  1. Please help: Error - Circular Redirection
    By Jerry in forum PHP Development
    Replies: 3
    Last Post: June 6th, 08:43 AM
  2. Text in circular format
    By Keith P in forum Adobe Photoshop 7, CS, CS2 & CS3
    Replies: 5
    Last Post: August 22nd, 11:52 AM
  3. wanted to buy 86C circular polarizer
    By Mike Allen in forum Photography
    Replies: 0
    Last Post: August 20th, 12:51 AM
  4. Circular references are not supported
    By Dan C Douglas in forum ASP.NET Building Controls
    Replies: 3
    Last Post: August 13th, 09:21 PM
  5. circular border
    By Lisa in forum Macromedia Fireworks
    Replies: 1
    Last Post: July 11th, 06:54 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139