How to read a perl script from withink itself

Discussion in 'Perl Misc' started by Newsgroup Reader, Jan 6, 2006.

  1. 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.
     
    Newsgroup Reader, Jan 6, 2006
    #1
    1. Advertising

  2. 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.
     
    Brian McCauley, Jan 7, 2006
    #2
    1. Advertising

  3. Newsgroup Reader

    Paul Lalli Guest

    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
     
    Paul Lalli, Jan 7, 2006
    #3
  4. Newsgroup Reader

    Anno Siegel Guest

    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.
     
    Anno Siegel, Jan 7, 2006
    #4
  5. Newsgroup Reader

    John Nurick Guest

    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

    ?
     
    John Nurick, Jan 8, 2006
    #5
  6. Newsgroup Reader

    Dr.Ruud Guest

    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."
     
    Dr.Ruud, Jan 8, 2006
    #6
  7. 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?
     
    Brian McCauley, Jan 8, 2006
    #7
  8. Newsgroup Reader

    John Nurick Guest

    On Sun, 8 Jan 2006 13:35:33 +0100, "Dr.Ruud" <>
    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.
     
    John Nurick, Jan 8, 2006
    #8
  9. 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
     
    Newsgroup Reader, Jan 9, 2006
    #9
  10. Newsgroup Reader

    J. Gleixner Guest

    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.
     
    J. Gleixner, Jan 9, 2006
    #10
  11. 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.
     
    Newsgroup Reader, Jan 9, 2006
    #11
  12. Newsgroup Reader <> wrote:
    > Hi Paul:


    > 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";

    --
    Darren Dunham
    Senior Technical Consultant TAOS http://www.taos.com/
    Got some Dr Pepper? San Francisco, CA bay area
    < This line left intentionally blank to confuse you. >
     
    Darren Dunham, Jan 11, 2006
    #12
  13. 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
    --
    Chris Richmond | I don't speak for Intel & vise versa
     
    Chris Richmond - MD6-FDC ~, Jan 12, 2006
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wet Basement
    Replies:
    1
    Views:
    2,593
    BobMonk
    Jul 15, 2003
  2. dpackwood
    Replies:
    3
    Views:
    1,872
  3. Sujith Gangaraju

    read document in browser itself

    Sujith Gangaraju, Jul 23, 2008, in forum: Ruby
    Replies:
    2
    Views:
    328
    Florian Gilcher
    Jul 23, 2008
  4. pj
    Replies:
    3
    Views:
    323
  5. Petterson Mikael

    Execute another perl script from my perl script

    Petterson Mikael, Jan 5, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    155
    Paul Lalli
    Jan 5, 2005
Loading...

Share This Page