LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-01-2007, 03:16 AM   #1
alix123
Member
 
Registered: Nov 2004
Posts: 63

Rep: Reputation: 15
Question In perl is there a way when i call exit function to execute a subroutine


In perl is there a way when i call exit function to execute a subroutine.

The problem is i have a perl file + with custom perl modules and Iam exiting at some places in the custom perl modules when it finds some bad values. is there way when iam calling 'exit' to always call a another subroutine.

Take a look at the following ,basically I want to call onexit function in onexit.pm, whenever there is an exit from any of the modules or from the main file.

###a.pl

use module1.pm
use module2.pm

&module1::funct1()

&module2::funct2()


######################
#module1.pm

sub funct1() {

if($badvalues) {
exit;

}
}

#######################

#module2.pm

sub funct2() {

if($badvalues) {
exit;

}
}
#################################


###onexit.pm

sub onexit()
{
###do some jobs if there is a exit...from module 1 and module 2 and #a.pl.

}

Last edited by alix123; 06-01-2007 at 03:19 AM.
 
Old 06-01-2007, 03:34 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

Although this is not what you ask, but it does fit your example: Why not call the 'onexit' subroutine and exit from there?

Taking your example, this would look like this:

Code:
######################
#module1.pm 
sub funct1() {
  if($badvalues) {
    onexit;
  }
}

#######################
#module2.pm 
sub funct2() {
  if($badvalues) {
    onexit;
  }
}
#################################
###onexit.pm 
sub onexit() 
{
  ###do some jobs if there is a exit...from module 1 and module 2 and #a.pl.
  exit
}
Hope this helps.
 
Old 06-01-2007, 03:34 AM   #3
flupke
Member
 
Registered: Jun 2005
Location: BE
Posts: 30

Rep: Reputation: 15
I don't see a way to do exactly what you want (I suppose some perl guru will give you a better answer than mine)
Meanwhile, maybe you could make it work in another way :

IF you have control on the modules, instead of exiting you send your own process a signal (would it be SIGUSR1 or SIGTERM or something else) and you trap this signal in your main file.

You can see how to catch signals at the very beginning of the man perlipc.


HTH
Flupke
 
Old 06-01-2007, 05:38 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
simple, put this at the end of your main script...

Code:
END { 
      print "Clean up\n" 
      # clean up code here
};
you can put END in the modules as well, they will be executed in
reverse order of definition, so you must put main END before your use
clause as the use is parsed before anything else.

Last edited by bigearsbilly; 06-01-2007 at 05:42 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
perl subroutine call shifty_eyes Programming 2 10-01-2005 08:43 PM
perl: what does the hyphen in function call params mean? johnMG Programming 2 08-19-2005 04:17 PM
Perl undefined subroutine /can't locate object method cobien Programming 1 11-17-2003 10:47 AM
Need Perl/CGI help! Undefined Subroutine CragStar Programming 4 03-05-2002 07:28 AM
Perl Subroutine problem sykkn Programming 0 02-20-2002 09:22 PM


All times are GMT -5. The time now is 09:03 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration