The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

SAP::Rfc - RFC Function calls against an SAP R/3 System

SYNOPSIS

  use SAP::Rfc;
  $rfc = new SAP::Rfc(
		      ASHOST   => 'myhost',
		      USER     => 'ME',
		      PASSWD   => 'secret',
		      LANG     => 'EN',
		      CLIENT   => '200',
		      SYSNR    => '00',
		      TRACE    => '1' );

my $it = $rfc->discover("RFC_READ_TABLE");

$it->QUERY_TABLE('TRDIR'); $it->ROWCOUNT( 2000 ); $it->OPTIONS( ["NAME LIKE 'RS%'"] );

or pass a list of hash refs like so: $it->OPTIONS( [ { TEXT => "NAME LIKE 'RS%'" } ] );

$rfc->callrfc( $it );

print "NO. PROGS: ".$it->tab('DATA')->rowCount()." \n"; print join("\n",( $it->DATA ));

$rfc->close();

DESCRIPTION

SAP::Rfc - is a Perl extension for performing RFC Function calls against an SAP R/3 System. Please refer to the README file found with this distribution. This Distribution also allows the creation of registered RFCs so that an SAP system can call arbitrary Perl code created in assigned callbacks.

The best way to describe this package is to give a brief over view, and then launch into several examples. The SAP::Rfc package works in concert with several other packages that also come with same distribution, these are SAP::Iface, SAP::Parm, SAP::Tab, and SAP::Struc. These come together to give you an object oriented programming interface to performing RFC function calls to SAP from a UNIX based platform with your favourite programming language - Perl. A SAP::Rfc object holds together one ( and only one ) connection to an SAP system at a time. The SAP::Rfc object can hold one or many SAP::Iface objects, each of which equate to the definition of an RFC Function in SAP ( trans SE37 ). Each SAP::Iface object holds one or many SAP::Parm, and/or SAP::Tab objects, corresponding to the RFC Interface definition in SAP ( SE37 ).

For all SAP::Tab objects, and for complex SAP::Parm objects, a SAP::Struc object can be defined. This equates to a structure definition in the data dictionary ( SE11 ). Because the manual definition of interfaces and structures is a boring and tiresome exercise, there are specific methods provided to automatically discover, and add the appropriate interface definitions for an RFC Function module to the SAP::Rfc object ( see methods discover, and structure of SAP::Rfc ).

METHODS:

PARAM_NAME()

$rfc->PARM_NAME( 'a value ')

The parameter or tables can be accessed through autoloaded method calls
- this can be useful for setting or getting the parameter values.

discover()

$iface = $rfc->discover('RFC_READ_REPORT');
Discover an RFC interface definition, and automaticlly add it to an 
SAP::Rfc object.  This will also define all associated SAP::Parm, 
SAP::Tab, and SAP::Struc objects.

structure()

$str = $rfc->structure('QTAB');
Discover and return the definition of a valid data dictionary 
structure.  This could be subsequently used with an SAP::Parm, or 
SAP::Tab object.

is_connected()

if ($rfc->is_connected()) {
} else {
};
Test that the SAP::Rfc object is connected to the SAP system.

sapinfo()

%info = $rfc->sapinfo();
map { print "key: $_ = ", $info{$_}, "\n" }
      sort keys %info;
Return a hash of the values supplied by the RFC_SYSTEM_INFO 
function module.  This function is only properly called once, and
the data is cached until the RFC connection is closed - then it 
will be reset next call.

callrfc()

$rfc->callrfc($iface);
Do the actual RFC call - this installs all the Export, Import, and
Table Parameters in the actual C library of the XS extension, does
the RFC call, Retrieves the table contents, and import parameter
contents, and then cleans the libraries storage space again.

close()

$rfc->close();
Close the current open RFC connection to an SAP system, and then 
reset cached sapinfo data.

error()

$rfc->error();
Returns error string if previous call returned undef (currently 
supported for discover, structure, is_connected and sapinfo).

accept()

This is the main function to initiate a registered RFC. Consider this example that implements the same functionality as the standard rfcexec executable that comes with all SAP R/3 server implementations:

use SAP::Rfc;
use SAP::Iface;
use Data::Dumper;

# this enables the user to call die "MY_CUSTOM_ERROR"
# and only the string MY_CUSTOMER_ERROR is returned to SAP instead of
# the whole die text + line number etc.
$SAP::Rfc::EXCEPTION_ONLY = 1;

# construct the Registered RFC conection
my $rfc = new SAP::Rfc(
              TPNAME   => 'wibble.rfcexec',
              GWHOST   => '172.22.50.1',
              GWSERV   => '3300',
              TRACE    => '1' );

# Build up the interface definition that the ABAP code is going to
# call including the subroutine reference that will be invoked
# on handling incoming requests
my $iface = new SAP::Iface(NAME => "RFC_REMOTE_PIPE", HANDLER => \&do_remote_pipe);

$iface->addParm( TYPE => $iface->RFCIMPORT,
                 INTYPE => $iface->RFCTYPE_CHAR,
                 NAME => "COMMAND",
                 LEN => 256);

$iface->addParm( TYPE => $iface->RFCIMPORT,
                 INTYPE => $iface->RFCTYPE_CHAR,
                 NAME => "READ",
                 LEN => 1);

$iface->addTab( NAME => "PIPEDATA",
                LEN => 80);

# add the interface definition to the available list of RFCs
$rfc->iface($iface);

# kick off the main event loop - register the RFC connection
# and wait for incoming calls
$rfc->accept();

...

# the callback subroutine
# the subroutine receives one argument of an SAP::Iface
# object that has been populated with the inbound data
# the callback must return "TRUE" or this is considered
# an EXCEPTION
sub do_remote_pipe {
  my $iface = shift;
  warn "Running do_remote_pipe...\n";
  my $ls = $iface->COMMAND;
  $iface->PIPEDATA( [ map { pack("A80",$_) } split(/\n/, `$ls`) ]);
  warn "   Data: ".Dumper($iface->PIPEDATA);
  # force an error
  die "MY_CUSTOM_ERROR" unless $iface->PIPEDATA;
  return 1;
}

If accept() returns a defined value then the $rfc->error() can be 
checked for an associated error message.

AUTHOR

Piers Harding, piers@ompa.net.

But Credit must go to all those that have helped.

SEE ALSO

perl(1), SAP::Iface(3).