Can I put a class in eval block

Discussion in 'Perl Misc' started by yong, Jan 16, 2006.

  1. yong

    yong Guest

    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
     
    yong, Jan 16, 2006
    #1
    1. Advertising

  2. yong

    Anno Siegel Guest

    yong <> wrote in comp.lang.perl.misc:
    > 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
    --
    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.
     
    Anno Siegel, Jan 16, 2006
    #2
    1. Advertising

  3. yong

    yong Guest

    Anno Siegel wrote:
    > yong <> wrote in comp.lang.perl.misc:
    > > 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
    > --
    > 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
     
    yong, Jan 17, 2006
    #3
  4. yong

    Anno Siegel Guest

    yong <> wrote in comp.lang.perl.misc:
    >
    > Anno Siegel wrote:
    > > yong <> wrote in comp.lang.perl.misc:
    > > > 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
    > > --
    > > 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";


    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.
     
    Anno Siegel, Jan 17, 2006
    #4
  5. yong

    yong Guest

    Anno Siegel wrote:
    > yong <> wrote in comp.lang.perl.misc:
    > >
    > > Anno Siegel wrote:
    > > > yong <> wrote in comp.lang.perl.misc:
    > > > > 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
    > > > --
    > > > 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";

    >
    > 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 :]
     
    yong, Jan 18, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Newton
    Replies:
    3
    Views:
    9,417
    Brock Allen
    Apr 4, 2005
  2. dee
    Replies:
    2
    Views:
    1,484
  3. morrell
    Replies:
    1
    Views:
    965
    roy axenov
    Oct 10, 2006
  4. Steve V
    Replies:
    6
    Views:
    238
    Steve V
    Apr 20, 2005
  5. matt neuburg
    Replies:
    2
    Views:
    120
    matt neuburg
    Mar 15, 2009
Loading...

Share This Page