Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl built in exit and print in one command

Tags:

exit

die

perl

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}

like image 940
700 Software Avatar asked Feb 21 '11 18:02

700 Software


People also ask

How do I print on the same line in Perl?

For example, your code can be written like this: use Time::Piece; $todaydate = localtime->strftime('%Y-%m-%d-%H%M%S'); $output_file = "my_data_$todaydate. csv"; print "SQL query output file name : $output_file\n"; Time::Piece has been included with all versions of Perl since 2007.

How do I exit a command in Perl?

exit() function evaluates the expression passed to it and exits from the Perl interpreter while returning the value as the exit value. The exit() function does not always exit immediately but calls the end routines before it terminates the program.

How do I print output in Perl?

print() operator – print operator in Perl is used to print the values of the expressions in a List passed to it as an argument. Print operator prints whatever is passed to it as an argument whether it be a string, a number, a variable or anything. Double-quotes(“”) is used as a delimiter to this operator.

How do I use echo in Perl?

So the echo is running inside of a shell and in this case it just returns the one shell variable. A cleaner way to get this in Perl is to use %ENV like so: my $jobID = $ENV{'JOBID'}; This also removes the need for chomp , avoids creating an extra process, and is much more efficient.


1 Answers

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

die "Error\n"
like image 108
runrig Avatar answered Sep 21 '22 14:09

runrig