Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Perl dropping " at line xxx" from warnings when "\n" is present

by bprew (Monk)
on Dec 09, 2005 at 19:04 UTC ( [id://515624]=perlquestion: print w/replies, xml ) Need Help??

bprew has asked for the wisdom of the Perl Monks concerning the following question:

Why does perl omit the "at line xxx" when I supply a "\n" at the end of a warning?
Example:
b3n@sdf:~> cat test.pl #!/usr/bin/perl warn "foo"; warn "bar\n"; b3n@sdf:~> perl test.pl foo at test.pl line 3. bar

This appears to be the same for both perl 5.6.0 (eww) and 5.8.7 (specifically perl, v5.8.7 built for alpha-netbsd-thread-multi)

Is this to allow for exception handling with objects or something similar? I'm assuming this is expected behaviour, but I didn't see any note of it in the warn() perldoc page. I won't lose any sleep over it, but it did strike me as odd.
Thanks
Update: added perl version(s)


--
Ben
"Naked I came from my mother's womb, and naked I will depart."

Replies are listed 'Best First'.
Re: Perl dropping " at line xxx" from warnings when "\n" is present
by friedo (Prior) on Dec 09, 2005 at 19:07 UTC
    It's a feature. die works the same way, e.g.

    perl -e 'die "foo"' perl -e 'die "foo\n"'

    It's useful if you want to report a warning or fatal error to a user as opposed to a programmer, since a user woudldn't care about scary line-numbers and such. It doesn't affect passing exception objects, since if you're passing an object do die you won't be passing a string that could contain "\n".

Re: Perl dropping " at line xxx" from warnings when "\n" is present
by ww (Archbishop) on Dec 09, 2005 at 19:10 UTC
    ummm -- bad man page! down, man page. sit!
    ActiveState has no such omission:

    perldoc -f warn
    and
    perldoc -f die

    As noted above, warn's doc offers a strong indication that one should also read re die... where, in the second corrected third paragraph one finds:

    If the last element of LIST does not end in a newline, the current script line number and input line number (if any) are also printed, and a newline is supplied....

    which clearly implies that there's something different about the behavior when \n is present.
Re: Perl dropping " at line xxx" from warnings when "\n" is present
by ikegami (Patriarch) on Dec 09, 2005 at 19:09 UTC
    It's a simple way of giving the programmer the option of including the line number in the message or not. It is indirectly mentioned ("just like die") in warn's documentation.
Re: Perl dropping " at line xxx" from warnings when "\n" is present
by Fletch (Bishop) on Dec 09, 2005 at 19:12 UTC

    If you look at the docs for die it says:

    If the last element of LIST does not end in a newline, the current script line number and input line number (if any) are also printed, and a newline is supplied.

    And warn says "Produces a message on STDERR just like "die", but doesn't exit or throw an exception." Granted that's not explicitly saying "warn will print the current script line number and input line number unless the message ends with a newline". You might submit a documentation patch to p5p adding something like "(see the documentation for C<die> for more details)."

      Don't I feel sheepish :)

      Thanks for pointing me in the right direction, next time when it says just like 'xxx', I'll got ahead and read the docs for 'xxx'


      --
      Ben
      "Naked I came from my mother's womb, and naked I will depart."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://515624]
Approved by jfroebe
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-25 14:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found