Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Executing line number

by Anonymous Monk
on Sep 09, 2009 at 14:18 UTC ( [id://794362]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi , How do I display the line of the perl script thats being executed... THanks

Replies are listed 'Best First'.
Re: Executing line number
by leocharre (Priest) on Sep 09, 2009 at 14:31 UTC
    • You can use warn...
      $i++ and warn;
      To see more about warn.. use perldoc -f warn

      Note that if you pass arguments to warn, and it ends with a newline char, it won't show line number.

    • Or using __LINE__ ...
      $i++ and printf "this is line %s\n", __LINE__; # or.. $i++ and print "this is line ". __LINE__ . "\n";
    • You could also use caller (perldoc -f caller).
      $i++ and line(); sub line { printf STDERR "now executing line %s\n", (caller)[2] }
    • Or use the perl debugger

    What I would be asking here though.. is why?

    I find that more important than telling myself what line I am executing is telling myself *what* is happening.

    Perl is pretty freaking nice about telling you where things went ape- unless the code is all messed up. You know, whenever you use die().. You get told what program and where... This is way cool. Usually unless you tell perl to lie, or to give stupid output, it's very nice about finding what's up. This as opposed to stuff like php, which is the devil.

    If you're using this because you have a large program that may screw up in many places... Use Carp.. Especially Carp::cluck().. which doesn't kill your program.. but tells you what happened all the way back... (man Carp)

Re: Executing line number
by ELISHEVA (Prior) on Sep 09, 2009 at 14:33 UTC

    One possibility:

    print STDERR 'file: ' . __FILE__ . ', line: ' . __LINE__ . "\n";

    In case you are wondering about all the concatenation, you can't use __FILE__ and __LINE__ inside double quotes. __FILE__ and __LINE are "special literals" and they lose their specialness inside double quotes. To learn more about __FILE__ and __LINE, look at perldata and search for the phrase "Special Literals".

    Best, beth

      I think printf() is more clear in such a situation :)
      printf STDERR "file: %s, line: %s\n", __FILE__, __LINE__;

      You can use the babycart operator to interpolate bits of code into a double-quoted string, not that it saves much, if any, typing or looks any clearer.

      warn "file: @{ [ __FILE__ ] } line: @{ [ __LINE__ ] } - oops\n";

      I hope this is of interest.

      Cheers,

      JohnGG

        I think that anyone using this idiom must be punished :p
Re: Executing line number
by almut (Canon) on Sep 09, 2009 at 15:52 UTC

    Your question is a little ambiguous... but maybe you're looking for a non-interactive tracing facility, as provided by the Perl debugger. E.g.

    $ cat 794362.pl #!/usr/bin/perl use strict; use warnings; print "Hello world!\n"; $ PERLDB_OPTS="AutoTrace NonStop frame=2" perl -d 794362.pl entering CODE(0x65f740) 3: use strict; 3: use strict; 3: use strict; entering strict::import 28: shift; 29: $^H |= @_ ? bits(@_) : $default_bits; exited strict::import exited CODE(0x65f740) entering CODE(0x65f780) 4: use warnings; 4: use warnings; 4: use warnings; entering warnings::import 336: shift; 338: my $catmask ; 339: my $fatal = 0 ; 340: my $no_fatal = 0 ; 342: my $mask = ${^WARNING_BITS} ; 344: if (vec($mask, $Offsets{'all'}, 1)) { 349: push @_, 'all' unless @_; 351: foreach my $word ( @_ ) { 352: if ($word eq 'FATAL') { 361: $mask |= $catmask ; 362: $mask |= $DeadBits{$word} if $fatal ; 363: $mask &= ~($DeadBits{$word}|$All) if $no_fatal ; 369: ${^WARNING_BITS} = $mask ; exited warnings::import exited CODE(0x65f780) Package 794362.pl. 6: print "Hello world!\n"; Hello world!

    (See perldebug for what other options you might want to set via the env variable PERLDB_OPTS)

    P.S.: Don't ask (me) why "3: use strict;" / "4: use warnings;" are being printed three times. I don't know... — but maybe someone else does :)

Re: Executing line number
by Fletch (Bishop) on Sep 09, 2009 at 14:34 UTC

    Devel::Trace perhaps? Or the special __LINE__ token (see perldata)?

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Executing line number
by ikegami (Patriarch) on Sep 09, 2009 at 14:36 UTC
    caller can get you the line number where the current subroutine was called. Carp might also be of interest.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-26 04:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found