Question on eval "new ..."

(I deleted my old question and repeated here)
I tried the some thing like the following:

use strict;
# MyPakage implemets 'new'
# use MyPakage; this line is commented
my $PakageName = "MyPakage";
eval "use $PakageName";
print "eval(use): $@" if $@; # this line prints nothing
my $var = eval "new $PakageName";
print "eval(new): $@" if $@;  # this too prints nothing
my $var2 = $var->FunctionInMyPakage;

I am getting an error "can not call FunctionInMyPakage with an undefined value..."

What is the problem with the code?

- Sridhar
Who is Participating?
prakashk021799Connect With a Mentor Commented:
sridhar_dvjsAuthor Commented:
Edited text of question.
How does your package implement new?
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

sridhar_dvjsAuthor Commented:
Here is the skeleton of my package.

package MyPakage;
use strict;
use xyz;  # some more use statements here

sub new{
    my ($class) = shift;
    my ($self) = {};
    $self->{VARIABLE} = shift;
    return bless($self,$class);

sub FunctionInMyPakage
    my ($self) = shift;
    # do some operations here
    return 1;

1; # this line is important

My actual statement in the caller program is
my $var = eval "new $PakageName $DataVariable";

- Sridhar

I ran your code (I commented out the "use xyz" line; everything else was the same). I did not get any errors.

For some reason the new seem to be failing in your code. You might want to check the value of $var, immediately after eval new.

my $var = eval "new $PakageName $DataVariable";
die "new failed\n" unless defined $var;

sridhar_dvjsAuthor Commented:
I think I got the answer. Thanks to prakashk for the clue. The answer is

my $var = eval "new $PakageName '$DataVariable'";
# note the single quotes

However, I think this is a bug in perl. With out the single quotes, perl gives me the following:
Bareword "xxx" not allowed while "strict subs" in use at (eval 2) line 1.
Can't call method "FunctionInMyPakage" on an undefined value at line 18.

Why isn't $@ has these error strings??

No. It's not a bug.

When a piece of code is eval'ed, the STDERR output produced at _run-time_ by that code is saved in $@.  When eval handles the code, it is done in two steps: first it is compiled and then it is run. The message you are getting (about Barewords) is output during the compile-phase of eval (not during the run-phase). That's why it is not being saved in $@.

When I ran your code yesterday, I used an integer value for $DataVariable, which did not need to be enclosed in single-quotes. That's why I did not get any errors. You might have been using a string value, which needs to be enclosed in quotes (either single or double).

Try your "eval new" code (with no quotes around $DataVariable) without the eval and you should get the same error.
sridhar_dvjsAuthor Commented:

Thanks for clearing all my doubts. Please answer this question to award points.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.