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

Hi wise perl monks,

is it possible that a script can get its own code as an string variable (or an array of strings).
something like the Pod::Usage

thanking you in anticipation
vlad

Replies are listed 'Best First'.
Re: get current script as text
by BrowserUk (Patriarch) on Apr 24, 2012 at 14:01 UTC

    The following snippet will put the code of the current top-level script into a variable:

    my $codeAsText = do{ local( @ARGV, $/ ) = $0; <>; };

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      mmh... Your code don't show nothing in my computer (?)

      This was my idea, but without success still, close, but not cigar... I wonder why

      open (my $foo, $0); # binmode $foo, raw; # nothing changes with or without this line print $foo; close $foo;

      perl script.pl prints

      GLOB(0x9b717dc)

      What I'm doing wrong?

        mmh... Your code don't show nothing in my computer (?)

        My code should work fine anywhere. Post the exact code you are using that is failing.

        open (my $foo, $0); print $foo; close $foo; What I'm doing wrong?

        You are never reading from the file you opened.

        You are printing the filehandle you opened.

        Perhaps you meant:

        open (my $foo, $0); print <$foo>; close $foo;

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

      thanks that is exactly that i searched.

      Very clever! I eventually did figure it out, but an explanation would have been appreciated.

        Very clever!

        It's not actually intended to be "clever". Just concise and proficient. Ie. idiomatic.

        I eventually did figure it out, but an explanation would have been appreciated.

        Had anyone asked for an explanation, it would have been forthcoming.

        But isn't it just really satisfying when you get there on your own.

        What's more, the idiom will probably stay with you now, and come back to you when you need it.

        For reference, see Cheap idioms.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

Re: get current script as text
by JavaFan (Canon) on Apr 24, 2012 at 16:09 UTC
    One of the rare times you can use 1-arg open!
    local $/;open 0;my $t=<0>;
Re: get current script as text
by Anonymous Monk on Apr 24, 2012 at 13:57 UTC