Title: RE: Script printing its own source code

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

_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to