5

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__;
Community
  • 1
  • 1
Parthian Shot
  • 1,390
  • 1
  • 13
  • 24
  • If you want to log structured information, use log4perl. – Jens Jul 03 '14 at 14:01
  • @Jens I do, and that is helpful, but in this particular case the program was already mostly written and I just wanted to shim in some basic stacktracing so the poor sod who has to maintain the thing doesn't hate me. And since that poor sod is, for the moment, me, I thought it well worth the effort. – Parthian Shot Jul 18 '14 at 20:07

1 Answers1

8

Check caller function,

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

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

logm("message");
mpapec
  • 50,217
  • 8
  • 67
  • 127