Link to home
Start Free TrialLog in
Avatar of sridhar_dvjs
sridhar_dvjs

asked on

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
Avatar of sridhar_dvjs
sridhar_dvjs

ASKER

Edited text of question.
Avatar of ozo
How does your package implement new?
Here is the skeleton of my package.

#!/usr/local/bin/perl
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
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;


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 MyTest.pl 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.
praksahk,

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

Sridhar
ASKER CERTIFIED SOLUTION
Avatar of prakashk021799
prakashk021799

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial