Perl built in exit and print in one command
Asked Answered
C

5

15

I know I can die but that prints out the script name and line number.

I like to do things like die 'error' if $problem;

Is there a way to do that without printing line number stuff?

It would be nice not to have to use braces if($problem){print 'error';exit}

Credible answered 21/2, 2011 at 18:12 Comment(2)
also print prints to STDOUT; die prints to STDERRTyphoon
Oh. I just realized that while that does not matter now, it will matter later. Thanks. @TyphoonCredible
U
12

You could use the fairly natural-sounding:

print "I'm going to exit now!\n" and exit if $condition;

If you have perl 5.10 or above and add e.g. use 5.010; to the top of your script, you can also use say, to avoid having to add the newline yourself:

say "I'm going to exit now!" and exit if $condition;
Untangle answered 21/2, 2011 at 22:20 Comment(2)
Note that in the unlikely event that print fails, the program will not exit. Better to say "print(...), exit if $condition;".Shammer
@Shammer note that the parentheses after print in that example are required, otherwise it will exit without printing anything.Thelma
T
25

Adding a newline to the die error message suppresses the added line number/scriptname verbage:

die "Error\n"
Tarpon answered 21/2, 2011 at 18:16 Comment(0)
H
20

You can append a new line to the die string to prevent perl from adding the line number and file name:

die "oh no!\n" if condition;

Or write a function:

sub bail_out {print @_, "\n"; exit}

bail_out 'oh no!' if condition;

Also keep in mind that die prints to stderr while print defaults to stdout.

Howlan answered 21/2, 2011 at 18:17 Comment(2)
Looks like I will have to write my own function because it will have to write to stdout. (I know that I did not originally state this, but I just realized that the only times I want to avoid the line number is when it prints to stdout).Credible
@George: you can still use {{die}}Ivonneivor
U
12

You could use the fairly natural-sounding:

print "I'm going to exit now!\n" and exit if $condition;

If you have perl 5.10 or above and add e.g. use 5.010; to the top of your script, you can also use say, to avoid having to add the newline yourself:

say "I'm going to exit now!" and exit if $condition;
Untangle answered 21/2, 2011 at 22:20 Comment(2)
Note that in the unlikely event that print fails, the program will not exit. Better to say "print(...), exit if $condition;".Shammer
@Shammer note that the parentheses after print in that example are required, otherwise it will exit without printing anything.Thelma
I
0

Here is an answer to the question you completed in you comment to Eric.

To do both (print STDOUT and print without line number) you can still use die by changing the __DIE__ handler:

$SIG{__DIE__} = sub { print @_, "\n"; exit 255 };

die "error" if $problem;
Ivonneivor answered 24/2, 2011 at 22:2 Comment(0)
G
-5

You can create complex messages with sprintf:

die sprintf( ... ) if $problem;
Gametocyte answered 21/2, 2011 at 18:44 Comment(1)
You may want to click the [delete] link on this answer.Credible

© 2022 - 2024 — McMap. All rights reserved.