Solved

calling package using variable in perl

Posted on 2010-11-22
2
292 Views
Last Modified: 2012-06-27
Hi Expert.

I still new in perl. Hope that you guys can guide me with this. :-)

I try to make a perl program that automatically detect a name and use the config file to load the setting.

I'm finish with the detection program and now stuck at the config file.


I'm getting this error:
"Use of uninitialized value in concatenation (.) or string at one.pl line 13."

Thanks for helping.

Regards,
Adnan


The code as follow.

####### one.pl ############
#!/usr/bin/perl

use strict;
use warnings;


my $max_setting;
my $chassis = "car1";
my $readData = "MAXIMUM_SETTING";

$max_setting = ReadPackage($chassis, $readData);

print "$max_setting\n";


sub ReadPackage{

      my ($package, $function) = @_;
      print "package = $package\n";
      print "fucntion  = $function\n";
      my $dataRead;

      $dataRead = eval " use $package (); ${package}::$function()";

      return $dataRead;
}

#########end of one.pl #############

########## car1.pm ###############
package car1;

$MAXIMUM_SETTING = 100;
$RECOMMENDED_SETTING = 70;
$MINIMUM_SETTING = 20;

1;
############ end of car1.pm #########

0
Comment
Question by:adnan_m
2 Comments
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 34193434
You had a couple of issues.  You should always check $@ after calling eval.  You should always use strict and warnings even in modules.

### one.pl ###
#!/usr/bin/perl

use strict;
use warnings;

my $max_setting;
my $chassis = "car1";
my $readData = "MAXIMUM_SETTING";

$max_setting = ReadPackage($chassis, $readData);

print "$max_setting\n";

sub ReadPackage{
      my ($package, $function) = @_;
      print "package = $package\n";
      print "fucntion  = $function\n";
      my $dataRead;
      $dataRead = eval " use $package; \$${package}::$function";
      die "${package}::$function failed with\n\t$@" if $@;
      return $dataRead;
}
### end one.pl ###

### car1.pm ###
package car1;

use strict;
use warnings;

our $MAXIMUM_SETTING = 100;
our $RECOMMENDED_SETTING = 70;
our $MINIMUM_SETTING = 20;

1;
### end car1.pm ###
0
 

Author Comment

by:adnan_m
ID: 34193478
Thanks for your replay. I learn a lot for you. I never put the use warning and strict in module before.
I'll take note from your comment regarding to check $@ after calling eval.

The problem have been solve.

Thanks again.

Adnan

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now