Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the __LINE__ for calling function?

Tags:

perl

Basically, my question is this except for perl rather than PHP.

I know warn() manages it, but then again warn() is core perl, so I'd understand if it weren't generally possible.

Addendum (in case that link fails eventually)

have a function

sub logm
{
  my ($msg, $line_no) = @_;
  # ... 
}

I would like to include __LINE__ (and __FILE__, but that's not necessary), but don't want to include it as a parameter each time as I currently do.

# This is attrocious
logm "That file handle is now closed", __LINE__;
like image 454
Parthian Shot Avatar asked Jul 03 '14 13:07

Parthian Shot


People also ask

How to get the line number of a program in C?

| > In C#, you can use System.Diagnostics.StackTrace class to get the code's | > line number, function name, filename and other information. | > Hope this helps. rights. | > C++?

How to write function call as a parameter to a function?

We can also write function call as a parameter to function. In the below code, first add (num1, num2) is evaluated, let the result of this be r1. The add (r1, num3) is evaluated. Let the result of this be r2. Finally add (r2, num4) is evaluated and its result is printed.

What is the use of the call() method?

The call () method is a predefined JavaScript method. It can be used to invoke (call) a method with an owner object as an argument (parameter). With call (), an object can use a method belonging to another object. This example calls the fullName method of person, using it on person1:

How do you call a method in JavaScript?

The JavaScript call () Method The call () method is a predefined JavaScript method. It can be used to invoke (call) a method with an owner object as an argument (parameter). With call (), an object can use a method belonging to another object.


Video Answer


1 Answers

Check caller function,

sub logm {
  my ($msg) = @_;
  my ($package, $filename, $line) = caller;

  print "'$msg' from file:$filename; line:$line\n";
}

logm("message");
like image 88
mpapec Avatar answered Oct 01 '22 20:10

mpapec