How to read a perl script from withink itself

  • Thread starter Newsgroup Reader
  • Start date
N

Newsgroup Reader

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.
 
B

Brian McCauley

Newsgroup said:
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.
 
P

Paul Lalli

Newsgroup said:
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
 
A

Anno Siegel

Newsgroup Reader said:
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
 
D

Dr.Ruud

John Nurick:
Brian McCauley:

Please excuse my ignorance, but why is this preferable to

open $theScript, "<", $0

First show how you would set $theScript. Then show how you assure to
have reading rights on it at the open(), even when it is not stored on
some encrypted device.
 
B

Brian McCauley

John said:
You can abuse the DATA handle.
[snip]

seek(DATA,0,0);

Please excuse my ignorance, but why is this preferable to

open $theScript, "<", $0

$0 is not reliably a full path name that can be used to find the script
file.

A more relable path name can be found using the FindBin module. But
even that's not 100% relialble (for details "perldoc FindBin").

Besides, even if it were 100%, why reopen the file?
 
J

John Nurick

First show how you would set $theScript. Then show how you assure to
have reading rights on it at the open(), even when it is not stored on
some encrypted device.

Thank you.
 
N

Newsgroup Reader

Hi Paul:

The source code control repository puts in this version number as a
comment, so I cannot use the variable. The code looks like:

# $Revision: 1.9 $

Rehan
 
J

J. Gleixner

Newsgroup said:
Hi Paul:

The source code control repository puts in this version number as a
comment, so I cannot use the variable. The code looks like:

# $Revision: 1.9 $

Give this a try:

use vars qw( $VERSION );
( $VERSION ) = '$Revision:$ ' =~ /\$Revision:\s+([^\s]+)/;

Then, after you commit the version, $VERSION will be set to the Revision
from CVS. If you're using some other revision control, there should be
something similar.
 
N

Newsgroup Reader

Thanks, I'll try this. The source code control system being used in
this system is PVCS. I can read the version if I re-open the script as
a text file. Still have to try the DATA filehandle approach so I won't
have to re-open the file.
 
D

Darren Dunham

Newsgroup Reader said:
The source code control repository puts in this version number as a
comment

Whether it's truly a perl comment or not depends on the surrounding
lines.
, so I cannot use the variable. The code looks like:
# $Revision: 1.9 $

Is it located somewhere in particular in the file? I assume it's not
simply the first line. If you can code ahead of it, how about something
like this?

my $version = q(
# $Revision: 1.9 $
);

$version =~ s/^.*Revision\:\s*([\d.]+)\s.*$/$1/s;

print "My version is $version.\n";
 
C

Chris Richmond - MD6-FDC ~

Here's an alternate that works for us:

$::VersionString = q|$Id: asm2trc.pl,v 1.24 2005/11/28 22:37:03 crichmon Exp $|;

print "\n$::VersionString\n\n";

Same trick works with any of the other rcs/cvs variables.

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,573
Members
45,046
Latest member
Gavizuho

Latest Threads

Top