sub DoTask1 { my ($object, @arguments) = @_; if (! $object->TaskIsDone() ) { $object->SetupTaskOne(); } if (! @arguments ) { @arguments = qw(ARG1 ARG2 ARG3); } return $object->CommonTask(@arguments); } sub DoTask2 { my ($object, @arguments) = @_; if (! $object->TaskIsDone() ) { $object->SetupTaskTwo(); } if (! @arguments ) { @arguments = qw(ARG6 ARG2 ARG1 ARG5); } return $object->CommonTask(@arguments); } # (...) sub DoTask10 { my ($object, @arguments) = @_; if (! $object->TaskIsDone() ) { $object->DoTask(); $object->DoSomeMoreSetup(); } if (! @arguments) { @arguments = qw(ARG3 ARG5 ARG7); } return $object->CommonTask(@arguments); } #### sub Helper { my ($object, $defaults, @arguments) = @_; if (!@arguments) { @arguments = @$defaults; } return $object->CommonTask(@arguments); } sub DoTask1 { my ($object, @arguments) = @_; if (! $object->TaskIsDone() ) { $object->SetupTaskOne(); } return $object->Helper([ qw(ARG1 ARG2 ARG3) ], @arguments); } sub DoTask2 { my ($object, @arguments) = @_; if (! $object->TaskIsDone() ) { $object->SetupTaskTwo(); } return $object->Helper([ qw(ARG6 ARG2 ARG1 ARG5) ], @arguments); } (...) sub DoTask10 { my ($object, @arguments) = @_; if (! $object->TaskIsDone() ) { $object->DoTask(); $object->DoSomeMoreSetup(); } return $object->Helper([ qw(ARG3 ARG5 ARG7) ], @arguments); } #### sub Helper { my ($object, $code, $defaults, @arguments) = @_; if (! $object->TaskIsDone()) { $code->(); } if (!@arguments) { @arguments = @$defaults; } return $object->common_task(@arguments); } sub DoTask1 { my ($object, @arguments) = @_; return $object->Helper( sub { $object->SetupTaskOne() }, [ qw(ARG1 ARG2 ARG3) ], @arguments); } sub DoTask2 { my ($object, @arguments) = @_; return $object->Helper( sub { $object->SetupTaskTwo() }, [ qw(ARG6 ARG2 ARG1 ARG5) ], @arguments); } (...) sub DoTask10 { my ($object, @arguments) = @_; return $object->Helper( sub { $object->DoTask(); $object->DoSomeMoreSetup(); }, [ qw(ARG3 ARG5 ARG7) ], @arguments); } #### my $original_state = Module::GetServiceState(); Module::SetServiceState(0); # temporarily disable the service, if it's not already $object->DoSomething(); $object->DoSomethingElse(); Module::SetStatus($original_state); # restore it to what it was before #### sub TempDisableService { my $code = shift; my $original_state = Module::GetServiceState(); Module::SetServiceState(0); # temporarily disable the service, if it's not already $code->(); Module::SetStatus($original_state); return; } # and later... TempDisableService sub { $object->DoSomething(); $object->DoSomethingElse(); }; # and even later... TempDisableService sub { $object->DoSomethingMore(); $object->DoSomethingAlso(); }; #### TempDisableService { $object->DoSomething(); $object->DoSomethingElse(); };