Loading ...
Sorry, an error occurred while loading the content.

271RE: [soaplite] Can the server process parameters by name?

Expand Messages
  • Chris Davies
    Apr 30, 2001

      Hi folks,

      Is it reasonable to provide a procedure that can handle arguments supplied either by name and by position? I can see one way of implementing this:

      package ByNameOrPos;

      use strict;
      use vars qw(@ISA);
      @ISA = qw(SOAP::Server::Parameters);

      sub ParamsTest
      {
          my $self = shift @_;                        # Class
          my $envelope = pop @_;                      # Envelope
          my $argshash = $envelope->method;           # Args by name
          my ($fruit, $vegetable, $meat) = @_;        # Args by pos

          # Check to see whether args have been passed by name
          my ($byName, $missing) = (0, 0);
          foreach my $mandatory (qw(fruit vegetable meat)) {
              $byName = 1 if exists $argshash->{$mandatory};
              $missing = 1 if not exists $argshash->{$mandatory};
          }
          $missing = 0 unless $byName;
         
          warn "byName=$byName, missing=$missing: fruit=$fruit, vegetable=$vegetable, meat=$meat; self=$self, envelope=$envelope, argshash=$argshash";
         
          my $byName = join (", ", map { "$_ => $argshash->{$_}" } sort keys %$argshash);
          my $byPosn = join (", ", @_);
         
          return "Given ($byName) by name and ($byPosn) by position." .
              ' Args ' . ($byName ? 'passed by name' : 'probably passed by position') . '.' .
              ($missing ? ' Some mandatory parameters are missing.' : '');
      }

      Is there a more reliable or more elegant technique that I can use instead of this? For starters, my proposed solution breaks if there are no mandatory parameters against which to check.

      For completeness, here's how I call this from SOAPsh.pl:

      > ParamsTest('apple', 'spinach', 'chicken')
      --- SOAP RESULT ---
      'Given (c-gensym215 => apple, c-gensym217 => spinach, c-gensym219 => chicken) by name and (apple, spinach, chicken) by position. Args passed by name.'

      > ParamsTest(SOAP::Data->name(meat=>'beef'),SOAP::Data->name(fruit=>'apricot'),SOAP::Data->name(vegetable=>'carrot'))
      --- SOAP RESULT ---
      'Given (fruit => apricot, meat => beef, vegetable => carrot) by name and (beef, apricot, carrot) by position. Args passed by name.'

      >

      ParamsTest(SOAP::Data->name(meat=>'beef'),SOAP::Data->name(fruit=>'apricot'))
      --- SOAP RESULT ---
      'Given (fruit => apricot, meat => beef) by name and (beef, apricot) by position. Args passed by name. Some mandatory parameters are missing.'

      I also would expect to have to massage the provided parameter names if something like MS .NET were to call this procedure with arguments by name, but in the interests of simplicity I'm ignoring that for now.

      Thanks,
      Chris

    • Show all 26 messages in this topic