-1

I have written a function and the function call works as expected and i get the desired output when i make the function call. However I'd like to enhance it by making a couple of changes to it. For now the function takes 'ip' and 'service names' as arguments.

Enhancement : 1 ) When i make the function call and if i do not pass any services as arguments, I want the function to take all the services as arguments by itself There are totally 5 services. say for example service1, service2,service3,service 6,service8.(These are just sample names.it could be anything). So what is the change that i need to make to the function to ensure that if services are not passed as arguments, The function must take all the services as arguments.

Eg Function call :

$self->{'status_of_services'} = $self->{'services_obj'}->health_check('ip')

So when the above function call is made all the function must take all the services as arguments

Enhancement.2) user wants to pass one or two services as arguments in the function call. currently i store them in an array. like @services = ('service1','service2','service3') .I do not want to store them in an array.instead i want to pass the services as arguments directly like below. Any suggestions please.

eg : $self->{'status_of_services'} = $self->{'services_obj'}->health_check('ip', [service1 ,service2]);

Function :

sub health_check{

     my ($self, $ip, @service_name)  = @_;

     $self->{'health_checks_obj'}  = ServiceManager->new( ip => $ip );

     $self->{'services_status'}  = $self->{'health_checks_obj'}->isRunning( {service => @service_name} );

      sleep(5);

     if ( not $self->{'services_status'} ) {
         $self->{'health_checks_obj'}->start( {service => @service_name ,  timeout => '30'} );

           sleep (3);
       }

    return  1 ;
}

Function call :

my @services = ('service1', 'service2', 'service3','service4','service5');

     $self->{'status_of_services'} = $self->{'services_obj'}->health_check('ip', @services);

     INFO (' Health check result is : ' . $self->{'status_of_services'} );

Output :

 Health check result is : 1
user3587025
  • 173
  • 1
  • 4
  • 17

1 Answers1

0

You just need to check whether the array reference is defined, and default the values used if not, so you would write

sub health_check{
  my ($self, $ip, $services)  = @_;

  my @services;
  if ( $services ) {
    @services = @$services;
  }
  else {
    @services = qw/ service1 service2 service3 service6 service8 /;
  }

  ...
}

But are you sure you want to pass an array reference? As long as it is the last part of your passed parameters you can use a bare list. You would do that by just assigning an array after the $ip parameter, and defaulting it if it is empty, like this

sub health_check{
  my ($self, $ip, @services)  = @_;

  unless ( @services ) {
    @services qw/ service1 service2 service3 service6 service8 /;
  }

  ...
}

And you would be able to call it like this

$self->{'services_obj'}->health_check('ip', 'service1', 'service2');

or

$self->{'services_obj'}->health_check('ip');
Borodin
  • 126,100
  • 9
  • 70
  • 144
  • are these values comma separated. services qw/ service1 service2 service3 service6 service8 /; } – user3587025 Apr 14 '15 at 01:32
  • It is just as I have written them. `qw/ service1 service2 service3 service6 service8 /` is the same as `('service1', 'service2', 'service3', 'service6', 'service8')` but much nicer to type and read. You could also call your method as `health_check(qw/ ip service1 service2 /)` – Borodin Apr 14 '15 at 01:49
  • I'm getting the following error. Odd number of elements in anonymous hash at Services.pm line 126 – user3587025 Apr 14 '15 at 03:02
  • That will be because if the `{ service => @service_name }` hash you're passing to `isRunning` and `start`. I imagine they can't handle multiple services, so you'll need to iterate over the `@services` array processing one service at a time – Borodin Apr 14 '15 at 03:48
  • Actually, it looks like you need to think about this some more. `$self` has only one `services_status` value, so how can it reflect the status of multiple services? I would have thought the status should be a property of a `Service` object, and each `ServiceManager` should have several `Service` objects, but I know virtually nothing about your application – Borodin Apr 14 '15 at 03:52
  • Instead I think you have a `Services` object that has a `ServiceManager`, which seems inside-out – Borodin Apr 14 '15 at 04:00