Linux - Perl: Printing the content of the script I am executing











up vote
1
down vote

favorite
1












Is it possible to print the whole content of the script I am executing?



Since there are many things that will go on inside the script like the calling perl modules I will call in runtime (require "/dir/file";), the print lines I am executing inside an array (foreach(@array) {print "$_n";}).



Why do I need this? To study the script generation I am making, especially when errors are occurring. Error occurred on line 2000 (even I have only 1 thousand lines of script).










share|improve this question






















  • "Error occurred on line 2000 (even I have only 1 thousand lines of script)." This doesn't make sense, and points to either a file mismatch or a source filter. Requiring outside files and running loops do not arbitrarily create new lines of source code.
    – Grinnz
    Nov 19 at 18:13















up vote
1
down vote

favorite
1












Is it possible to print the whole content of the script I am executing?



Since there are many things that will go on inside the script like the calling perl modules I will call in runtime (require "/dir/file";), the print lines I am executing inside an array (foreach(@array) {print "$_n";}).



Why do I need this? To study the script generation I am making, especially when errors are occurring. Error occurred on line 2000 (even I have only 1 thousand lines of script).










share|improve this question






















  • "Error occurred on line 2000 (even I have only 1 thousand lines of script)." This doesn't make sense, and points to either a file mismatch or a source filter. Requiring outside files and running loops do not arbitrarily create new lines of source code.
    – Grinnz
    Nov 19 at 18:13













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





Is it possible to print the whole content of the script I am executing?



Since there are many things that will go on inside the script like the calling perl modules I will call in runtime (require "/dir/file";), the print lines I am executing inside an array (foreach(@array) {print "$_n";}).



Why do I need this? To study the script generation I am making, especially when errors are occurring. Error occurred on line 2000 (even I have only 1 thousand lines of script).










share|improve this question













Is it possible to print the whole content of the script I am executing?



Since there are many things that will go on inside the script like the calling perl modules I will call in runtime (require "/dir/file";), the print lines I am executing inside an array (foreach(@array) {print "$_n";}).



Why do I need this? To study the script generation I am making, especially when errors are occurring. Error occurred on line 2000 (even I have only 1 thousand lines of script).







linux perl






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 0:54









Dem

113




113












  • "Error occurred on line 2000 (even I have only 1 thousand lines of script)." This doesn't make sense, and points to either a file mismatch or a source filter. Requiring outside files and running loops do not arbitrarily create new lines of source code.
    – Grinnz
    Nov 19 at 18:13


















  • "Error occurred on line 2000 (even I have only 1 thousand lines of script)." This doesn't make sense, and points to either a file mismatch or a source filter. Requiring outside files and running loops do not arbitrarily create new lines of source code.
    – Grinnz
    Nov 19 at 18:13
















"Error occurred on line 2000 (even I have only 1 thousand lines of script)." This doesn't make sense, and points to either a file mismatch or a source filter. Requiring outside files and running loops do not arbitrarily create new lines of source code.
– Grinnz
Nov 19 at 18:13




"Error occurred on line 2000 (even I have only 1 thousand lines of script)." This doesn't make sense, and points to either a file mismatch or a source filter. Requiring outside files and running loops do not arbitrarily create new lines of source code.
– Grinnz
Nov 19 at 18:13












1 Answer
1






active

oldest

votes

















up vote
3
down vote













There are probably better ways to debug a script (the perl debugger, using Carp::Always to get stack traces with any errors and warnings), but nonetheless there are at least two three mechanisms for obtaining the source code of the running script.





  1. Since $0 contains the name of the file that perl is executing, you can read from it.



    open my $fh, '<', $0;
    my @this_script = <$fh>;
    close $fh;



  2. If a script has the __DATA__ or __END__ token in its source, then Perl also sets up the DATA file handle. Initially, the DATA file handle points to the text after
    the __DATA__ or __END__ token, but it is actually opened to the whole source file, so you can seek to the beginning of that file handle and access the entire script.



    seek DATA, 0, 0;
    my @this_script = <DATA>;



  3. HT Grinnz: the token __FILE__ in any Perl source file refers to the name of the file that contains that token



    open my $fh, '<', __FILE__;
    my @this_file = <$fh>;
    close $fh;







share|improve this answer























  • I would recommend opening __FILE__, not $0, since $0 can be modified and won't necessarily be the current file to begin with.
    – Grinnz
    Nov 19 at 18:11











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53367020%2flinux-perl-printing-the-content-of-the-script-i-am-executing%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote













There are probably better ways to debug a script (the perl debugger, using Carp::Always to get stack traces with any errors and warnings), but nonetheless there are at least two three mechanisms for obtaining the source code of the running script.





  1. Since $0 contains the name of the file that perl is executing, you can read from it.



    open my $fh, '<', $0;
    my @this_script = <$fh>;
    close $fh;



  2. If a script has the __DATA__ or __END__ token in its source, then Perl also sets up the DATA file handle. Initially, the DATA file handle points to the text after
    the __DATA__ or __END__ token, but it is actually opened to the whole source file, so you can seek to the beginning of that file handle and access the entire script.



    seek DATA, 0, 0;
    my @this_script = <DATA>;



  3. HT Grinnz: the token __FILE__ in any Perl source file refers to the name of the file that contains that token



    open my $fh, '<', __FILE__;
    my @this_file = <$fh>;
    close $fh;







share|improve this answer























  • I would recommend opening __FILE__, not $0, since $0 can be modified and won't necessarily be the current file to begin with.
    – Grinnz
    Nov 19 at 18:11















up vote
3
down vote













There are probably better ways to debug a script (the perl debugger, using Carp::Always to get stack traces with any errors and warnings), but nonetheless there are at least two three mechanisms for obtaining the source code of the running script.





  1. Since $0 contains the name of the file that perl is executing, you can read from it.



    open my $fh, '<', $0;
    my @this_script = <$fh>;
    close $fh;



  2. If a script has the __DATA__ or __END__ token in its source, then Perl also sets up the DATA file handle. Initially, the DATA file handle points to the text after
    the __DATA__ or __END__ token, but it is actually opened to the whole source file, so you can seek to the beginning of that file handle and access the entire script.



    seek DATA, 0, 0;
    my @this_script = <DATA>;



  3. HT Grinnz: the token __FILE__ in any Perl source file refers to the name of the file that contains that token



    open my $fh, '<', __FILE__;
    my @this_file = <$fh>;
    close $fh;







share|improve this answer























  • I would recommend opening __FILE__, not $0, since $0 can be modified and won't necessarily be the current file to begin with.
    – Grinnz
    Nov 19 at 18:11













up vote
3
down vote










up vote
3
down vote









There are probably better ways to debug a script (the perl debugger, using Carp::Always to get stack traces with any errors and warnings), but nonetheless there are at least two three mechanisms for obtaining the source code of the running script.





  1. Since $0 contains the name of the file that perl is executing, you can read from it.



    open my $fh, '<', $0;
    my @this_script = <$fh>;
    close $fh;



  2. If a script has the __DATA__ or __END__ token in its source, then Perl also sets up the DATA file handle. Initially, the DATA file handle points to the text after
    the __DATA__ or __END__ token, but it is actually opened to the whole source file, so you can seek to the beginning of that file handle and access the entire script.



    seek DATA, 0, 0;
    my @this_script = <DATA>;



  3. HT Grinnz: the token __FILE__ in any Perl source file refers to the name of the file that contains that token



    open my $fh, '<', __FILE__;
    my @this_file = <$fh>;
    close $fh;







share|improve this answer














There are probably better ways to debug a script (the perl debugger, using Carp::Always to get stack traces with any errors and warnings), but nonetheless there are at least two three mechanisms for obtaining the source code of the running script.





  1. Since $0 contains the name of the file that perl is executing, you can read from it.



    open my $fh, '<', $0;
    my @this_script = <$fh>;
    close $fh;



  2. If a script has the __DATA__ or __END__ token in its source, then Perl also sets up the DATA file handle. Initially, the DATA file handle points to the text after
    the __DATA__ or __END__ token, but it is actually opened to the whole source file, so you can seek to the beginning of that file handle and access the entire script.



    seek DATA, 0, 0;
    my @this_script = <DATA>;



  3. HT Grinnz: the token __FILE__ in any Perl source file refers to the name of the file that contains that token



    open my $fh, '<', __FILE__;
    my @this_file = <$fh>;
    close $fh;








share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 at 18:47

























answered Nov 19 at 3:32









mob

95.5k13129250




95.5k13129250












  • I would recommend opening __FILE__, not $0, since $0 can be modified and won't necessarily be the current file to begin with.
    – Grinnz
    Nov 19 at 18:11


















  • I would recommend opening __FILE__, not $0, since $0 can be modified and won't necessarily be the current file to begin with.
    – Grinnz
    Nov 19 at 18:11
















I would recommend opening __FILE__, not $0, since $0 can be modified and won't necessarily be the current file to begin with.
– Grinnz
Nov 19 at 18:11




I would recommend opening __FILE__, not $0, since $0 can be modified and won't necessarily be the current file to begin with.
– Grinnz
Nov 19 at 18:11


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53367020%2flinux-perl-printing-the-content-of-the-script-i-am-executing%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga