Module should not work, but works

Discussion in 'Perl Misc' started by Bart Van der Donck, Mar 10, 2005.

  1. Hello,

    This is a file named p.pl:

    #!/usr/bin/perl
    use strict;
    use warnings;
    use lib '/path/to/mm_e.pm/';
    use mm_e;
    mm::printok();
    printok();
    sub printok { print "ok from p.pl\n"; }

    This is a file named mm_e.pm:

    package mm;
    sub printok { print "ok from mm_e.pm\n"; }
    1;

    Result:

    % perl -w p.pl
    ok from mm_e.pm
    ok from p.pl
    %

    Why does this work fine ?
    Shouldn't "strict" or "warnings" give me at least an error about this
    kind of construction ? I 'm on perl 5.8.3 built for i386-freebsd.

    --
    Bart
    Bart Van der Donck, Mar 10, 2005
    #1
    1. Advertising

  2. Bart Van der Donck

    Arne Ruhnau Guest

    Bart Van der Donck wrote:
    > Why does this work fine ?
    > Shouldn't "strict" or "warnings" give me at least an error about this
    > kind of construction ? I 'm on perl 5.8.3 built for i386-freebsd.


    Hm, why shouldn't it? You name your package mm, use it, call printok
    residing in the namespace mm, and then you call printok residing in
    main. So, what's the deal?
    Maybe you misinterpret the filename and the packagename, which happen to
    be different?

    puzzled,

    Arne Ruhnau
    Arne Ruhnau, Mar 10, 2005
    #2
    1. Advertising

  3. Bart Van der Donck wrote:

    > Hello,
    >
    > This is a file named p.pl:
    >
    > #!/usr/bin/perl
    > use strict;
    > use warnings;
    > use lib '/path/to/mm_e.pm/';
    > use mm_e;
    > mm::printok();
    > printok();
    > sub printok { print "ok from p.pl\n"; }
    >
    > This is a file named mm_e.pm:
    >
    > package mm;
    > sub printok { print "ok from mm_e.pm\n"; }
    > 1;
    >
    > Result:
    >
    > % perl -w p.pl
    > ok from mm_e.pm
    > ok from p.pl
    > %
    >
    > Why does this work fine ?
    > Shouldn't "strict" or "warnings" give me at least an error about this
    > kind of construction ? I 'm on perl 5.8.3 built for i386-freebsd.
    >

    Um, what error? Why would perl complain about that construction? It's
    perfectly proper in every way. You've declared two subs, &mm::printok
    and &main::printok (with main being the default package name; all
    globals have a package name, and if you don't write a package statement,
    your default package is main. In mm_e.pm, the default package is
    mm, because you put in a package statement). Then you call them.
    Nothing wrong with any of that. Note that perl doesn't finish
    parsing your source until it finishes reading your source. It
    doesn't care that you used &main::printok before you defined it.

    --
    Christopher Mattern

    "Which one you figure tracked us?"
    "The ugly one, sir."
    "...Could you be more specific?"
    Chris Mattern, Mar 10, 2005
    #3
  4. Bart Van der Donck wrote:
    >
    > use lib '/path/to/mm_e.pm/';


    That line does not help much. @INC is supposed to contain paths to
    *directories* with .pm files, not paths to .pm files directly.

    I suppose that the reason why it still works is that mm_e.pm happens to
    be located in the same directory as p.pl, while your @INC includes the
    current directory.

    Others have answered your actual question.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Mar 10, 2005
    #4
  5. Arne Ruhnau wrote:

    > Maybe you misinterpret the filename and the packagename, which happen to
    > be different?


    Yes this one - I have always worked with same names there.

    Bart
    Bart Van der Donck, Mar 15, 2005
    #5
    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. Christian Seberino
    Replies:
    2
    Views:
    422
    Christian Seberino
    Feb 12, 2004
  2. John fabiani
    Replies:
    14
    Views:
    500
    Donn Cave
    Jul 2, 2004
  3. F. GEIGER
    Replies:
    3
    Views:
    756
    F. GEIGER
    Aug 6, 2004
  4. Bill Davy
    Replies:
    0
    Views:
    682
    Bill Davy
    May 12, 2005
  5. David Wang
    Replies:
    0
    Views:
    720
    David Wang
    Dec 1, 2006
Loading...

Share This Page