Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing package names as parameters to modules 1

Status
Not open for further replies.

coldbohemia

Programmer
Jun 5, 2008
30
US

i have a module that contains general use subroutines that
will be accessed from different scripts-

my question is:

can i pass a package names to these subroutines?

i want to be able to initialize package variables within these subs
the package names will differ depending on the calling script


 
 http://files.engineering.com/getfile.aspx?folder=c64590c8-abe0-49cb-90a0-a71f57d3ee57&file=uploadtotechtips.txt
Your problem is scope.

although you set the package var $Pack1::var1, as soon as the sub finishes it goes out of scope.

You could either look at using Exporter to make global functions available in your package as a utility helper module or perhaps look at moose for OOP.

However, you could use an interface type concept where you have setters for all your variables..

pack1.pl
Code:
#!/usr/bin/perl
   
use strict;
use warnings;
package Pack1;

# Set path to user modules (I use this to include the current path in the search environment var @INC)
use FindBin qw($Bin);
use lib "$Bin";

# use your helper module
use module1;   

# private vars for this package. 
my $var1;
 
# public setters
sub var1()
{
    my $self = shift;
    $var1 = shift;
}

# call helper module to set the local vars
module1->assignvars("Pack1");     

# print result of local variable
print $var1  ,"\n";

Then in the helper module...
module1.pm
Code:
#!/usr/bin/perl
   

 use strict;
 use warnings;
 
 package module1;
 
 sub assignvars {
      my ($self, $packagealias) = @_; 	
      $packagealias->var1("whatever would go here"); 
 }
 
 1;

So using the OOP interface paradigm, you create setter methods for all your variables.

As mentioned, you could use moose and declare object attributes and have moose create all the setters for you.

Hope this helps.

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top