Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: I just want to include another file

by hmerrill (Friar)
on Sep 14, 2004 at 13:18 UTC ( [id://390830] : note . print w/replies, xml ) Need Help??


in reply to I just want to include another file

IMHO, that's exactly what modules are for - to contain functions or code commonly used. Bite the bullet and take the time to learn how to write modules. They're really not that tough :-)

Then once you've got your modules, you can put them in one place so that all your apps can get to them by doing something like this:

use lib '/path/to/your/module/directory'; use your_module1; use your_module2;
HTH.

Replies are listed 'Best First'.
Re^2: I just want to include another file
by Cap'n Steve (Friar) on Sep 14, 2004 at 18:51 UTC
    It just seems silly to me to clutter the code with extra modules and deal with exporting when I'll want the same symbols every time. I'll try use() again, I think having to declare variables in both files was what was messing me up. It seems like it should work just by using our() in the included file though.
      I don't have code at my fingertips to show you, but you don't have to declare variables in both files. If the export is done properly in the module, you can simply refer to the variables in the code that does the "use". I hope I'm not oversimplying here - it's been a while since I've done this, and I'm not actively using Perl now.

      I also think when I did create modules that I used a simplified approach of not doing "export"s at all. The "use" still worked the same, but to refer to variables in the module I had to use the module name, something like this:

      use lib '/path/to/my/module/directory'; use MyMod; print "MyMod var1 value is $MyMod::var1\n";
      Again, not sure if this is exactly right, but it should be close. I liked this approach because looking at the code you can always tell *where* a variable comes from - you don't need to go hunting around for it if you see that it hasn't been declared in the current module. You *are* using warnings(-w), strict, and declaring all your variables with "my", right??

      HTH.