For Programmers: Free Programming Magazines  


Home > Archive > PERL Programming > December 2005 > Terminating a sub?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Terminating a sub?
kroesjnov

2005-12-07, 7:57 am

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.


Gunnar Hjalmarsson

2005-12-07, 7:57 am

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
kroesjnov

2005-12-07, 7:57 am

"Gunnar Hjalmarsson" <noreply@gunnar.cc> 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!


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com