At 11:59 AM 4/20/04 -0700, [EMAIL PROTECTED] wrote: >I need to include a header/boilerplate file in several Perl scripts. >At this point, I'm using it as a module, but it's a big kludge. Essentially, > I want the functionality that you have in C, where you can just #include >the file, and it's evaluated in the scope of the file doing the #include'ing. > >I've considered using: > do "<file>"; >but it just doesn't seem to be the best solution. This header file needs >to do operations in the main:: namespace (like getting command line arguments, > etc), which is why doing it as a module is not working too well. Does >anybody have any suggestions?
I would recommend using the 'use' perl command. Make your package a module, and have it export symbols into the caller's namespace only when directed. In this scheme, the default is to export nothing (meaning that if you want to use it as a 'regular' module, you can); but it still allows you to tell it to export symbols, which is how you want to use it in your current scheme. The difference between the two cases would be like this: use YourModule (); # exports no symbols -- i.e. is considered "well-behaved" or, as you are doing it now: use YourModule qw(:all); # exports all symbols into caller's namespace To make this happen, your module would look like this: YourModule.pm: ------------- use warnings; use strict; package YourPackage; require Exporter; our @ISA = qw(Exporter); our %EXPORT_TAGS = (all => [qw( function_a function_b variable_a Exporter::export_ok_tags('all'); # add to @EXPORT_OK @EXPORT = qw(); our $variable_a = 'some value'; sub function_a { return 1; } sub function_b { return 2; } 1; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>