Link to home
Start Free TrialLog in
Avatar of gppkuntz
gppkuntz

asked on

What is the differnce between System and System::Execute in Perl?

I am not a perl expert but in maintaining code, I am seeing System::Execute.  I am do not know what the command !System::Execute($cmd) does or rather System::Execute.  
I am also wondering what is the difference between using System($cmd) and System::Execute($cmd)

One example is:
if (!System::Execute($cmd))
{
      $::ErrorMessage = "Error calling server $::NameServer";
      return 1;
}

I assume that these commands are used in a Unix environment and not a Windows environment.
Avatar of Tintin
Tintin

System::Execute is not a CPAN module, so it's probably been written by the author of the script.  Without seeing the contents of System::Execute, it's impossible to say exactly what it does.

Going along with Tintin, is there a "use System" anywhere (likely near the top) of the script?
If so, post the code for the System.pm file.
Did you load a module like
use System;
System::Execute($cmd) would call the Execute sub in the System module
you would need to consult documentation for that module to find what it does
Avatar of gppkuntz

ASKER

I missed the fact that one was a lower case "s" and the other upper case, system($cmd).
The System.pm is pasted below:
package System;
use File::Basename;
BEGIN {
        $::InstallDir = dirname("$0");
        push @INC, "$::InstallDir";

        select(STDERR);
        $| = 1;
        select(STDOUT);
        $| = 1;
        if ($ENV{'ProgRoot'} eq "")
            {$ENV{'ProgRoot'} = "$::InstallDir/..";
             print "Using default ProgRoot=$ENV{'ProgRoot'}\n";}
        push @INC, "$ENV{'ProgRoot'}/engine";
}

use Environment;
use Keyword;

BEGIN { }

#---------------------------------------------------------------------------------
# function: System
# description: Performs a specified system command
# parameters: ShellCommand OutputLog ErrorLog ContinueFlag
#---------------------------------------------------------------------------------
sub Execute {

      my ( $Command, $OutputLog, $ErrorLog, $ContinueFlag, $IsSilent ) = @_;
      my ( $Keyword, $IsPrintCommand );

      $IsPrintCommand = "yes";

      #---------------------------------------------------
      # check to see if secure keyword in command
      #---------------------------------------------------

      foreach $Keyword ($::KeywordsSecure) {
            if ( rindex( "$Command", "##" . $Keyword . "##" ) != -1 ) {

                  #---------------------------------------------------
                  # secure keyword exit, do not print value in log
                  #---------------------------------------------------
                  $IsPrintCommand = "no";
            }
      }

      #---------------------------------------------------
      # if keyword exists, print preprocessed command
      #---------------------------------------------------

      if ( "$IsPrintCommand" eq "yes" ) {
            if ( rindex( "$Command", "##" ) != -1 ) {
                  debugPrint("Executing $Command...\n") unless ($IsSilent);
            }
      }
      debugPrint("Executing $Command...\n") ;

      #---------------------------------------------------
      # if keywords exist, expand command keywords
      #---------------------------------------------------

      $Command = Keyword::Expand("$Command");

      #---------------------------------------------------
      # if specified, redirect output and error
      #---------------------------------------------------

      if ( "$OutputLog" ne "" ) {
            $Command = "$Command >$OutputLog";
      }

      if ( "$ErrorLog" ne "" ) {
            $Command = "$Command 2>$ErrorLog";
      }
      if ( system("$Command") ) {
            #---------------------------------------------------
            # shell command failed
            #---------------------------------------------------

            if ( "$IsPrintCommand" eq "yes" ) {
                  $::ErrorMessage = "Command failed for $Command";
                  infoPrint("Command failed for $Command \n") unless ($IsSilent);
            }
            else {
                  $::ErrorMessage = "Command failed";
                  infoPrint("Command failed \n") unless ($IsSilent);
            }

            if ( "$ContinueFlag" ne "yes" ) { return 0 }

            infoPrint("\nError: $::ErrorMessage") unless ($IsSilent);
            infoPrint("\nError: Continue On error set and processing continued\n\n")
              unless ($IsSilent);
                 return 0;  #when it fails
      }

      return 1;
}

sub infoPrint {
      print( $_[0] ."\n\n" );
      return 1;
}

sub debugPrint {
      if ( lc($::DEBUG_MODE) eq "yes" ) {
            print( $_[0] );
      }
      return 1;
}

sub rmdir {
      my ($dir) = $_[0];
      if ( lc($::DEBUG_MODE) eq "yes" ) {
            print("Attempting to clean up directory: $dir\n");
      }

      # do minimum security check, we do not want to rm -rf the whose root!
      if ( $dir =~ /^ *\/ *$/ ) {
            print("Can not clean root dir in this unix host!\n");
            return 0;
      }
      else {
            print("Cleaning up $dir:\n");
            system("rm -rf $dir");
      }
      return 1;
}

END { }

return 1;
ASKER CERTIFIED SOLUTION
Avatar of Adam314
Adam314

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, I appreciate your time.  The keyword.pm appears not to have been completed before the originator left.  I was hoping that the built-in-system commands were implemented with just some options.