By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,776 Members | 1,777 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,776 IT Pros & Developers. It's quick & easy.

Regarding require function in perl

P: 9
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
Nov 18 '08 #1
Share this Question
Share on Google+
9 Replies


Ganon11
Expert 2.5K+
P: 3,652
The only example I can think of is when I was reading about "use integer;" - something to the effect of:
Expand|Select|Wrap|Line Numbers
  1. # MyScript.pl
  2. use strict;
  3. use warnings;
  4.  
  5. {
  6.    use integer; # In the current scope, all division is integer division
  7.    print 'Inside the brackets, 7/5 = ', 7/5, "\n";
  8. } # Now outside the scope, all division is decimal division
  9. print 'Outside the brackets, 7/5 = ', 7/5, "\n";
results in the following output:

Expand|Select|Wrap|Line Numbers
  1. Inside the brackets, 7/5 = 1
  2. Outside the brackets, 7/5 = 1.4
This suggests to me that the 'use' keyword only applies to the current scope.
Nov 19 '08 #2

eWish
Expert 100+
P: 971
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
Nov 19 '08 #3

Ganon11
Expert 2.5K+
P: 3,652
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.
Nov 19 '08 #4

KevinADC
Expert 2.5K+
P: 4,059
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.
Nov 19 '08 #5

P: 9
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.
Nov 19 '08 #6

P: 13
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.
Nov 19 '08 #7

Kelicula
Expert 100+
P: 176
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 &HomemadePackage::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.
Nov 19 '08 #8

P: 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.
Nov 19 '08 #9

P: 9
Hi all,
Thank you for all your suggestions. I am using finally a different sub name.
Nov 19 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.