Can I put a class in eval block

Y

yong

There is a server program which writed in perl can excute perl scripts
which come from remote client with eval{} call.The server program seemd
that first store the incoming script in a string and then eval them.Now
I have some scripts which needs some extra classes.How can I send the
extra classes with the scripts to the server?

Thanks
 
A

Anno Siegel

yong said:
There is a server program which writed in perl can excute perl scripts
which come from remote client with eval{} call.The server program seemd
that first store the incoming script in a string and then eval them.Now
I have some scripts which needs some extra classes.How can I send the
extra classes with the scripts to the server?

You'll have to wrap the "extra classes" into the script so that
the script can be run standalone. Roughly, this can be done by
adding a BEGIN block at the end of your script which contains the
class definition.

Anno
 
Y

yong

Anno said:
You'll have to wrap the "extra classes" into the script so that
the script can be run standalone. Roughly, this can be done by
adding a BEGIN block at the end of your script which contains the
class definition.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.


I write a piece of test script.But the class seems cannot be used.The
script is seemed like this:

===
use strict;

my $return=eval (
"
use \&main::package_1;
BEGIN {
package package_1;
use vars qw(\@ISA);
}
"
);

print $@."\n";
===

How can I call the package named package_1 from the main part?

Thanks
 
A

Anno Siegel

yong said:
I write a piece of test script.But the class seems cannot be used.The
script is seemed like this:

===
use strict;

my $return=eval (
"
use \&main::package_1;
BEGIN {
package package_1;
use vars qw(\@ISA);
}
"
);

print $@."\n";

The "use" statement in your code is nonsense. You are trying to "use"
what looks like a code-ref, but that is not what "use" expects. Your
code-ref wouldn't be defined anyway. With this technique you don't
need a "use" statement at all because the code is no longer external
to your file.

What's in your BEGIN block doesn't look much like a class definition.

my $code = <<'EOC';
my $ob = MyClass->new( 'gaga');
print $ob->value, "\n";

BEGIN {
package MyClass;

sub new {
bless { value => $_[ 1]}, $_[ 0];
}

sub value { $_[ 0]->{ value} }
}
EOC

eval $code;

Anno
 
Y

yong

Anno said:
yong said:
I write a piece of test script.But the class seems cannot be used.The
script is seemed like this:

===
use strict;

my $return=eval (
"
use \&main::package_1;
BEGIN {
package package_1;
use vars qw(\@ISA);
}
"
);

print $@."\n";

The "use" statement in your code is nonsense. You are trying to "use"
what looks like a code-ref, but that is not what "use" expects. Your
code-ref wouldn't be defined anyway. With this technique you don't
need a "use" statement at all because the code is no longer external
to your file.

What's in your BEGIN block doesn't look much like a class definition.

my $code = <<'EOC';
my $ob = MyClass->new( 'gaga');
print $ob->value, "\n";

BEGIN {
package MyClass;

sub new {
bless { value => $_[ 1]}, $_[ 0];
}

sub value { $_[ 0]->{ value} }
}
EOC

eval $code;

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.


It works

Thanks :]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top