exit(), die, ??

T

Tony McGuire

New to PERL.

Can you exit from the middle of a script that is handling incoming
data based on IF statements?

I have a simple script for processing incoming uploads. (Thanks,
Tore!)

Based on the input, I'd like to be able to return info back to the
user.
(I have the info on retrieving the input values)

input field: $uniqueid
input field: $filename

if (length $uniqueid<1) {
# build HTML and return to user
}

if (length $filename<1) {
# build HTML and return to user
}

# process everything, since we have the info we need.

So, if certain conditions aren't met, I'd like to bail and return info
back to the user. I realize you can do this with nested if..else
statements; I am hoping I can halt processing and return to the user
if individual 'if' statements resolve to false.

Is this possible in PERL? And if so, can anyone point me to the
methodology I would need?
 
P

Paul Lalli

Tony McGuire said:
New to PERL.

First lesson: The language is Perl. The interpeter is perl. Never ever PERL.
(see also: perldoc -q difference )
Can you exit from the middle of a script that is handling incoming
data based on IF statements?

There's no such thing as an IF statement. Case matters.
I have a simple script for processing incoming uploads. (Thanks,
Tore!)

Based on the input, I'd like to be able to return info back to the
user.
(I have the info on retrieving the input values)
So, if certain conditions aren't met, I'd like to bail and return info
back to the user. I realize you can do this with nested if..else
statements; I am hoping I can halt processing and return to the user
if individual 'if' statements resolve to false.

Is this possible in PERL? And if so, can anyone point me to the
methodology I would need?

You should always make an attempt before asking if it's possible. You clearly
already know that the way to end a script is with either exit() or die() (based
on the subject of your post). So what happened when you tried putting exit() or
die() inside an if block?

Paul Lalli
 
T

Tony McGuire

Paul Lalli said:
You should always make an attempt before asking if it's possible. You clearly
already know that the way to end a script is with either exit() or die() (based
on the subject of your post). So what happened when you tried putting exit() or
die() inside an if block?

Paul Lalli

I tried 'die' several times based on example code I found.

I always got 'premature end of script headers' as a error. Looking at
the logs the error pointed to the 'die' section.

I came to the conclusion that the examples were incomplete, and so I
needed to ask about 'die'.

However, if 'exit(1)' will return the contents of previous 'print'
statements, it would be much preferred.

I'll give both another go.
 
T

Tony McGuire

Paul Lalli said:
You should always make an attempt before asking if it's possible. You clearly
already know that the way to end a script is with either exit() or die() (based
on the subject of your post). So what happened when you tried putting exit() or
die() inside an if block?

Paul Lalli

I went back and set up a print statement followed by exit(1);

And it worked exactly as I hoped. Haven't tried die again, but I
assume I had some other error in those previous attempts. I should
have tested exit, yes, but nothing I read indicated you could use it
in the main script to stop and return to the user.

Thanks, Paul. At least your post indicated that it should work. :)
 
J

Joe Smith

Tony said:
I tried 'die' several times based on example code I found.
I always got 'premature end of script headers' as a error. Looking at
the logs the error pointed to the 'die' section.

That's what you get for outputting text that does not conform to CGI
requirements. You need to fix that. The "Content-type:" header is required.

use CGI::Carp qw(fatalsToBrowser);
die "I give up." if $something == $not_right;

-Joe
 
B

Brian McCauley

Sara said:
"die" IS a premature end to the script so that's normal message.

Don't worry about how you capitalize "PERL", most of us can parse your
spelling with /i and understand what you mean.

My spelling is very poor. I know most people can read what I write
nonetheless. This does not mean I should not attempt to spell correctly.
Capitalization is one of those cult things.
s/cult/culture/

As for the execution path in your code, Perl has some very nice
capabilities to exit programs, subroutines, and loops asynchronously.

You do not mean 'asynchronously'.
If you're used to c or many other languages,

Er, isn't it 'C' not 'c'? :)
sub ILovePerl
{local $_ = $_[0];

Never use local($_) - I used to do it all the time before I discovered
how dangerous it was. Always use for() to localize $_ or use local(*_)
(after, of course, you've got what you need from @_).

Note using explicit subscripts on @_ is affected. Use a list assignment
or shift.
return "lucky you!" if /ACE/; # go back to calling routine NOW
return "ohoh!" if /DUECE/; # go back to calling routine NOW
"soso!"; # default return value, the last lvalue is returned by
default
}

You do not mean 'lvalue'.
 
P

Paul Lalli

Sara said:
"die" IS a premature end to the script so that's normal message.

No, it's not. "Premature end of script headers" is the message a server
will output when the headers not sent correctly. It has nothing at all
to do with die(). Die simply exits the currently running script with
the current value of $!, and prints its parameters to STDERR. A test:

#!/usr/bin/env perl
use strict;
use warnings;
use CGI qw/:standard/;
print header();
print start_html('Test');
print h1('Here I am!');
die("Exiting...");
print end_html();

This program will echo the beginning HTML to the browser, as well as
'<h1>Here I am</h1>'. The program will then end, with the ending HTML
not printed. (Most browsers will render what was printed regardless of
the incorrect HTML they received). The server logs will show the
message "Exiting... at temp.cgi line 8."
Don't worry about how you capitalize "PERL", most of us can parse your
spelling with /i and understand what you mean. Capitalization is one
of those cult things.

YES, AND mOST pEOPLe wiLL UNDersTAND thIS TOO, but that doesn't mean
it's okay to type like that. Perhaps Sara should read
perldoc -q difference
as well.


Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,884
Members
47,419
Latest member
ArturoBres

Latest Threads

Top