?
Solved

calling package using variable in perl

Posted on 2010-11-22
2
Medium Priority
?
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 26

Accepted Solution

by:
wilcoxon earned 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Six Sigma Control Plans
Suggested Courses

770 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