Linux - Perl: Printing the content of the script I am executing
up vote
1
down vote
favorite
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
add a comment |
up vote
1
down vote
favorite
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
"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
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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
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
linux perl
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
add a comment |
"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
add a comment |
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.
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;
If a script has the
__DATA__
or__END__
token in its source, then Perl also sets up theDATA
file handle. Initially, theDATA
file handle points to the text after
the__DATA__
or__END__
token, but it is actually opened to the whole source file, so you canseek
to the beginning of that file handle and access the entire script.
seek DATA, 0, 0;
my @this_script = <DATA>;
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;
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
add a comment |
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.
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;
If a script has the
__DATA__
or__END__
token in its source, then Perl also sets up theDATA
file handle. Initially, theDATA
file handle points to the text after
the__DATA__
or__END__
token, but it is actually opened to the whole source file, so you canseek
to the beginning of that file handle and access the entire script.
seek DATA, 0, 0;
my @this_script = <DATA>;
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;
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
add a comment |
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.
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;
If a script has the
__DATA__
or__END__
token in its source, then Perl also sets up theDATA
file handle. Initially, theDATA
file handle points to the text after
the__DATA__
or__END__
token, but it is actually opened to the whole source file, so you canseek
to the beginning of that file handle and access the entire script.
seek DATA, 0, 0;
my @this_script = <DATA>;
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;
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
add a comment |
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.
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;
If a script has the
__DATA__
or__END__
token in its source, then Perl also sets up theDATA
file handle. Initially, theDATA
file handle points to the text after
the__DATA__
or__END__
token, but it is actually opened to the whole source file, so you canseek
to the beginning of that file handle and access the entire script.
seek DATA, 0, 0;
my @this_script = <DATA>;
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;
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.
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;
If a script has the
__DATA__
or__END__
token in its source, then Perl also sets up theDATA
file handle. Initially, theDATA
file handle points to the text after
the__DATA__
or__END__
token, but it is actually opened to the whole source file, so you canseek
to the beginning of that file handle and access the entire script.
seek DATA, 0, 0;
my @this_script = <DATA>;
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;
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
"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