Loading ...
Sorry, an error occurred while loading the content.

use and require

Expand Messages
  • sudhin_deshpande
    I have a MOD1.pm which has a routine m1(). So I have a prog1.pl with the lines use lib /home/libs ; use MOD1; use strict; m1(); This works. Now there is
    Message 1 of 7 , Mar 24, 2004
    • 0 Attachment
      I have a MOD1.pm which has a routine m1(). So I have a prog1.pl with
      the lines

      use lib "/home/libs";
      use MOD1;
      use strict;

      m1();

      This works. Now there is subs.lib which has a routine sub1 which I
      want to call within m1.
      So to MOD1.pm I add the line

      require "/home/subs.lib";

      and call sub1() within m1() This works too.

      Now there is a sub2() in subs.lib which I want to call from prog1.pl
      So I add the lines to prog1.pl

      require "/home/subs.lib";
      use lib "/home/libs";
      use MOD1;
      use strict;

      m1();

      sub2();

      Now I get an error "undefined subroutine &main::sub2 in prog1.pl !!!
      Any ideas how to fix this? Thanks.
    • Charles K. Clarkson
      ... Sounds like sub2() is in MOD1.pm. Either call it with the MOD1:: prefix or export it using Exporter. MOD1::sub2() HTH, Charles K. Clarkson -- Mobile
      Message 2 of 7 , Mar 24, 2004
      • 0 Attachment
        sudhin_deshpande <sudhin_deshpande@...> wrote:
        :
        : require "/home/subs.lib";
        : use lib "/home/libs";
        : use MOD1;
        : use strict;
        :
        : m1();
        :
        : sub2();
        :
        : Now I get an error "undefined subroutine &main::sub2
        : in prog1.pl !!! Any ideas how to fix this? Thanks.

        Sounds like sub2() is in MOD1.pm. Either call it
        with the "MOD1::" prefix or export it using Exporter.

        MOD1::sub2()


        HTH,

        Charles K. Clarkson
        --
        Mobile Homes Specialist
        254 968-8328
      • sudhin_deshpande
        No, sub2 is in subs.lib If I change prog1.pl like this require /home/subs.lib ; #use lib /home/libs ; #use MOD1; use strict; #m1(); sub2(); then it works,
        Message 3 of 7 , Mar 24, 2004
        • 0 Attachment
          No, sub2 is in subs.lib
          If I change prog1.pl like this

          require "/home/subs.lib";
          #use lib "/home/libs";
          #use MOD1;
          use strict;

          #m1();

          sub2();

          then it works, sub2 gets executed.
          But if I uncomment the lines for MOD1,
          then I get the error "undefined routine &main::sub2

          thanks

          --- In perl-beginner@yahoogroups.com, "Charles K. Clarkson"
          <cclarkson@h...> wrote:
          > sudhin_deshpande <sudhin_deshpande@y...> wrote:
          > :
          > : require "/home/subs.lib";
          > : use lib "/home/libs";
          > : use MOD1;
          > : use strict;
          > :
          > : m1();
          > :
          > : sub2();
          > :
          > : Now I get an error "undefined subroutine &main::sub2
          > : in prog1.pl !!! Any ideas how to fix this? Thanks.
          >
          > Sounds like sub2() is in MOD1.pm. Either call it
          > with the "MOD1::" prefix or export it using Exporter.
          >
          > MOD1::sub2()
          >
          >
          > HTH,
          >
          > Charles K. Clarkson
          > --
          > Mobile Homes Specialist
          > 254 968-8328
        • Charles K. Clarkson
          ... Hmmm... Now that sounds strange. use gets compiled before require does. Perhaps Perl sees the require in MOD1:: and doesn t load it again in
          Message 4 of 7 , Mar 24, 2004
          • 0 Attachment
            sudhin_deshpande <sudhin_deshpande@...> wrote:
            :
            : Charles K. Clarkson <cclarkson@h...> wrote:
            :
            : No, sub2 is in subs.lib
            :
            : If I change prog1.pl like this
            :
            : require "/home/subs.lib";
            : #use lib "/home/libs";
            : #use MOD1;
            : use strict;
            :
            : #m1();
            :
            : sub2();
            :
            : then it works, sub2 gets executed.
            :
            : But if I uncomment the lines for MOD1,
            : then I get the error "undefined routine &main::sub2


            Hmmm... Now that sounds strange. "use" gets compiled
            before "require" does. Perhaps Perl sees the require in
            "MOD1::" and doesn't load it again in "main::". That's just
            a guess, though. Try pulling the "require" out of MOD1.pm
            and see what happens.

            It still sounds like sub2() is in "MOD1::". Did you try
            using MOD1::sub2()?


            HTH,

            Charles K. Clarkson
            --
            Mobile Homes Specialist
            254 968-8328
          • sudhin_deshpande
            MOD1::sub2() works !!! But actually sub2 exists in subs.lib and not MOD1.pm. Yes, the require in MOD1.pm is causing this problem. But I need it because I want
            Message 5 of 7 , Mar 24, 2004
            • 0 Attachment
              MOD1::sub2() works !!! But actually sub2 exists in subs.lib and not
              MOD1.pm.
              Yes, the require in MOD1.pm is causing this problem.
              But I need it because I want to call sub1 of subs.lib within MOD1.pm

              I would prefer to invoke sub2 something like subs::sub2()
              Is there any documentation on using routines within a .lib
              library?

              thanks

              --- In perl-beginner@yahoogroups.com, "Charles K. Clarkson"
              <cclarkson@h...> wrote:
              > sudhin_deshpande <sudhin_deshpande@y...> wrote:
              > :
              > : Charles K. Clarkson <cclarkson@h...> wrote:
              > :
              > : No, sub2 is in subs.lib
              > :
              > : If I change prog1.pl like this
              > :
              > : require "/home/subs.lib";
              > : #use lib "/home/libs";
              > : #use MOD1;
              > : use strict;
              > :
              > : #m1();
              > :
              > : sub2();
              > :
              > : then it works, sub2 gets executed.
              > :
              > : But if I uncomment the lines for MOD1,
              > : then I get the error "undefined routine &main::sub2
              >
              >
              > Hmmm... Now that sounds strange. "use" gets compiled
              > before "require" does. Perhaps Perl sees the require in
              > "MOD1::" and doesn't load it again in "main::". That's just
              > a guess, though. Try pulling the "require" out of MOD1.pm
              > and see what happens.
              >
              > It still sounds like sub2() is in "MOD1::". Did you try
              > using MOD1::sub2()?
              >
              >
              > HTH,
              >
              > Charles K. Clarkson
              > --
              > Mobile Homes Specialist
              > 254 968-8328
            • Paul Archer
              Read up on require . You re reading the subroutine into MOD1, so it becomes part of MOD1( s namespace). My suggestion: change it to module format, and get
              Message 6 of 7 , Mar 24, 2004
              • 0 Attachment
                Read up on 'require'. You're reading the subroutine into MOD1, so it becomes
                part of MOD1('s namespace).
                My suggestion: change it to module format, and get away from using
                libraries.

                Paul Archer

                10:04pm, sudhin_deshpande wrote:

                > No, sub2 is in subs.lib
                > If I change prog1.pl like this
                >
                > require "/home/subs.lib";
                > #use lib "/home/libs";
                > #use MOD1;
                > use strict;
                >
                > #m1();
                >
                > sub2();
                >
                > then it works, sub2 gets executed.
                > But if I uncomment the lines for MOD1,
                > then I get the error "undefined routine &main::sub2
                >
                > thanks
                >
                > --- In perl-beginner@yahoogroups.com, "Charles K. Clarkson"
                > <cclarkson@h...> wrote:
                > > sudhin_deshpande <sudhin_deshpande@y...> wrote:
                > > :
                > > : require "/home/subs.lib";
                > > : use lib "/home/libs";
                > > : use MOD1;
                > > : use strict;
                > > :
                > > : m1();
                > > :
                > > : sub2();
                > > :
                > > : Now I get an error "undefined subroutine &main::sub2
                > > : in prog1.pl !!! Any ideas how to fix this? Thanks.
                > >
                > > Sounds like sub2() is in MOD1.pm. Either call it
                > > with the "MOD1::" prefix or export it using Exporter.
                > >
                > > MOD1::sub2()
                > >
                > >
                > > HTH,
                > >
                > > Charles K. Clarkson
                > > --
                > > Mobile Homes Specialist
                > > 254 968-8328
                >
                >
                >
                >
                > Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.html
                > Yahoo! Groups Links
                >
                >
                >
                >
                >

                -----------------
                WWJD?
                JWRTFM
                -----------------
              • Charles K. Clarkson
                ... MOD1:: is a namespace, not a file. When you require subs.lib, you are in essence including the contents of that file in the MOD1:: namespace. (It s not
                Message 7 of 7 , Mar 24, 2004
                • 0 Attachment
                  sudhin_deshpande <sudhin_deshpande@...> wrote:
                  :
                  : MOD1::sub2() works !!! But actually sub2 exists in subs.lib
                  : and not MOD1.pm.

                  "MOD1::" is a namespace, not a file. When you require
                  subs.lib, you are in essence including the contents of
                  that file in the "MOD1::" namespace. (It's not exactly the
                  same, but close.)


                  : Yes, the require in MOD1.pm is causing this problem.
                  : But I need it because I want to call sub1 of subs.lib
                  : within MOD1.pm
                  :
                  : I would prefer to invoke sub2 something like subs::sub2()
                  : Is there any documentation on using routines within a .lib
                  : library?

                  It would probably be better to change "subs.lib" into a
                  module and "use" it. If you have the time to learn objects
                  and classes, you could create a "subs" class to be a super
                  class of the "MOD1" class.


                  HTH,

                  Charles K. Clarkson
                  --
                  Mobile Homes Specialist
                  254 968-8328
                Your message has been successfully submitted and would be delivered to recipients shortly.