Background documentationExamples for the SAP::DBTech::dbm Module Locate this document in the navigation structure

 

To create a connection, enter the corresponding arguments from the following examples on the command line.

Use the following command to call the sample.pl Perl script and log on to the DEMODB database as DBM operator OLEG with password MONDAY.

perl sample.pl OLEG MONDAY DEMODB

Establishing a Connection

Syntax Syntax

  1. # To reference Perl libraries:
    # ------------------------------
    use SAP::DBTech::dbm;
    # Parse the call arguments
    # --------------------------
    my $user_name = $ARGV [0];
    my $password = $ARGV [1];
    my $database_name = $ARGV [2];
    # To create a DBM session:
    # ---------------------------------------
    my $session = new DBM ('', $database_name, '', "$user_name,$password");
    # To log off:
    # -------------------------------------
    $session->release ();
End of the code.

The following examples show a shortened login process:

Syntax Syntax

  1. session = sapdb.dbm.DBM ("", $ARGV [2], $ARGV [0] . "," . $ARGV [1])
End of the code.
Listing all Databases

You can use the cmd method to execute DBM commands. The result is a character string that can be further processed using Perl.

Syntax Syntax

  1. # To reference Perl libraries:
    # ------------------------------
    use SAP::DBTech::dbm;
    # To create a DBM session:
    # ------------------------------------------------------
    my $session = new DBM ('', $ARGV [2], '', $ARGV [0] . ',' . $ARGV [1]);
    # To execute the DBM command that lists all database
    # s. The result is a character string.
    output = session.cmd ('db_enum')
    my $dbstate = 'offline';
    my $lastdb = '';
    # Individual databases are separated by line breaks.
    foreach my $line (split /\n/, $output){
    	# Data fields are separated by tab characters.
    	my ($name, $instroot, $release, $kind, $state) = split /\t/, $line;
    	if ($name ne $lastdb) {
    		# Several lines exist for each database,
    		# max. one line for each of the following kernel variants:
    		# fast and slow.
    		if ($lastdb ne '') {
    			print "$lastdb\t$dbstate\n";
     		}
    		$lastdb = $name;
    		$dbstate = 'offline';
    	}
    	# The database is active if one of the core variants
    	# is displayed as 'running'.
    	if ($state eq 'running') {
    		$dbstate = $state;
    	}
    }
    print "$lastdb\t$dbstate\n";
    $session->release ();
    
End of the code.
Handling Error Situations

If an error occurs within a cmd method, an exception is generated. You can intercept this exception with eval and query it with the $@ system variable.

Syntax Syntax

  1. # To reference Perl libraries:
    # ------------------------------
    use SAP::DBTech::dbm;
    # To create a DBM session:
    # -----------------------------------------
    my $session = new DBM ('', $ARGV [2], '', $ARGV [0] . ',' . $ARGV [1]);
    foreach my $cmd (('db_state', 'invalid command')) {
    	eval {
    		my $result = $session->cmd ($cmd);
    		# To output the result:
    		print "$cmd: OK $result\n";
    		};
    	if ($@) {
    		# To output the error message:
    		print "$CMD: ERR $@\n";
    	}
    }
    $session->release ();
    
End of the code.