in reply to Re: How to get die() to stop printing a line number?
in thread How to get die() to stop printing a line number?

Strange. I usually am not interested at all on which line a program gave up and died. I want to know why it died. I generally don't care the program couldn't open a file on line 1273 - but I do care which file it couldn't open, and why.

Abigail

  • Comment on Re: How to get die() to stop printing a line number?

Replies are listed 'Best First'.
Re: Re: How to get die() to stop printing a line number?
by tilly (Archbishop) on Jul 02, 2003 at 03:05 UTC
    For me the question of where it died allows me to go there and start figuring out why it was doing what it did when it died. Therefore while where is in and of itself unimportant, it helps answer what I want answered.
      In the example I gave, a die after a failure of a system call (which is a very common usuage of die), I would not look at the program to solve the problem - because the problem lies elsewhere. So, why bother with the line number? It's irrelevant to both the programmer and the user.

      Abigail

        And in the example you gave, a die after failure of a system call, I would look at the program to solve the problem.

        It usually doesn't take long to verify that the program had no way of succeeding in that particular operation. The question then becomes why it thought it might. Which means that I need to figure out what it was doing, and the line number is useful. In fact it is so useful that I prefer to use Carp's confess to get even more context at my finger tips.

        Furthermore I find automatic line numbers useful when a program has coding problems which I need to sort through to figure out the error. Coding problems like a program that did a chdir somewhere then died in the middle of a run (OK, I have the file name, where is that?) or ones that didn't bother including the name of the file that they were trying to open.

        And finally often after a batch script has terminated abnormally it is necesssary to decide what to do about the mess it left behind. The line number is useful for that because it gives me information about where the program was and therefore (if I know it) knowledge about what it was doing, what it had done, and what should be done about. (Yes, a well-designed program will clean up any mess before it does anything. I have not always had the luxury of using well-designed programs...)

        Now if the information is not useful for you, then you can readily ignore it. But it has been very useful for me, and I appreciate Perl automatically including it.

        Sure there are many instances when the line number is a waste of space, but there are also instances where it's useful.

        For example I often use simple dies for checking for contract violations. Where the contract was violated is just as interesting as what aspect of the contract was violated.

        Line numbers sometimes good, sometimes bad - which was, I guess, your point ;-)

Re: Re: How to get die() to stop printing a line number?
by cfreak (Chaplain) on Jul 02, 2003 at 13:48 UTC

    That's very foriegn to me. Say for instance I have a large program that has multiple files to open and I mistype a filename, either when I declare in some variable somewhere or even if I just used it in the open call itself. So now if I don't have a line number and the filename it spit back out was useless I have to go through the entire program to find where I made such a mistake. Even if its a variable that was declared elsewhere, the line number at least gives me a place to start. I may have forgotten the variable name I used, etc.

    For me an even more common use would be if a SQL query failed. Its extremely easy to have a syntax error in query, and it won't get caught until runtime. Sure I could print out the whole query and look for it, but a line number seems so much easier: fire up 'vim' <line-number> shift-G and there's the error.

    Lobster Aliens Are attacking the world!
      Well, assuming you print the name of the file that failed to open in the error message, a simple grep on the file name would find its declaration immediately - which got to be faster than first going to the line number, and then grepping for the variable name. (vi +/filename/ program.pl)

      Depending on your preference, you might be right about the SQL query (I usually write modules with just a handful of related queries in the same file, which would then be relatively small, so there's hardly any searching needed). But I was arguing against the sentiment one should always have to have the line number in a die message. The fact that there are cases where you might want to have the line numbers doesn't dispute that.

      Abigail

        Well I think we can both agree that it comes down to a matter of personal preference. I didn't mean to imply with my original post that there would never be a reason to not show a line number, rather the original poster seemed to want to just exit his program with no output, which is what exit is for, where as die indicates some kind of error occured.

        Update: Why on earth was this modded down? Its not a flame and I don't believe that it is wrong either.

        Lobster Aliens Are attacking the world!