Re: Script printing its own source code
> ... called quines ... And, in perl, one of the most amazing quine programs is Damian Conway's SelfGol. It's also the game of Life and a quine-izer for other programs - don't even think about looking at it. http://libarynth.f0.am/cgi-bin/twiki/view/Libarynth/SelfGOL I had the good fortune to sit in a nice enough bar, drinking beer (not the best idea considering) while he char by char worked his way through the script (it was an obfusticated perl submission). He includes evil things like stray parens/curlies so vi's '%' won't help you de-parse it and, even at a merer 1000 chars, managed to slip in useless code just to further befuddle. It was beyond humbling, made you want to give up even typing on a computer and go back to banging rocks and sticks together. a Andy Bach, Sys. Mangler Internet: [EMAIL PROTECTED] VOICE: (608) 261-5738 FAX 264-5932 self-reference, n. - See self-reference ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Script printing its own source code
Perl makes this even easier if the script is in a file... read from filehandle 0 (stdin for perl.exe as opposed to <> which will be your terminal rather than stdin). ---cut--- $x=q/test test test/; open 0 or die('eek'); undef $/; $a=<0>; close (0); print $a; print "\n$x\n"; ---cut--- Kind regards, Mark Anderson Service Improvement Programme Level 2, 113 Dundas Street Edinburgh, EH3 5DE Tel: 0131 523 8786 Mob: 07808 826 063 > -Original Message- > From: [EMAIL PROTECTED] > [SMTP:[EMAIL PROTECTED] On Behalf Of > Gardner, Sam > Sent: Tuesday, May 31, 2005 1:58 PM > To: '[EMAIL PROTECTED]'; Perl Mailing List > Subject: RE: Script printing its own source code > > *** WARNING : This message originates from the Internet *** > > > > 1) H. . . Did you chomp? > > 2) I don't think there's any problem at all with a script opening itself > as a text file. Keep in mind that when it's running it's been interpreted > and is in memory (so no file sharing issue). It could, of course, destroy > or modify itself. Whether you think that's a good thing or not depends on > the purpose and methods of your script. > > Sam Gardner > > GTO Application Development > > Keefe, Bruyette & Woods, Inc. > > 212-887-6753 > > > > > > -Original Message- > From: Ted Schuerzinger [ <mailto:[EMAIL PROTECTED]>] > Sent: Saturday, May 28, 2005 1:05 PM > To: Perl Mailing List > Subject: Script printing its own source code > > > Over on rec.puzzles on Usenet (<[EMAIL PROTECTED]>), somebody > > posted a puzzle asking about using programming languages to print out a > program's own source code. So, I quickly whipped up the following 11-line > > script in Perl: > > > #! perl -w > > use strict; > > my $selfprint="c:/scripts/selfprint.pl"; > > open SELFPRINT, "$selfprint" or die "Cannot open $selfprint for read :$!"; > > > while () { > print; > } > > > I opened up the DOS prompt in Windows 98, and ran the script. The result > > was the following: > > > Microsoft(R) Windows 98 > (C)Copyright Microsoft Corp 1981-1999. > > C:\WINDOWS>cd\scripts > > C:\scripts>perl selfprint.pl > #! perl -w > > use strict; > > my $selfprint="c:/scripts/selfprint.pl"; > > open SELFPRINT, "$selfprint" or die "Cannot open $selfprint for read :$!"; > > > while () { > print; > } > > C:\scripts> > > > Two questions: > > 1) When the script prints itself out, it prints an extra blank line before > > giving me the command line again. (I've got the script open in my text > editor, set to show line numbers, and there are definitely only 11 > lines.) Where's the blank line coming from? > > 2) Is it really a good idea for a script to be allowed to open itself? I > > don't know if I want to try having a script open itself for write, even if > > only to see what would happen. :-) > > -- > Ted Schuerzinger, [EMAIL PROTECTED] > ___ > Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com > To unsubscribe: <http://listserv.ActiveState.com/mailman/mysubs> > << File: ATT5659778.txt >> The Royal Bank of Scotland plc, Registered in Scotland No. 90312. Registered Office: 36 St Andrew Square, Edinburgh EH2 2YB The Royal Bank of Scotland plc is authorised and regulated by the Financial Services Authority and represents The Royal Bank of Scotland Marketing Group. The Bank sells life policies, collective investment schemes and pension products and advises only on the Marketing Group's range of these products and on a With-Profit Bond produced by Norwich Union Life (RBS) Limited. This e-mail message is confidential and for use by the addressee only. If the message is received by anyone other than the addressee, please return the message to the sender by replying to it and then delete the message from your computer. Internet e-mails are not necessarily secure. The Royal Bank of Scotland plc does not accept responsibility for changes made to this message after it was sent. Whilst all reasonable care has been taken to avoid the transmission of viruses, it is the responsibility of the recipient to ensure that the onward transmission, opening or use of this message and any attachments will not adversely affect its systems or data. No responsibility is accepted by The Royal Bank of Scotland plc in this regard and the recipient should carry out such virus and other checks as it considers appropriate. ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Script printing its own source code
Title: RE: Script printing its own source code 1) H. . . Did you chomp? 2) I don't think there's any problem at all with a script opening itself as a text file. Keep in mind that when it's running it's been interpreted and is in memory (so no file sharing issue). It could, of course, destroy or modify itself. Whether you think that's a good thing or not depends on the purpose and methods of your script. Sam Gardner GTO Application Development Keefe, Bruyette & Woods, Inc. 212-887-6753 -Original Message- From: Ted Schuerzinger [mailto:[EMAIL PROTECTED]] Sent: Saturday, May 28, 2005 1:05 PM To: Perl Mailing List Subject: Script printing its own source code Over on rec.puzzles on Usenet (<[EMAIL PROTECTED]>), somebody posted a puzzle asking about using programming languages to print out a program's own source code. So, I quickly whipped up the following 11-line script in Perl: #! perl -w use strict; my $selfprint="c:/scripts/selfprint.pl"; open SELFPRINT, "$selfprint" or die "Cannot open $selfprint for read :$!"; while () { print; } I opened up the DOS prompt in Windows 98, and ran the script. The result was the following: Microsoft(R) Windows 98 (C)Copyright Microsoft Corp 1981-1999. C:\WINDOWS>cd\scripts C:\scripts>perl selfprint.pl #! perl -w use strict; my $selfprint="c:/scripts/selfprint.pl"; open SELFPRINT, "$selfprint" or die "Cannot open $selfprint for read :$!"; while () { print; } C:\scripts> Two questions: 1) When the script prints itself out, it prints an extra blank line before giving me the command line again. (I've got the script open in my text editor, set to show line numbers, and there are definitely only 11 lines.) Where's the blank line coming from? 2) Is it really a good idea for a script to be allowed to open itself? I don't know if I want to try having a script open itself for write, even if only to see what would happen. :-) -- Ted Schuerzinger, [EMAIL PROTECTED] ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Script printing its own source code
Ted Schuerzinger wrote: > Over on rec.puzzles on Usenet (<[EMAIL PROTECTED]>), somebody posted > a puzzle asking about using programming languages to print out a program's > own source code. This is a classic programming challenge called "self-replicating program" or "quine". (Try Googling either of those.) The point of the challenge is that you are (sadly) *not* allowed to have the program simply read its own source code and echo it to output. The challenge is to write a program that will *programmatically* generate a new copy of itself. Quite a mind-bender if you think about it too much. The solution is easier in some languages than others. HTH David ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Script printing its own source code
Isn't that the easy part? (knowing the scriptname)? print "scriptname = ".$0."\n"; C:\perl\scripts>perl thisscript.pl scriptname = thisscript.pl -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Chris Wagner Sent: Saturday, May 28, 2005 3:23 PM To: Perl-Win32-Users@listserv.ActiveState.com Subject: Re: Script printing its own source code That's just the Windows command prompt putting in a line before returning to the prompt. If u did it in Bash u could get it to not do that. But for the puzzle they were probably talking about making a script that printed itself without knowing it's own name ahead of time. -- REMEMBER THE WORLD TRADE CENTER ---=< WTC 911 >=-- "...ne cede males" 0100 ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Script printing its own source code
That's just the Windows command prompt putting in a line before returning to the prompt. If u did it in Bash u could get it to not do that. But for the puzzle they were probably talking about making a script that printed itself without knowing it's own name ahead of time. -- REMEMBER THE WORLD TRADE CENTER ---=< WTC 911 >=-- "...ne cede males" 0100 ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Script printing its own source code
Over on rec.puzzles on Usenet (<[EMAIL PROTECTED]>), somebody posted a puzzle asking about using programming languages to print out a program's own source code. So, I quickly whipped up the following 11-line script in Perl: #! perl -w use strict; my $selfprint="c:/scripts/selfprint.pl"; open SELFPRINT, "$selfprint" or die "Cannot open $selfprint for read :$!"; while () { print; } I opened up the DOS prompt in Windows 98, and ran the script. The result was the following: Microsoft(R) Windows 98 (C)Copyright Microsoft Corp 1981-1999. C:\WINDOWS>cd\scripts C:\scripts>perl selfprint.pl #! perl -w use strict; my $selfprint="c:/scripts/selfprint.pl"; open SELFPRINT, "$selfprint" or die "Cannot open $selfprint for read :$!"; while () { print; } C:\scripts> Two questions: 1) When the script prints itself out, it prints an extra blank line before giving me the command line again. (I've got the script open in my text editor, set to show line numbers, and there are definitely only 11 lines.) Where's the blank line coming from? 2) Is it really a good idea for a script to be allowed to open itself? I don't know if I want to try having a script open itself for write, even if only to see what would happen. :-) -- Ted Schuerzinger, [EMAIL PROTECTED] ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs