Regarding require function in perl

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nirmal1349
    New Member
    • Sep 2008
    • 9

    Regarding require function in perl

    Hi,
    I want to know how can we limit the scope of require function in perl file. I have a perl file and it's importing another file in it using require like this:
    # main file is ABC.pl
    require "abc.pl"
    ...
    ...
    I perform some operations and now I want to limit the scope of require...Like if we have an open file function in perl then we also have the corresponding close function in perl. What is the corresponding function in perl for require.

    Thanks
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    The only example I can think of is when I was reading about "use integer;" - something to the effect of:
    Code:
    # MyScript.pl
    use strict;
    use warnings;
    
    {
       use integer; # In the current scope, all division is integer division
       print 'Inside the brackets, 7/5 = ', 7/5, "\n";
    } # Now outside the scope, all division is decimal division
    print 'Outside the brackets, 7/5 = ', 7/5, "\n";
    results in the following output:

    Code:
    Inside the brackets, 7/5 = 1
    Outside the brackets, 7/5 = 1.4
    This suggests to me that the 'use' keyword only applies to the current scope.

    Comment

    • eWish
      Recognized Expert Contributor
      • Jul 2007
      • 973

      #3
      I don't believe you can. I believe that you can set the scope of 'use' as Gannon suggests. You might also look at perlfunc

      --Kevin

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Regarding my response, after reading the perldocs for the integer module, it looks like the scope limitation is specific to this module, and is not a characteristic of the 'use' keyword.

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          You're trying to do something that seems inherently wrong. When you "require" a file it basically becomes part of the main script. Functions typically have no scope but you can make references to functions that do. You can also use OO programming to give an objects methods a limited scope.

          Comment

          • nirmal1349
            New Member
            • Sep 2008
            • 9

            #6
            Acutally I want to use 2 different functions with the same name present in 2 different perl files in the 3rd perl file.For this reason I import the 2 files using 'require' in the 3rd perl file.But how does my 3rd perl file knows which function it's calling,it could be the function either from the 1st file or the 2nd file.

            Comment

            • ahgan
              New Member
              • Jul 2008
              • 13

              #7
              Originally posted by nirmal1349
              Acutally I want to use 2 different functions with the same name present in 2 different perl files in the 3rd perl file.For this reason I import the 2 files using 'require' in the 3rd perl file.But how does my 3rd perl file knows which function it's calling,it could be the function either from the 1st file or the 2nd file.
              Sounded similar to what I've asked before. See thread849210 for some good ideas. I ended up keeping the functions with different names.

              Comment

              • Kelicula
                Recognized Expert New Member
                • Jul 2007
                • 176

                #8
                Perl knows what file the function came from with "use".
                All subroutines in the "main" file have the prefix "main", for simplicity this can be ommited. So that:

                &main::doIt( );

                is the same as:

                &doIt();

                If you "use" a package then the name of that package must be appended,
                IE:

                use HomemadePackage ;

                Then &HomemadePackag e::doIt();

                would not clash with a sunroutine in the main file called doIt();

                However with "require" the other file actually gets clobbered into the main package. All variables and subroutines must be carfeully considered.

                Really the only reliable way would be to rename the subs.
                Although I'm sure there's a workaround.

                Comment

                • nirmal1349
                  New Member
                  • Sep 2008
                  • 9

                  #9
                  I followed the thread...and I tried using undef..It dint work.I dont want to use package as that would fit into my requirements. I think I will eventually end up doing the same thing u did,using different subroutine names.

                  Comment

                  • nirmal1349
                    New Member
                    • Sep 2008
                    • 9

                    #10
                    Hi all,
                    Thank you for all your suggestions. I am using finally a different sub name.

                    Comment

                    Working...