Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Question on eval "new ..."

Posted on 1999-07-05
8
Medium Priority
?
228 Views
Last Modified: 2010-03-04
(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
0
Comment
Question by:sridhar_dvjs
[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
  • 4
  • 3
8 Comments
 

Author Comment

by:sridhar_dvjs
ID: 1213864
Edited text of question.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1213865
How does your package implement new?
0
 

Author Comment

by:sridhar_dvjs
ID: 1213866
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
0
Independent Software Vendors: 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!

 
LVL 5

Expert Comment

by:prakashk021799
ID: 1213867
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;


0
 

Author Comment

by:sridhar_dvjs
ID: 1213868
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??

0
 
LVL 5

Expert Comment

by:prakashk021799
ID: 1213869
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.
0
 

Author Comment

by:sridhar_dvjs
ID: 1213870
praksahk,

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

Sridhar
0
 
LVL 5

Accepted Solution

by:
prakashk021799 earned 150 total points
ID: 1213871
OK.
0

Featured Post

Industry Leaders: 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 have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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

670 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