Re: A newbie question - line number inside the script




Amit Koren wrote:

I'm a newbie to Perl, (and to this mailing list) :)
There's a task i was given, in which it is necessary to get the
number of the current executing line/command - inside the script itself.

Can someone assist please ?


The value of __LINE__ is the source file line number where it is placed. For
instance

print

__LINE__

;

will output '3'.




The built-in function caller() will return a list of three values - the package
name, file name, and file line number of the place where the current subroutine
was called. So

use strict;
use warnings;

test();

sub test { print ((caller)[2]) };

will output '4'.




The value of $. is the number of records read from the last-accessed file handle. So

use strict;
use warnings;

<DATA> for 1 .. 3;
print $.;

__DATA__
a
b
c
d
e

will output '3'.




I hope one of these solves your problem.

Rob
.



Relevant Pages

  • Re: debugger exiting
    ... strict and warnings pragmas. ... I think portraying Perl as a command-line tool limits it to fewer platforms than ... work only as a Unix shell command line. ...
    (perl.beginners)
  • Re: dns querry script.
    ... use warnings; ... use strict; ... C:\Dload> perl dns.pl ...
    (comp.lang.perl.misc)
  • Re: Any way to access global variable in Perl script from one module file?
    ... use strict; ... use warnings; ... a separate process has a completely separate memory space. ... There is probably no reason to create a new perl ...
    (comp.lang.perl.misc)
  • Re: Debug Help Please
    ... In Perl there is the expression TIMTOWTDI which means that you will probably get different opinions on "The Right Way" to do something in Perl. ... use strict; ... That is a specious argument because you can disable specific strictures or warnings in local scope: ... for all Failures ...
    (perl.beginners)
  • Re: List Variable becomes undefined inexplicably
    ... warnings and strictures very often find bugs in programs. ... It is inconsiderate of your audience to post without warnings and strict. ... There is a Perl and there is a perl. ... chomp $LINE; ...
    (comp.lang.perl.misc)