Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

How to determine current line executing in Script

by bearslumber (Novice)
on Dec 04, 2008 at 12:08 UTC ( #727944=perlquestion: print w/ replies, xml ) Need Help??
bearslumber has asked for the wisdom of the Perl Monks concerning the following question:

Hallo Monks,

I am trying to find a way to dynamically determine the current line being executed in my script.

I have a batch script which I accumulate a progress report or log whilst it is running and then output the log to a file on completion. I wish to add the current line number being executed in the script to each instance in the report

The following describes a snippet of code to demonstrate my requirement. I have annotated the code with line numbers for demonstration purposes

1. #!/usr/bin/perl 2. use warnings; 3. use strict; 4. 5. my @log ; 6. . . . 25. my $success = &Get_Item_From_File ($file) 26. 27. if ($Success) { 28. $_ref_DB_DD_OSD = &Get_DB_OSD ($_ref_Model) 29. } 30. else { 31. push (@log, "Read $file not successfull" . "31") ; 32. } . . . 1024 print <FILE> @log ; 1025 exit ; .

The following demonstrates the hard coded method at line 31, but if there is a way of determining this dynamically then this would reduce the need to have to manually update the hard coded value whenever the script is modified.

I envisage substituting line 31 as follows:

 push (@log, "Read $file not successfull" . &function_returning_current_line) ;

or anything similar; i.e. Get a near enough line number before the call to push etc

Any help would be greatly appreciated

Best regards

Lucas Redding AKA Bearslumber

Replies are listed 'Best First'.
Re: How to determine current line executing in Script
by Corion (Pope) on Dec 04, 2008 at 12:14 UTC

    Also see perldata about __LINE__, and possibly caller, should you want to put your logging code into a subroutine.

Re: How to determine current line executing in Script
by ccn (Vicar) on Dec 04, 2008 at 12:11 UTC

      Hi ccn

      Many thanks for your prompt reply. I blinked and you had already replied.

      I have looked at Log4Perl and having read the POD, it does just the job. i.e. layout = %L.

      Much appreciated.

      Lucas AKA: Bearslumber

Re: How to determine current line executing in Script
by clinton (Priest) on Dec 04, 2008 at 12:15 UTC
    You can use __LINE__. See "Special literals" in perldata.
Re: How to determine current line executing in Script
by bearslumber (Novice) on Dec 04, 2008 at 13:47 UTC

    All.

    Many thanks for your replies. I am grateful for all responses.

    Here is my conclusion...

    1. Log4Perl: Brilliant package. Does everything I could ever have imagined would be required in a log. I just wish I had known about it when I embarked on this project. I would use this if I was to start again.

    2. Perdata __LINE__: Getting there. A quick solution, seems to be just what I was hoping for.

    3. caller: This is icing on the cake.

    4. Make use of warn, $SIG{__WARN__} and END {...}: And I can now ensure I get the log out regardless of the death of my script.

    I am truly spoilt for choice, and for this project will definately be using caller and will Make use of warn, $SIG{__WARN__} and END {...}:.

    I shall definitley find use for verything sometime in future.

    Many thanks again.

    Lucas Bearslumber

Re: How to determine current line executing in Script
by Anonymous Monk on Dec 04, 2008 at 12:56 UTC
    Make use of warn, $SIG{__WARN__} and END {...}. If the argument to warn doesn't end with a new line ( warn $x." "; ) then the line number will be appended. In $SIG{__WARN__} do a "push(@log,"@_");". In the END block print @log. You probably should add a $SIG{__DIE__} where you also do the "push(@log ..." before you re-raise the die. That way you'll get a log if you "die".
      #!/usr/bin/perl use strict; use warnings; my @log="\n"; BEGIN { $SIG{__WARN__}=sub { push(@log,"@_"); }; $SIG{__DIE__}=sub { push(@log,"@_"); }; } # BEGIN END { if (@log) { print @log; }; } # END warn "a warning "; my $warning="just another warning\n"; warn $warning." "; die "death by request";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://727944]
Approved by clinton
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2016-08-24 07:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The best thing I ever won in a lottery was:















    Results (338 votes). Check out past polls.