Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: caller() returns wrong line on multi-line function call

by GrandFather (Sage)
on Sep 13, 2017 at 22:23 UTC ( #1199355=note: print w/replies, xml ) Need Help??


in reply to caller() returns wrong line on multi-line function call

Which is "the line where the function call actually happens"? Is it the line containing the function name, or the line containing the closing ), or the line half way between, or some other line? You could make up pretty good stories for almost any of those lines, but I can't see any being The One True Line.

In the most common context where it's likely to be interesting - debugging the code, any of the lines should be sufficient so long as indentation doesn't disguise the fact that the call spans multiple lines. Have you a context where one of the lines is more important for some reason? If so, tell us about that and maybe we can help solve the problem of determining the one true line from the dross for you.

Premature optimization is the root of all job security
  • Comment on Re: caller() returns wrong line on multi-line function call

Replies are listed 'Best First'.
Re^2: caller() returns wrong line on multi-line function call (refactoring)
by LanX (Bishop) on Sep 14, 2017 at 13:19 UTC
    Any attempt to use life introspection for refactoring of function names would be sabotaged by this bug, since the op-tree is flawed.

    :-[

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

Re^2: caller() returns wrong line on multi-line function call
by jh (Sexton) on Sep 14, 2017 at 14:36 UTC

    I would argue that the One True Line—for debugging and other purposes—is the line that contains the function name. That said, if caller() reported some other line (e.g. the line with the open paren or close paren) consistently, then it would be OK, I guess, but the inconsistency seems like a Problem.

      No need to argue, the one true line is by definition where a statement starts.

      This is for sure a bug and it's not caller's fault.

      Probably also affecting other statements not only function calls.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

        the one true line is by definition where a statement starts.

        How can that possibly be right? By definition you don't have a call until the close bracket for the argument list because you don't have enough information to make the call until then!

        Like I said already, good stories could be made for almost any line. :-)

        Premature optimization is the root of all job security
Re^2: caller() returns wrong line on multi-line function call
by jh (Sexton) on Sep 14, 2017 at 14:40 UTC

    I was writing a script to generate a Perl module full of regexes, and I wanted to include in the output any comments in the script adjacent to each function call to generate a regex in the output... I ended up going a different way because I decided that solution was too Clever.

      I wanted to include in the output any comments in the script adjacent to each function call to generate a regex in the output

      Seems a reasonable thing to me.

      My approach would be to change the script so those comments are string literal instead of actual comments. Then the text is readily available to your generator script to include in the generated module.

Re^2: caller() returns wrong line on multi-line function call
by RonW (Vicar) on Sep 15, 2017 at 18:38 UTC

    I would say the "true line" is either the line with the function name or the line with the closing ) (or the last argument, in the case of a function call without parenthesis).

    The reason I say this is because the arguments to a function call may be any expression that can be used on the right-hand side of an assignment, including another function call.

      The information in nextstate is used by the debugger to display the current line while stepping thru

      • hence it has to be the start of the statement,
      • hence the line with the sub statement in our case
      • not the "function name", since it could be in another line

      Sorry, I don't think there is any room left to argue.

      edit
      The debugger can't work properly with this bug, and actually fails.

      If you type v you'll see something like

      DB<1> n main::tst(caller_subline.pl:24): do { A +=> 1, B => main::tst(caller_subline.pl:25): ); DB<1> v 21 22 23 print_calling_line( __LINE__, 24==> do { A => 1, B => 2, C => 3 } 25 ); 26 27 print_calling_line( __LINE__, 28: do { A => 1, B => 2, C => 3 }, 29: do { A => 1, B => 2, C => 3 , 30: do { A => 1, B => 2, C => 3 } DB<1> n Called from line 23; caller() reports line 24 main::tst(caller_subline.pl:29): do { A +=> 1, B =>

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

        hence the line with the sub statement in our case

        In the OP's case, yes.

        I was talking about function calls in general. And for the case of displaying line numbers to a sentient being (which the OP did mention).

        For a debugger, yes, the line of number of start of the function call is needed to be able to display the function call correctly.

        Update: Though, for a GUI debugger, highlighting the line with the closing ) as long as there wasn't another function call on the same line.

        Certainly, the least ambiguous is to have the line number with the start of the call (either the function name or the sub).

        My main point of my previous post still remains: Anything line "in the middle" of the call is too ambiguous, therefore not acceptable.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1199355]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2017-11-25 06:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (355 votes). Check out past polls.

    Notices?