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

How can I genereate the current line number and program information from within a PERL program? I guess I'm thinking of something like getting a stack trace in java.

Replies are listed 'Best First'.
Re: line number
by leira (Monk) on Mar 01, 2004 at 20:21 UTC
    The Carp module should give you what you need.

    Linda

Re: line number
by mutated (Monk) on Mar 01, 2004 at 20:27 UTC
    __LINE__ is the linenumber in the current source file..ie
    print "current line is " . __LINE__ . "\n";


    daN.
      wow......3 solid responses in like 1 minute. Thanks.
        I just wanted to make a global debug function and incorporate as much possible as I'm writing a lexer/parser and I prefer to generate my own debugging information built into the code. I'm not sure why this was so hard to find but I'm guessing way more people care about knowing where they are in a file they are reading, then in their own code in the context of PERL. I think the __LINE__ should suffice....and I haven't checked into CARP yet but I do vaguely recall stact traces somehow being involved with that.
Re: line number
by duff (Parson) on Mar 01, 2004 at 20:26 UTC
      While caller will give the linenumber, it's not seemingly the best for the OP's needs.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: line number
by blue_cowdawg (Monsignor) on Mar 01, 2004 at 20:27 UTC

        How can I genereate the current line number

    Check it out:

    print "blah\n"; print __LINE__,"\n"; print "blah\n"; print "blah\n"; print "blah\n"; print "blah\n"; print "blah\n"; print __LINE__,"\n";
    when run gives you
    blah 2 blah blah blah blah blah 8

    What other information are you looking for?


    Peter L. Berghold -- Unix Professional
    Peter at Berghold dot Net
       Dog trainer, dog agility exhibitor, brewer of fine Belgian style ales. Happiness is a warm, tired, contented dog curled up at your side and a good Belgian ale in your chalice.
      If your code is split into multiple files, it will also help to use __FILE__ , like this:

      print "Error at line " . __LINE__ . " in file " . __FILE__ . "\n";

      meant to reply here....see above