Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple DIE question

Status
Not open for further replies.

massctrl

MIS
Dec 29, 2002
46
BE
Hi

In the beginning of my script there is :

if ($#ARGV<1) {die print &quot;give arguments};

And if i give no arguments when running the script it says:

***
give arguments

1 at test.pl line 5
***

now i understand where the &quot;1 at test.pl line 5&quot; comes from but i want to get rid of such error messages and only use mine,..

how to do that ?

greets and tnx
 
just leave out the [tt]print[/tt]:
[tt]if ($#ARGV<1) {die &quot;give arguments&quot;};[/tt]
or the perlish one line conditional is reverse order:
[tt]die &quot;give arguments&quot; if ($#ARGV<1);[/tt] ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Call die with a list and it'll be printed to STDERR, you don't need to call print as well. Also, when printing the list for die, if you end it with a newline then the Perl interpreter won't add anything, but if you omit the newline the interpreter will tack on the file, line, etc... Consider the two following examples:
Code:
#!usr/bin/perl -w

die &quot;give arguments&quot; if scalar @ARGV < 1;
This prints something along the lines of

&quot;give arguments at test.pl line 3.&quot;

However, this script
Code:
#!usr/bin/perl -w

die &quot;give arguments\n&quot; if scalar @ARGV < 1;
simply prints

&quot;give arguments&quot;
 
Code:
die &quot;give arguments\n&quot; if scalar @ARGV < 1;
                          ^^^^^^
The above 'scalar' is unnecessary. The boolean context of the comparison (< 1) puts @array in scalar context already. In fact, just being in the test expression of the if statment puts @array in boolean (and therefore scalar) context. So the statement could be written
Code:
die &quot;give arguments\n&quot; unless @ARGV;
(just to be pedantic [smarty])

jaa

 
>The above 'scalar' is unnecessary.
True, but I like to be explicit. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top