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) Hmmmm. . . 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: 
> 
> <snip> 
> #! perl -w 
> 
> use strict; 
> 
> my $selfprint="c:/scripts/selfprint.pl"; 
> 
> open SELFPRINT, "$selfprint" or die "Cannot open $selfprint for read :$!";
> 
> 
> while (<SELFPRINT>) { 
>     print; 
>      } 
> </snip> 
> 
> I opened up the DOS prompt in Windows 98, and ran the script.  The result
> 
> was the following: 
> 
> <snip> 
> 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 (<SELFPRINT>) { 
>     print; 
>      } 
> 
> C:\scripts> 
> </snip> 
> 
> 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 [email protected] 
> 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
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to