How can i stop a module nicely?

D

doolittle

Hello,

I would like to exit from my perl module, transferring control to the
calling program. Can this be done directly from an arbitrary subroutine
in the module?

eg

progam.pl:

use mymodule;

my $errror = mymodule::doSomething();
if ($error) { print 'error' }
....

-------------

mymodule.pm:

sub doSomething { doOne() }

sub doOne {
my $error = 1;
if ($error) {
# code to return directly to program.pl
}
}

1

------------------

So what can i replace '# code to return directly to program.pl' with so
that progam.pl prints

error

?

Die and Croak are too drastic, as they stop perl, and I don't want to
return an error up from doOne because it gets called from all over
mymodule.pm, deep within the program.

Any suggestions? Is it time i learnt about fork?

Thanks
 
J

J. Gleixner

doolittle said:
Hello,

I would like to exit from my perl module, transferring control to the
calling program. Can this be done directly from an arbitrary subroutine
in the module?

eg

progam.pl:

use mymodule;

I know this is an example, however avoid using lowercase module names.
my $errror = mymodule::doSomething();
if ($error) { print 'error' }
...

-------------

mymodule.pm:

sub doSomething { doOne() }

sub doOne {
my $error = 1;
if ($error) {
# code to return directly to program.pl
}
}

1

------------------

So what can i replace '# code to return directly to program.pl' with so
that progam.pl prints

error

perldoc -f return

Since you have

if ($error) { print 'error' }

Then $error needs to be True ( e.g. 1 ), so you could

return 1;


Die and Croak are too drastic, as they stop perl, and I don't want to
return an error up from doOne because it gets called from all over
mymodule.pm, deep within the program.

Any suggestions? Is it time i learnt about fork?

No, however if you knew about fork you'd know that's not at all related
to handling errors, instead learn about handling exceptions/errors
correctly using eval.

perldoc -f eval


Then your code would be
sub doOne {
my $error = 1;
if ($error) {

die "Hey there's an error in doOne";

eval {
mymodule::doSomething();
# ... lots of other code can be here...
};

if( $@ )
{
print "The error was $@";
}

The die is "caught" and the program will continue unless you exit/die in
the above if().
 
T

Tad McClellan

doolittle said:
I would like to exit from my perl module,


You cannot "exit" from a module.

You can "return" from a subroutine though:

perldoc -f return

transferring control to the
calling program. Can this be done directly from an arbitrary subroutine ^^^^^^^^^
in the module?


No. It must be from a subroutine that your calling program invokes.

my $errror = mymodule::doSomething(); ^^^
if ($error) { print 'error' }
^^

There is an error in the error examples of your program
containing example errors. :)


Is it time i learnt about fork?


Huh?
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top