From: Newsgroup Reader on
Hi: Does anyone know how to read a perl script from within itself?
Basically, I have a script that contains a version number that I would
like to output to a log file, but I need to output that from the script
itself, so I need to read it while the script is running. Is there any
way to do this? Thanks.

From: Alexis Huxley on
> Basically, I have a script that contains a version number that I would
> like to output to a log file, but I need to output that from the script
> itself, so I need to read it while the script is running. Is there any
> way to do this? Thanks.

Put the version number inside a variable. E.g.:

my($rcs_id) = '$Revision$';

or whatever text it is that (somewhere along the production line)
is replaced by the actual version number. In my case I do:

$main::version = 'THIS_TEXT_IS_REPLACED_BY_ANOTHER_PROG_AT_RELEASE_TIME_FOR_A_RELEASE_NUM';

and then, well, you can work out the rest.

HTH

Alexis
From: Anno Siegel on
Newsgroup Reader <ng.reader(a)gmail.com> wrote in comp.lang.perl.misc:
> Hi: Does anyone know how to read a perl script from within itself?

Alexis has shown up an alternative, but to answer the question (untested):

seek DATA, 0, 0 or die "Script '$0' not seekable";
while ( <DATA> ) {
last if /^__END__$/;
# watch the script lines passing by
}

This may fail if the script isn't in a normal disk file.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
From: Brian McCauley on

Newsgroup Reader wrote:
> Hi: Does anyone know how to read a perl script from within itself?

You can abuse the DATA handle.

Stick a __DATA__ at the end of the script and the Perl interpreter will
keep open the file descriptor it used to read the Perl source and
associate it with the special file handle *DATA.

Simply rewind that handle with

seek(DATA,0,0);

Then you can then read the script source from DATA.

> Basically, I have a script that contains a version number that I would
> like to output to a log file, but I need to output that from the script
> itself, so I need to read it while the script is running. Is there any
> way to do this?

Generally you should make the line that contains the version number be
a variable assigment or constant declaration.

From: Paul Lalli on
Newsgroup Reader wrote:
> Hi: Does anyone know how to read a perl script from within itself?
> Basically, I have a script that contains a version number

In what way does the script "contain" the version number?

> that I would
> like to output to a log file, but I need to output that from the script
> itself, so I need to read it while the script is running. Is there any
> way to do this?

That sounds very roundabout. The standard way of defining the version
number of the current script is:

our $VERSION = 1.2;

You can then simply output that variable:
print $log_fh "Version: $VERSION\n";

If you think you require something more complicated than that, show us
a relevant sample code that demonstrates this issue.

(Please read the Posting Guidelines for this group for clues on
effective questioning and follow-up techniques before replying)

Paul Lalli

 |  Next  |  Last
Pages: 1 2 3
Prev: Perl Integer Length
Next: FTP PUT with proxy?