Calling Exit from within a subroutine

------=_NextPart_000_0001_01CB2889.4F9422D0
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit

I call several subroutines within my program. The program is design in a
way that I don't expect my subroutines to return to the caller but to exit
once they have executed.

At the moment I don't have exit or return at the end of my subroutines.



mysub(); # will the sub return here? If yes what happen if I don't capture
it and act?

.....

......

....

reset of the code



sub mysub{

do this

don that

...

# I don't call exit or return

}





I understand a sub will normally implicitly return the last thing it
evaluation, but what happens if I don't act on what has been returned to the
caller? Will be programme stall because it doesn't know what to do next or
will it exit?



Mimi


------=_NextPart_000_0001_01CB2889.4F9422D0--
Babale Fongo [ Mi, 21 Juli 2010 05:00 ] [ ID #2044887 ]

Re: Calling Exit from within a subroutine

--001485f1ea76082d00048be248d3
Content-Type: text/plain; charset=UTF-8

On Wed, Jul 21, 2010 at 5:00 AM, Babale Fongo <bfongo [at] googlemail.com> wrote:

> I call several subroutines within my program. The program is design in a
> way that I don't expect my subroutines to return to the caller but to exit
> once they have executed.
>
> At the moment I don't have exit or return at the end of my subroutines.
>
>
>
> mysub(); # will the sub return here? If yes what happen if I don't capture
> it and act?
>
> ....
>
> .....
>
> ...
>
> reset of the code
>
>
>
> sub mysub{
>
> do this
>
> don that
>
> ...
>
> # I don't call exit or return
>
> }
>
>
>
>
>
> I understand a sub will normally implicitly return the last thing it
> evaluation, but what happens if I don't act on what has been returned to
> the
> caller? Will be programme stall because it doesn't know what to do next or
> will it exit?
>
>
>
> Mimi
>
> Hi Mimi,

You program will happily continue doing what you want it doens't care that
you don't deal with the return value.

So if you have code like this:

do some stuff;
do some more;
call_a_sub();
do some other stuff;


Your program will execute the first two lines, then do what ever the sub
call_a_sub() wants it to do and return to do some other stuff without you
having to worry about a return value. Keep in mind though that $_ has likely
be altered by you sub. ;-)

There always "good" reasons not to bother with return values but in most
cases I would still say it is a sign of sloppy programming.
For the simple reason that you expect a sub to do something what ever it is.
If this something fails you should, even if your code doesn't care about the
result, inform the end user of this fact... you would not be the first
programmer that writes a program that seems to totally at random do this one
thing or fail to do this without any apparent reason... a simple: Failed to
execute something message written to STDERR if nothing else is just one of
those things that makes the end users life a million times easier.

Rob

--001485f1ea76082d00048be248d3--
Rob Coops [ Mi, 21 Juli 2010 11:17 ] [ ID #2044888 ]

RE: Calling Exit from within a subroutine

Hi Rob

I take care of everything that needs (execution errors) to be done =
within my sub. Perhaps this may help explain what I have:

A the beginning of my program I test to see what needs to be done, and =
if a conditional evaluates to true, then a sub is called to work and =
that is it. The program should not continue once a sub ends executing.


If ($foo){
my_sub_foo();
# Is the program going to continue from here once sub foo has ended =
and I have not called exit here or in the sub?
}
elsif ($bar){
my_sub_bar();
}
else {

my_other_sub();
}

sub foo {
do everything that needs to be done.
# I expect the program to end here once the sub has completed =
executing.
}

sub bar {
do everything that needs to be done.
# I expect the program to end here once the sub has completed =
executing.
}

Sub my_other_sub {
...............
...............
# I expect the program to end here once the sub has completed =
executing.
}

As seen above, if a condition is met, then a sub is called to deal with =
it and the program should exit. My question is: what happens if I don't =
call exit in the last line of the sub or after calling the sub? Is the =
program going to continue running once the sub has ended?

If my program will continue to evaluate the rest of the program, then is =
it efficient to call exit one time at the end of the program or call =
exit as the last line of each subroutine to ensure the program ends =
there?

Mimi


=3D> You program will happily continue doing what you want it doens't =
care
=3D> that
=3D> you don't deal with the return value.
=3D>
=3D> So if you have code like this:
=3D>
=3D> do some stuff;
=3D> do some more;
=3D> call_a_sub();
=3D> do some other stuff;
=3D>
=3D>
=3D> Your program will execute the first two lines, then do what ever =
the
=3D> sub
=3D> call_a_sub() wants it to do and return to do some other stuff =
without
=3D> you
=3D> having to worry about a return value. Keep in mind though that $_ =
has
=3D> likely
=3D> be altered by you sub. ;-)
=3D>
=3D> There always "good" reasons not to bother with return values but in
=3D> most
=3D> cases I would still say it is a sign of sloppy programming.
=3D> For the simple reason that you expect a sub to do something what =
ever
=3D> it is.
=3D> If this something fails you should, even if your code doesn't care
=3D> about the
=3D> result, inform the end user of this fact... you would not be the =
first
=3D> programmer that writes a program that seems to totally at random do
=3D> this one
=3D> thing or fail to do this without any apparent reason... a simple:
=3D> Failed to
=3D> execute something message written to STDERR if nothing else is just
=3D> one of
=3D> those things that makes the end users life a million times easier.
=3D>
=3D> Rob


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Mimi Cafe [ Mi, 21 Juli 2010 13:18 ] [ ID #2044889 ]

Re: Calling Exit from within a subroutine

--000e0cd4b30a5fc9dc048be4bc8a
Content-Type: text/plain; charset=ISO-8859-1

On Wed, Jul 21, 2010 at 6:18 AM, Mimi Cafe <mimicafe [at] googlemail.com> wrote:

> If my program will continue to evaluate the rest of the program, then is it
> efficient to call exit one time at the end of the program or call exit as
> the last line of each subroutine to ensure the program ends there?
>

"Efficient" doesn't offer a lot of benefit at this point in the program. The
difference is fractions of a microsecond. "Readable" will help you more in
the long term.

My personal rule of thumb says that exiting belongs in the main application.
Ending the application is not integral to the actions "foo" or "bar". I
assume that from the description - you don't care which subroutine runs, the
program should exit after any of them. So I would exit from the main
program. Maybe something like this...

If ($foo){
my_sub_foo();
# Is the program going to continue from here once sub foo has ended and
I have not called exit here or in the sub?
}
elsif ($bar){
my_sub_bar();
}
else {
my_other_sub();
}
exit 0;

--
Robert Wohlfarth

--000e0cd4b30a5fc9dc048be4bc8a--
Robert Wohlfarth [ Mi, 21 Juli 2010 14:13 ] [ ID #2044890 ]

Re: Calling Exit from within a subroutine

On Wed, Jul 21, 2010 at 07:18, Mimi Cafe <mimicafe [at] googlemail.com> wrote:
> Hi Rob
>
> I take care of everything that needs (execution errors) to be done within=
my sub. Perhaps this may help explain what I have:
>
> A the beginning of my program I test to see what needs to be done, and if=
a conditional evaluates to true, then a sub is called to work and that is =
it. The program should not continue once a sub ends executing.
>
>
> If ($foo){
> =C2=A0 my_sub_foo();
> =C2=A0 # Is the program going to continue from here once sub foo has ende=
d and I have not called exit here or in the sub?
> }
> elsif ($bar){
> =C2=A0 =C2=A0my_sub_bar();
> }
> else {
>
> =C2=A0 my_other_sub();
> }
>
> sub foo {
> =C2=A0 =C2=A0do everything that needs to be done.
> =C2=A0 =C2=A0# I expect the program to end here once the sub has complete=
d executing.
> }
>
> sub bar {
> =C2=A0 =C2=A0do everything that needs to be done.
> =C2=A0 =C2=A0# I expect the program to end here once the sub has complete=
d executing.
> }
>
> Sub my_other_sub {
> =C2=A0 ...............
> =C2=A0 ...............
> =C2=A0 # I expect the program to end here once the sub has completed exec=
uting.
> }
>
> As seen above, if a condition is met, then a sub is called to deal with i=
t and the program should exit. My question is: what happens if I don't call=
exit in the last line of the sub or after calling the sub? Is the program =
going to continue running once the sub has ended?
>
> If my program will continue to evaluate the rest of the program, then is =
it efficient to call exit one time at the end of the program or call exit a=
s the last line of each subroutine to ensure the program ends there?
snip

Yes, the program will run any code after your if/elsif/else statement:

#!/usr/bin/perl

use strict;
use warnings;

foo();

sub foo {
print "foo\n";
}

print "bar\n";

But since you don't have any directly executable code after the if
statement (subroutine definitions don't execute), you shouldn't have a
problem. A return or an exit at the end of what you consider to be
your code is safer and is a good marker for other readers that the
this is where the program ends. Because you are using the style of
code where functions follow the main part of the code, it will also
prevents stray code from executing:

#!/usr/bin/perl

use strict;
use warnings;

foo();
exit; #the code will no longer print "bar\n"

sub foo {
print "foo\n";
}

print "bar\n";


--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
chas.owens [ Mi, 21 Juli 2010 14:27 ] [ ID #2044891 ]

RE: Calling Exit from within a subroutine

Cool, I have changed my program to exit at the end of the main program code.

Thanks

=> -----Original Message-----
=> From: Robert Wohlfarth [mailto:rbwohlfarth [at] gmail.com]
=> Sent: 21 July 2010 13:14
=> To: Perl Beginners
=> Subject: Re: Calling Exit from within a subroutine
=>
=> On Wed, Jul 21, 2010 at 6:18 AM, Mimi Cafe <mimicafe [at] googlemail.com>
=> wrote:
=>
=> > If my program will continue to evaluate the rest of the program,
=> then is it
=> > efficient to call exit one time at the end of the program or call
=> exit as
=> > the last line of each subroutine to ensure the program ends there?
=> >
=>
=> "Efficient" doesn't offer a lot of benefit at this point in the
=> program. The
=> difference is fractions of a microsecond. "Readable" will help you
=> more in
=> the long term.
=>
=> My personal rule of thumb says that exiting belongs in the main
=> application.
=> Ending the application is not integral to the actions "foo" or "bar".
=> I
=> assume that from the description - you don't care which subroutine
=> runs, the
=> program should exit after any of them. So I would exit from the main
=> program. Maybe something like this...
=>
=> If ($foo){
=> my_sub_foo();
=> # Is the program going to continue from here once sub foo has
=> ended and
=> I have not called exit here or in the sub?
=> }
=> elsif ($bar){
=> my_sub_bar();
=> }
=> else {
=> my_other_sub();
=> }
=> exit 0;
=>
=> --
=> Robert Wohlfarth


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Mimi Cafe [ Mi, 21 Juli 2010 19:00 ] [ ID #2044896 ]
Perl » gmane.comp.lang.perl.beginners » Calling Exit from within a subroutine

Vorheriges Thema: install patch to module Text::LevenshteinXS
Nächstes Thema: DBI under SOAP/mod_perl