Dealing with exit in a Modulino
  Home FAQ Contact Sign in
comp.lang.perl.moderated only
 
Advanced search
POPULAR GROUPS

more...

comp.lang.perl.moderated Profile…
 Up
Dealing with exit in a Modulino         


Author: Marc Girod
Date: Mar 26, 2008 09:33

Hello,

I am writing tests for a CPAN module, which contains a script.
I found it convenient to make this script a Modulino, so that
I could 'use' it from my test.pl (thus being sure I use the same
perl, the same context etc.)
Now, this script does call exit in different places, which is not
suitable for my purpose.
The solution I used so far strikes me as inelegant:

- pass a $noexit when calling from make test
- replace everywhere:
exit $rc
with
($noexit ? return $rc : exit $rc)

There is even a signal handler...

Marc
3 Comments
Re: Dealing with exit in a Modulino         


Author: Peter Makholm
Date: Mar 27, 2008 00:06

Marc Girod gmail.com> writes:
> I am writing tests for a CPAN module, which contains a script.
> I found it convenient to make this script a Modulino, so that
> I could 'use' it from my test.pl (thus being sure I use the same
> perl, the same context etc.)
> Now, this script does call exit in different places, which is not
> suitable for my purpose.

For testing purposes you can override the exit function to do
something else. I would probaly do something like:

sub My::Module::exit {
die "exit called with return code $_[0]\n";
}

My::Module::somefunction();

is($@, "exit called with return code 1",
"somefunction exited as expected");

For an even better test you could have your overridden exit function
note where it was called from. If you need to override exit globally,
and not just within you My::Module, you can use CORE::GLOBAL::exit
instead of My::Module::exit.
Show full article (1.29Kb)
no comments
Re: Dealing with exit in a Modulino         


Author: Marc Girod
Date: Mar 28, 2008 09:38

On Mar 27, 7:06 am, Peter Makholm makholm.net> wrote:
> For testing purposes you can override the exit function to do
> something else. I would probaly do something like:
...

Thanks. That's exactly the kind of answer I was hoping for.
> You are using Test::More or another Test::Harness module to du you
> testing, right?

Right. Test::More for now.

Marc
no comments
Re: Dealing with exit in a Modulino         


Author: Marc Girod
Date: Mar 29, 2008 04:44

On Mar 28, 4:38 pm, Marc Girod gmail.com> wrote:
> Thanks. That's exactly the kind of answer I was hoping for.

Sorry, I had left my critical spirit home.
The problem is then: how to return to the caller context
(when there is one, i.e. when I do not choose to exit),
instead of to the caller of my exit subroutine?

Some kind of exception mechanism.

Thanks,
Marc
no comments