Any Ideas About Embedding Codes in Runtime?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PerlPhi
    New Member
    • May 2007
    • 5

    Any Ideas About Embedding Codes in Runtime?

    hi! i have a Perl code in here that when ran the program accepts any Perl codes from the user input (<STDIN>, of course use no syntax errors), then after breaking the multiline input, the inputs will be part of the program codes and well executed simoultaneously after breaking the multiline input.

    my novice codes as follows:

    [CODE=perl]#!/perl/bin/perl

    use strict;

    if (-e "sign.me") {
    unlink "sign.me";
    } else {
    print "Please enter the Perl codes you want to insert: \n";
    my @nEW_cODES_ = <STDIN>;
    unshift @nEW_cODES_, "eval {\n";
    push @nEW_cODES_, "};\n";
    push @nEW_cODES_, "print \"An error occured: \$@\" if \$@;\n";

    open FILE, "< ".$0;
    my @rEVISED_cODES_ ;
    for (<FILE>) {
    if ($_ eq "#END\n") {
    for (@nEW_cODES_) {
    push @rEVISED_cODES_ , $_;
    }
    }
    push @rEVISED_cODES_ , $_;
    }
    close FILE;

    open FILE, "> ".$0;
    for (@rEVISED_cODES _) {
    print FILE $_;
    }
    close FILE;

    open FILE, "> sign.me";
    close FILE;

    system "perl ".$0;
    exit;
    }

    #START
    #END

    print "Hello World!";[/CODE]

    a sample of what i inputted was as follows:

    [CODE=perl]
    print "Enter the First Number: ";
    chomp(my $nUM1_ = <STDIN>);
    print "Enter the Second Number: ";
    chomp(my $nUM2_ = <STDIN>);
    print "The Sum of $nUM1_ and $nUM2_ is ",$nUM1_+$nUM2_ ."\n";
    [/CODE]

    the whole output of the program looks like this:

    Code:
    Please enter the Perl codes you want to insert:
    print "Enter the First Number: ";
    chomp(my $nUM1_ = <STDIN>);
    print "Enter the Second Number: ";
    chomp(my $nUM2_ = <STDIN>);
    print "The Sum of $nUM1_ and $nUM2_ is ",$nUM1_+$nUM2_."\n";
    ^Z
    Enter the First Number: 1
    Enter the Second Number: 2
    The Sum of 1 and 2 is 3
    Hello World!
    a brief explanation: any Perl codes you enter on the multiline input is stored on the same source file. when stored, this program calls the Perl interpreter to compile the new source file as well it is run. while after doing the system call the program exit then the new source file takes incharge. giving the look of a program as a single running program.

    is there any other way around to embed perl codes on the program source file of the currently running program on the fly?

    thanks

    From: PerlPhi
    Last edited by miller; May 16 '07, 07:50 AM. Reason: Code Tag and ReFormatting
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    Hello PerlPhi,

    The immediate and obvious question that arises from your description is what are you trying to accomplish? Are you trying to make a runtime perl interpreter? Is there some purpose behind this endevour? What is the problem with the method that you have already attempted, besides the obvious one where the point is rather lost?

    Answer these questions concerning what you are actually trying to accomplish and we might be able to set you on a better path.

    - Miller

    Comment

    • PerlPhi
      New Member
      • May 2007
      • 5

      #3
      Originally posted by miller
      Hello PerlPhi,

      The immediate and obvious question that arises from your description is what are you trying to accomplish? Are you trying to make a runtime perl interpreter? Is there some purpose behind this endevour? What is the problem with the method that you have already attempted, besides the obvious one where the point is rather lost?

      Answer these questions concerning what you are actually trying to accomplish and we might be able to set you on a better path.

      - Miller
      actually im a novice with perl... im just tweaking of what i've learned in the "Llama Book". i have no other intentions or porpuses of making that program but for learning. thats the truth... its just i want to know other ways than what i have did... maybe more efficient than what i made, because i understand that im just a begginner and i need words from my superiors... right? c",))

      Comment

      • miller
        Recognized Expert Top Contributor
        • Oct 2006
        • 1086

        #4
        Well, if you're just learning perl, the first advice I would give you is create better variable names. The ones that you are currently using are really hard to read.

        [CODE=perl]
        use strict;

        if (-e "sign.me") {
        unlink "sign.me";
        } else {
        print "Please enter the Perl codes you want to insert: \n";
        my @newCode = <STDIN>;
        unshift @newCode, "eval {\n";
        push @newCode, "};\n";
        push @newCode, "print \"An error occured: \$@\" if \$@;\n";

        open (FILE, "$0") or die "Can't open $0: $!";
        my @revisedCode;
        while (<FILE>) {
        if ($_ eq "#END\n") {
        push @revisedCode, @newCode;
        }
        push @revisedCode, $_;
        }
        close FILE;

        open (FILE, "> $0") or die "Can't open $0; $!";
        for (@revisedCode) {
        print FILE $_;
        }
        close FILE;

        open (FILE, "> sign.me") or die "Can't open sign.me: $!";
        close FILE;

        system "perl ".$0;
        exit;
        }

        #START
        #END

        print "Hello World!";
        [/CODE]

        You'll also notice that I editted the open statements slightly to add error checking. This is just a good habit to start getting into from the very beginning.

        Overall, I must say that this script is not very effective for what it does. The fact that you can add code that has syntax errors and then the script is unuseable until you manually fix it is rather badly designed.

        Fixing this isn't hard though. Just change the script so that it runs the eval statements immediately instead of hard coding them in the file. Here's a different script that allows you to write perl statements line by line that are immediately executed. Two limitations of this script are the fact that it does not allow you to use localized variables as my'd vars won't last past the line that they are defined. And it also won't let you do more than one line at a time.

        [CODE=perl]
        #!/usr/bin/perl

        # Perl Shell
        while ( <STDIN> ) {
        print(join(',', eval),"\n");
        print $@;
        }

        1;

        __END__
        [/CODE]

        Good luck in your learning

        - Miller

        Comment

        Working...