Terminating a sub?
am 07.12.2005 12:30:42 von kroesjnov
Is there a (builtin) way to terminate a sub in a perlscript?
Something like the code below, the objective being that 'function' cannot
print it is alive under specific conditions:
#start
#!/usr/bin/perl
my $kill = TRUE;
sub function
{
&kill();
print((caller (0))[3]." is alive\n");
}
sub kill
{
if($kill)
{
#terminate/kill calling function code
print("killed ".(caller (1))[3]."\n");
}
}
&function();
#end
I know I can check these conditions in the sub's themself and use 'return',
but I deem it better coding to group repeating code in it's own function and
call that function.
Re: Terminating a sub?
am 07.12.2005 13:18:29 von Gunnar Hjalmarsson
kroesjnov wrote:
> Is there a (builtin) way to terminate a sub in a perlscript?
> Something like the code below, the objective being that 'function' cannot
> print it is alive under specific conditions:
>
> #start
> #!/usr/bin/perl
>
> my $kill = TRUE;
>
> sub function
> {
> &kill();
> print((caller (0))[3]." is alive\n");
> }
>
> sub kill
> {
> if($kill)
> {
> #terminate/kill calling function code
> print("killed ".(caller (1))[3]."\n");
> }
> }
>
> &function();
> #end
>
> I know I can check these conditions in the sub's themself and use 'return',
> but I deem it better coding to group repeating code in it's own function and
> call that function.
Can't you let kill() return something, and do:
return if kill();
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Re: Terminating a sub?
am 07.12.2005 13:59:22 von kroesjnov
"Gunnar Hjalmarsson" schreef in bericht
news:3vo28oF16lperU1@individual.net...
> Can't you let kill() return something, and do:
>
> return if kill();
Yeah, I guess that is the closed you can get to what I want; You still run
all the checks in the seperate function.
Sometimes you overlook the simple solutions :)
Thx!