Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Print Current Program Source Code

by Samy_rio (Vicar)
on Aug 18, 2006 at 09:34 UTC ( [id://568087]=perlquestion: print w/replies, xml ) Need Help??

Samy_rio has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I want to print the source code of the current program. Is it possible? If so how can I achieve that.

For example,

use strict; use warnings; print "hello world";

I want to print that source code exactly after execution in command prompt.

Regards,
Velusamy R.


eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Replies are listed 'Best First'.
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
Re: Print Current Program Source Code
by liverpole (Monsignor) on Aug 18, 2006 at 10:26 UTC
    Hi Samy_rio,

    Another way to do it in Linux:

    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$..$/

      You can have spaces in filenames on Linux (and Unix) too. And using qq// would avoid all that ugly escaping.

      system(qq(cat "$0"));
      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

        and
        system('cat', $0);
        would need no quoting at all =)

        update: actually only system(cat => $0); really needs no quotes... isn't that elegant?

      or just:
      open SRC, '<', $0 or die; print for <SRC>; close SRC;
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..
Re: Print Current Program Source Code
by davidrw (Prior) on Aug 27, 2006 at 19:40 UTC
    As promised by my earlier reply, Acme::Echo has just been uploaded to cpan:
    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";

      But that only prints part of the source. :)


      DWIM is Perl's answer to Gödel
        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 ;)
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.

    -- Hofmator

Re: Print Current Program Source Code (HQ9+)
by ysth (Canon) on Aug 18, 2006 at 20:15 UTC
    q

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://568087]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-04-16 12:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found