Re: Print Current Program Source Code
by GrandFather (Saint) on Aug 18, 2006 at 09:35 UTC
|
use strict;
use warnings;
seek DATA, 0, 0;
print while <DATA>;
__DATA__
Prints:
use strict;
use warnings;
seek DATA, 0, 0;
print while <DATA>;
__DATA__
Update Note that you must have either __END__ or __DATA__ at the end of the script (you may have data following that if you wish - it will be printed too).
DWIM is Perl's answer to Gödel
| [reply] [d/l] [select] |
Re: Print Current Program Source Code
by liverpole (Monsignor) on Aug 18, 2006 at 10:26 UTC
|
use strict;
use warnings;
system("cat $0");
If your on a Windows box, change cat to type, and consider adding quotation marks (for the likely case of a filename with spaces in it):
use strict;
use warnings;
system("type \"$0\"");
s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
| [reply] [d/l] [select] |
|
system(qq(cat "$0"));
--
< http://dave.org.uk>
"The first rule of Perl club is you do not talk about
Perl club." -- Chip Salzenberg
| [reply] [d/l] |
|
| [reply] [d/l] [select] |
|
open SRC, '<', $0 or die;
print for <SRC>;
close SRC;
| [reply] [d/l] |
Re: Print Current Program Source Code
by davidrw (Prior) on Aug 18, 2006 at 13:08 UTC
|
you can make a shell/batch wrapper and invoke the script as perlRunAndShowSrc foo.pl...
/usr/bin/perl $@
cat $1
Or, here's another way. First, define a module:
package foo;
use Filter::Simple;
FILTER {
$_ .= "\n;print qq{\n===SOURCE CODE===\n$_};";
};
1;
Then put that in your code:
use foo;
use strict;
use warnings;
print "hello world";
i think i might toss this into an Acme module later today, assuming there isn't one already..
| [reply] [d/l] [select] |
Re: Print Current Program Source Code
by davidrw (Prior) on Aug 27, 2006 at 19:40 UTC
|
use Acme::Echo 'after', src_fmt=>"Source of just-executed code:\n%s";
use strict;
use warnings;
print "hello world\n";
Produces the output:
hello world
Source of just-executed code:
use strict;
use warnings;
print "hello world\n";
| [reply] [d/l] [select] |
|
| [reply] |
|
well, yeah :) .. well, prints the enitre thing between the 'use Acme::Echo' statement and the end or a 'no use Acme::Echo' .. which could be handy .. and could turn on,off,on,off as desired..
And in lines mode you'll lose some whitespace ... though it provided a challenging Filter::Simple and PPI task -- you can see i had trouble recursing into compound statements .. it's on my project list to revist ;)
| [reply] |
Re: Print Current Program Source Code
by Hofmator (Curate) on Aug 18, 2006 at 10:55 UTC
|
I'm not sure I get your question. Let me try to rephrase.
You have a file called 'hello_world.pl' with the content you have shown. On the command promt you type perl hello_world.pl and get the printed hello world as a return.
Now you would like to know how to get the text of the program printed out, instead of the program executed. This depends on your operating system. On Unix/Linux you type cat hello_world.pl and on Windows you type type hello_world.pl.
For how to do these things from perl, see the other answers.
| [reply] [d/l] [select] |
Re: Print Current Program Source Code (HQ9+)
by ysth (Canon) on Aug 18, 2006 at 20:15 UTC
|
| [reply] |
|
For those wondering, a simple Google search reveals the following site:
to which brother ysth was cryptically referring to. See also http://en.wikipedia.org/wiki/HQ9+.
Alex / talexb / Toronto
"Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds
| [reply] |