Solved

calling package using variable in perl

Posted on 2010-11-22
2
296 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…

734 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