Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

passing package names as parameters to modules

passing package names as parameters to modules

passing package names as parameters to modules


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

RE: passing package names as parameters to modules

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..



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

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

Then in the helper module...



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

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close