Solved

calling package using variable in perl

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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl Sort Question 4 135
Which version of C should I use when Perl is too slow? 13 181
Check file date before getting folder path 1 74
Perl tar error 8 66
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…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 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