473,386 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. #!/perl/bin/perl
  2.  
  3. use strict;
  4.  
  5. if (-e "sign.me") {
  6.     unlink "sign.me";
  7. } else {
  8.     print "Please enter the Perl codes you want to insert: \n";
  9.     my @nEW_cODES_ = <STDIN>;
  10.     unshift @nEW_cODES_, "eval {\n";
  11.     push @nEW_cODES_, "};\n";
  12.     push @nEW_cODES_, "print \"An error occured: \$@\" if \$@;\n";
  13.  
  14.     open FILE, "< ".$0;
  15.     my @rEVISED_cODES_;
  16.     for (<FILE>) {
  17.         if ($_ eq "#END\n") {
  18.             for (@nEW_cODES_) {
  19.                 push @rEVISED_cODES_, $_;
  20.             }
  21.         }
  22.         push @rEVISED_cODES_, $_;
  23.     }
  24.     close FILE;
  25.  
  26.     open FILE, "> ".$0;
  27.     for (@rEVISED_cODES_) {
  28.         print FILE $_;
  29.     }
  30.     close FILE;
  31.  
  32.     open FILE, "> sign.me";
  33.     close FILE;
  34.  
  35.     system "perl ".$0;
  36.     exit;
  37. }
  38.  
  39. #START
  40. #END
  41.  
  42. print "Hello World!";
a sample of what i inputted was as follows:

Expand|Select|Wrap|Line Numbers
  1. print "Enter the First Number: ";
  2. chomp(my $nUM1_ = <STDIN>);
  3. print "Enter the Second Number: ";
  4. chomp(my $nUM2_ = <STDIN>);
  5. print "The Sum of $nUM1_ and $nUM2_ is ",$nUM1_+$nUM2_."\n";
  6.  
the whole output of the program looks like this:

Expand|Select|Wrap|Line Numbers
  1. Please enter the Perl codes you want to insert:
  2. print "Enter the First Number: ";
  3. chomp(my $nUM1_ = <STDIN>);
  4. print "Enter the Second Number: ";
  5. chomp(my $nUM2_ = <STDIN>);
  6. print "The Sum of $nUM1_ and $nUM2_ is ",$nUM1_+$nUM2_."\n";
  7. ^Z
  8. Enter the First Number: 1
  9. Enter the Second Number: 2
  10. The Sum of 1 and 2 is 3
  11. Hello World!
  12.  
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
May 16 '07 #1
3 1372
miller
1,089 Expert 1GB
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
May 16 '07 #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
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",))
May 16 '07 #3
miller
1,089 Expert 1GB
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.

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2.  
  3. if (-e "sign.me") {
  4.     unlink "sign.me";
  5. } else {
  6.     print "Please enter the Perl codes you want to insert: \n";
  7.     my @newCode = <STDIN>;
  8.     unshift @newCode, "eval {\n";
  9.     push @newCode, "};\n";
  10.     push @newCode, "print \"An error occured: \$@\" if \$@;\n";
  11.  
  12.     open (FILE, "$0") or die "Can't open $0: $!";
  13.     my @revisedCode;
  14.     while (<FILE>) {
  15.         if ($_ eq "#END\n") {
  16.             push @revisedCode, @newCode;
  17.         }
  18.         push @revisedCode, $_;
  19.     }
  20.     close FILE;
  21.  
  22.     open (FILE, "> $0") or die "Can't open $0; $!";
  23.     for (@revisedCode) {
  24.         print FILE $_;
  25.     }
  26.     close FILE;
  27.  
  28.     open (FILE, "> sign.me") or die "Can't open sign.me: $!";
  29.     close FILE;
  30.  
  31.     system "perl ".$0;
  32.     exit;
  33. }
  34.  
  35. #START
  36. #END
  37.  
  38. print "Hello World!";
  39.  
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.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. # Perl Shell
  4. while ( <STDIN> ) {
  5.     print(join(',',eval),"\n");
  6.     print $@;
  7. }
  8.  
  9. 1;
  10.  
  11. __END__
  12.  
Good luck in your learning

- Miller
May 16 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Roose | last post by:
With some googling I have found these resources: http://docs.python.org/ext/win-dlls.html http://www.python.org/doc/faq/windows.html I have a large Win32/MFC/C/C++ application that has an...
56
by: alan b | last post by:
I copied it and tried to edit the movie.htm below in the <PARAM NAME=*** location of the file where the videoclip is on my hard drive. It is already recorded by Windows Media Player. My version...
4
by: Mok-Kong Shen | last post by:
Apology, if this is OT. (Please kindly refer me eventually to another group.) With VC++ I can embed asm statements in a function e.g. as follows: __asm { ........ je lab
1
by: gavinpaterson | last post by:
Dear Pythoners, I am writing as I am having trouble embedding a Python program into a Win XP C++ console application. I have written a script file for importing and I am trying to use the...
6
by: mistabean | last post by:
Hello, first of all, I am a programming newbie, especially in python... Onwards to the problem, I have been having difficulty embedding a python module into my C/C++ program. (just a test...
3
by: anonymisiert85 | last post by:
At the moment i can run python-string-code from C (MinGW, WinXP) But how can i register a C-function in python-RUNTIME and call this C function from python - without wrapper dll's or libs??? ...
1
by: openuser | last post by:
Hello, I've been researching how to embed python into C/C++. And, I learned how to write c/c++ code that, in threory, should do its job in embedding Python module into itself. Here is what I have.....
0
by: Severian | last post by:
I am working on embedding Python 2.5 in my C++ application, and I have a few questions: 1) My application is multi-threaded; what problems should I be aware of if I create a separate interpreter...
7
by: juliangrendell | last post by:
Hi experts- I have a potential use case of embedding Python where it must co- operate with the host c/c++ application in a single thread. That is, the c code and python interpreter need to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.