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

This is a strange question, and asked for the purposed of an obfuscation exercise.
Is there a way for a running perl program to access its own source code (beyond using $0 to find its own name, opening its own .pl file, and slurping itself in)?

Replies are listed 'Best First'.
(jeffa) Re: Obfuscation: program seeing own source
by jeffa (Bishop) on Feb 08, 2001 at 03:07 UTC
    Mess around with this a bit:
    seek(DATA,0,0); print while(<DATA>); __DATA__
    Fun stuff ;)

    Jeff

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    F--F--F--F--F--F--F--F--
    (the triplet paradiddle)
    
      Excellent!

      But why does seek(DATA,0,0); go to the start of the file, not the start of __DATA__ ?

      andy.

      update: so it's a handle for the script itself... on disk, presumably, rather than in memory? shame.

      #!/usr/bin/perl -w use strict; print "foo\n"; seek(DATA,0,0); while (<DATA>) { s/foo/bar/ and ! /eval/ and eval ; } __DATA__
      versus
      #!/usr/bin/perl -w use strict; print "foo\n"; seek(DATA,0,0); my @me = <DATA> ; foreach (@me) { s/foo/bar/ and ! /eval/ and eval ; } foreach (@me) { s/bar/baz/ and ! /eval/ and eval ; } __DATA__
Quines
by MrNobo1024 (Hermit) on Feb 08, 2001 at 04:31 UTC
    I think what you're looking for is a quine (a program that displays its own source code without accessing it). Here's an example:
    $q=q(print"\$q=q($q);$q";);print"\$q=q($q);$q";
Re: Obfuscation: program seeing own source
by Adam (Vicar) on Feb 08, 2001 at 03:17 UTC
      I bought my camel shirt cool code too!
Re: Obfuscation: program seeing own source
by MeowChow (Vicar) on Feb 08, 2001 at 04:35 UTC
    When a project calls for self-modifying or self-reading code {g}, I like to stick my code in $_ and eval it like so:

    $_=' #obfuscated #self-modifying code #here #and then eval #again if you want to get really messy ',eval;
    Of course, there are other, more obfuscated ways to go about doing this too :-)
       MeowChow                                               
                    print $/='"',(`$^X\144oc $^X\146aq1`)[-2]
Re: Obfuscation: program seeing own source
by arhuman (Vicar) on Feb 08, 2001 at 12:42 UTC

    See Morph on obfuscation section.
    For it's what I've done in this prog.