Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to read a perl script from withink itself

Reply
Thread Tools

How to read a perl script from withink itself

 
 
Newsgroup Reader
Guest
Posts: n/a
 
      01-06-2006
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.

 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      01-07-2006

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.

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      01-07-2006
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

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      01-07-2006
Newsgroup Reader <> 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.
 
Reply With Quote
 
John Nurick
Guest
Posts: n/a
 
      01-08-2006
On 7 Jan 2006 02:04:36 -0800, "Brian McCauley" <>
wrote:

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


>You can abuse the DATA handle.


[snip]

> seek(DATA,0,0);


Please excuse my ignorance, but why is this preferable to

open $theScript, "<", $0

?



 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      01-08-2006
John Nurick:
> Brian McCauley:
>> Newsgroup Reader:


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

>>
>> You can abuse the DATA handle.
>> seek(DATA,0,0);

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

--
Affijn, Ruud

"Gewoon is een tijger."

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      01-08-2006

John Nurick wrote:
> On 7 Jan 2006 02:04:36 -0800, "Brian McCauley" <>
> wrote:
>
> >Newsgroup Reader wrote:
> >> Hi: Does anyone know how to read a perl script from within itself?

>
> >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?

 
Reply With Quote
 
John Nurick
Guest
Posts: n/a
 
      01-08-2006
On Sun, 8 Jan 2006 13:35:33 +0100, "Dr.Ruud" <rvtol+>
wrote:

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


Thank you.

 
Reply With Quote
 
Newsgroup Reader
Guest
Posts: n/a
 
      01-09-2006
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

 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      01-09-2006
Newsgroup Reader wrote:
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
obtain element name, or attribute and value of the document name itself, and some elemnts and attributes from an ancestor or the node itself using xquery Jeff Kish XML 4 10-30-2008 05:47 PM
problem calling perl script from SOAP server perl script pj Perl Misc 3 04-09-2004 10:23 PM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM
How to make Perl Script "POST" call from another Perl Script??? Wet Basement Perl 1 07-15-2003 10:25 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57