0

warn behaves differently in the following two cases:

#! /usr/bin/perl
use warnings;
use strict;
use Data::Dumper;

warn "string";
warn Dumper("string");

The first prints:

string at dumper.pl line 6.

And the second prints just:

$VAR1 = 'string';

without any line number.

How get the line number when warning with the Dumper result?

ceving
  • 21,900
  • 13
  • 104
  • 178

3 Answers3

5

The reason for the difference is because the string ends with a new line.

warn "test";
warn "test\n";

Output from Dumper includes a linefeed, so concat anything on the end will do it.

Or just explicitly reference __LINE__:

warn Dumper ("error") . "at line:" .__LINE__."\n";

(See perldoc warn)

Sobrique
  • 52,974
  • 7
  • 60
  • 101
3

Just connect a string after the Dumper call:

warn Dumper("string").' ';

yields

$VAR1 = 'string';
  at /tmp/execpad-a668561a2ac4/source-a668561a2ac4 line 7.

at eval.in.

syck
  • 2,984
  • 1
  • 13
  • 23
2

See the documentation for the warn function:

$ perldoc -f warn

warn LIST
        Prints the value of LIST to STDERR. If the last element of LIST
        does not end in a newline, it appends the same file/line number
        text as "die" does.

        (... and much more information that is worth reading ...)

In your case, the output of Dumper() ends with a newline, so the file/line number is not printed.

Andy Lester
  • 91,102
  • 13
  • 100
  • 152