diffrence between method call and function call in perl

 
Thread Tools Search this Thread
Special Forums UNIX and Linux Applications Infrastructure Monitoring diffrence between method call and function call in perl
# 1  
Old 03-29-2009
diffrence between method call and function call in perl

Hello,
I have a problem with package and name space.
Code:
require "/Mehran/DSGateEngineLib/general.pl";
use strict;
sub System_Status_Main_Service_Status_Intrusion_Prevention
{
 my %idpstatus;
        my @result;
        &General_ReadHash("/var/dsg/idp/settings",\%idpstatus);
        #print $idpstatus{'ENABLE_IPS'};
        if( $idpstatus{'ENABLE_IPS'} )
        {
                $result[0] = 'on';
                if( $idpstatus{'POLICY'} eq "default" )
                {
                        $result[1] = "Default";
                  }elsif ($idpstatus{'POLICY'} eq "alert")
                  { $result[1] = "Alert"; }
                  else { $result[1] = "Action"; }
        }elsif ($idpstatus{'ENABLE_IPS'} eq 'off')
        {
          $result[0] = 'off';
        }

                print $result[0]
}
&System_Status_Main_Service_Status_Intrusion_Prevention();

This is my script that work properly, but when I pack this function to other namespace and used it to another namespace an error arise. this error: used of intialized value $result[0] . Note that file have been opened and haven't any error.
below is my master file that on interpreting got me error. Code from aspect of spell and common concepts are true but I think there are an exclusive concept on package and namespace's method call that I don't know it. REALLY IT IS IMPORTANT. HELP ME Experts plz.
Thanks in advance.
Code:
package DSGateEngineLib::System;

require "DSGateEngineLib/general.pl";
#use strict;
sub new #Constructor
{
    my ($class) = @_;
    my $self = {    };
    bless $self, $class;
    return $self;
}

sub Do_Action
{
    my ($self, @arg) = @_;
    $_=$arg[0];
    #print $arg[0];
    #case "Object_Address_ListAll"
    if ($_ eq "System_Status_Main_Resource_Usage_CPU")
    {
        #print $arg[0];
        my @result =  &System_Status_Main_Resource_Usage_CPU();
        return \@result;
    }
    elsif($_ eq "System_Status_Main_Service_Status_Firewall")
    {
        my @result = &System_Status_Main_Service_Status_Firewall();
        return \@result;
    }elsif($_ eq "System_Status_Main_Service_Status_Intrusion_Prevention")
    {
        my @result = &System_Status_Main_Service_Status_Intrusion_Prevention();
        return \@result;
    }

}

#--------------------------------------------
#System -> Status -> Main -> Service Status
#--------------------------------------------
sub System_Status_Main_Service_Status_Firewall
{
    my $hash;
    my $line = `sed -n "/^firewall=/p" /var/dsg/license`;
    my $stat = index $line,'on';
    if ($stat>=0) 
    {
        return 'on';
    }elsif($stat<= -1) 
    {
        return 'off';
    }
}

sub System_Status_Main_Service_Status_Intrusion_Prevention
{
 #my %idpstatus;
    my @result;
    &General_ReadHash("/var/dsg/idp/settings",\%idpstatus);
    #print $idpstatus{'ENABLE_IPS'};
    if( $idpstatus{'ENABLE_IPS'} )
    { 
        $result[0] = 'on';
        if( $idpstatus{'POLICY'} eq "default" ) 
        { 
            $result[1] = "Default"; 
          }elsif ($idpstatus{'POLICY'} eq "alert") 
          { $result[1] = "Alert"; } 
          else { $result[1] = "Action"; }
    }elsif ($idpstatus{'ENABLE_IPS'} eq 'off') 
    { 
      $result[0] = 'off';
    }
print $result[0];
}

sub System_Status_Main_Service_Status_Web_Filter
{
}

sub System_Status_Main_Service_Status_Anti_Virus
{
}

sub System_Status_Main_Service_Status_Anti_Spam
{
}

sub System_Status_Main_Service_Status_IMnP2P
{
}

sub System_Status_Main_Service_Status_VPN
{
}

#-----------------------------------------------------
#System -> Status -> Main -> Resource Usage
#-----------------------------------------------------
sub System_Status_Main_Resource_Usage_CPU
{
    my @cpu = `ps aux`;
    chomp @cpu;
    my @cpuusage;
    $cpuusage[0]=0;
    my $flag;
    $flag = 0;chomp @cpu;
    my @partss = split('\n', $cpu[0]);
    foreach my $l (@partss)
    {
         if ($flag == 0)
        {
            $flag=1;
            next;
        }
        my @parts = split(' ', $l);
        $cpuusage[0] += $parts[2];
    }
  return $cpuusage[0];
}

sub System_Status_Main_Resource_Usage_Memory
{

  my @echom = `/usr/bin/free -ot |grep Mem`;
  my $mperc;
  #chomp(@echom);
  foreach my $mline (@echom) 
  {
    chomp($mline);
    my ($mdev, $mtotal, $mused, $mfree, $mshared, $mbuffers, $mcached) = split(/\s+/, $mline);
    my $memuse = $mfree + $mbuffers + $mcached;
    $mperc = int((($memuse/$mtotal)*100));
    $mperc = 100 - $mperc;
  }
  return $mperc;
}

sub System_Status_Main_Resource_Usage_Data_Disk
{
  my @hdddata = `df -hT | grep \"\/$\"`;
  chomp @hdddata;
  my @parts = split(' ', $hdddata[0]);
  my $HddData = $parts[5];
  return $HddData;
}

sub System_Status_Main_Resource_Usage_Log_Disk
{
  my @hddlog = `df -hT | grep \"\/var\/log$\"`;
  chomp @hddlog;
  my @parts = split(' ', $hddlog[0]);
  my $HddLog = $parts[5];
  return $HddLog;
}

#-----------------------------------------------------
#System -> Status -> Main -> System Information
#-----------------------------------------------------
sub System_Status_Main_System_Information_Serial_Number
{
}
sub System_Status_Main_System_Information_Model
{
}
sub System_Status_Main_System_Information_Firmware_Version
{
}
sub System_Status_Main_System_Information_System_Time
{
}
sub System_Status_Main_System_Information_Uptime
{
}
sub System_Status_Main_System_Information_Last_DSGate_Update
{
}
sub System_Status_Main_System_Information_Last_IDP_Update
{
}
sub System_Status_Main_System_Information_Last_AntiVirus_Update
{
}

#-----------------------------------------------------
#System -> Status -> Main -> Network Information
#-----------------------------------------------------
sub System_Status_Main_Network_Information_Port_status # Note that, this method would have loop for supporting all ports 
{
}
sub System_Status_Main_Network_Information_Packet_Statistic_All
{
}
sub System_Status_Main_Network_Information_Packet_Statistic_TCP
{
}
sub System_Status_Main_Network_Information_Packet_Statistic_ICMP
{
}
sub System_Status_Main_Network_Information_Packet_Statistic_HTTP
{
}
sub System_Status_Main_Network_Information_Packet_Statistic_UDP
{
}

#-----------------------------------------------------
#System -> Status -> Main -> Traffic Graph
#-----------------------------------------------------
sub System_Status_Main_Traffic_Graph_Ports # Note that, this method would have loop for supporting all ports
{
}

#-----------------------------------------------------
#System -> Status -> Main -> Last Signatures Updates
#-----------------------------------------------------
sub System_Status_Main_Last_Signatures_Update
{
}

#-----------------------------------------------------
#System -> Status -> Main -> Alert Message
#-----------------------------------------------------
sub System_Status_Main_Alert_Messages
{
}

##################################################################################
#---------------------------------------------------------------------------------
#System -> Status -> Monitor
#_________________________________________________________________________________
##################################################################################
sub System_Status_Monitor_System_Resources
{
}

#---------------------------------------------------------------------------------
#System -> Status -> Monitor -> System Resources
#---------------------------------------------------------------------------------
sub System_Status_Monitor_System_Resources_Traffic_On_Ports
{
}

sub System_Status_Monitor_System_Resources_Memory_Usage_Over_Last_Hour
{
}

sub System_Status_Monitor_System_Resources_CPU_Load_Over_The_Last_Hour
{
}

##################################################################################
#---------------------------------------------------------------------------------
#System -> Status -> Sessions
#_________________________________________________________________________________
##################################################################################
sub System_Status_Session_Conntrack_Viewer
{
}
sub System_Status_Session_Active_Connections
{
 #Total: 1    connections    TCP: 1    UDP: 0    ICMP: 0
}
sub System_Status_Session_Connections_ListAll
{
 #There are fields Proto-SRCIP-SRCPort-SRCPackets-SRCbytes-State-DSTIP-DSTPort-DSTPackets-DSTbytes
}


################################################
#--------------------------------------------
#System -> Admin -> Users
#--------------------------------------------
################################################
sub System_Admin_Current_Users_Lists
{
}
sub System_Admin_Current_Users_Edit
{
}
sub System_Admin_Current_Users_Delete
{
}

sub System_Admin_AddNew_User
{
}
################################################
#--------------------------------------------
#System -> Admin -> Permissions
#--------------------------------------------
################################################

sub System_Admin_Permissions_List_Of_Users_and_Permissions
{
}
sub System_Admin_Permissions_Edit_Permissions
{
}
sub System_Admin_Permissions_Delete
{
}
################################################
#--------------------------------------------
#System -> Config -> Time
#--------------------------------------------
################################################
sub System_Config_Time_Apply
{
    #Some of inputed variable
    #(Time Zone, Time and Date, Network Time Retrieval, Network Time Servers)
}
sub System_Config_Time_Get
{
    #Some of inputed variable
    #(Time Zone, Time and Date, Network Time Retrieval, Network Time Servers)
}

################################################
#--------------------------------------------
#System -> Config -> Admin
#--------------------------------------------
################################################
sub System_Config_Admin_Authenticate_Time_Out_Set
{

}
sub System_Config_Admin_Authenticate_Time_Out_Get
{

}

################################################
#--------------------------------------------
#System -> Config -> ISPUtil GUI
#--------------------------------------------
################################################
sub System_Config_ISPUtil_Interface_Check_Status
{
}

sub System_Config_ISPUtil_Connector_Apply
{
}
sub System_Config_ISPUtil_Connector_Get
{
}

sub System_Config_ISPUtil_Parameters_List_Apply
{
}
sub System_Config_ISPUtil_Parameters_List_Get
{
}
################################################
#--------------------------------------------
#System -> Config -> SNMP
#--------------------------------------------
################################################
sub System_Config_SNMP_Status_Apply
{
}
sub System_Config_SNMP_Status_Get
{
}
################################################
#--------------------------------------------
#System -> Maintenance -> Backup
#--------------------------------------------
################################################
sub System_Maintenace_Make_Backup
{
}

sub System_Maintenace_Restore_Backup
{
}

################################################
#--------------------------------------------
#System -> Maintenace -> Update
#--------------------------------------------
################################################
sub System_Maintenace_AutoUpdate_Settings
{
}
sub System_Maintenace_ManualUpdate_Settings
{
}
sub System_Maintenace_ManualUpdate_Install
{
}
################################################
#--------------------------------------------
#System -> Maintenace -> Update
#--------------------------------------------
################################################
sub System_Maintenace_License_CurrentLicense
{
}

################################################
#--------------------------------------------
#System -> Maintenace -> Tools
#--------------------------------------------
################################################
sub System_Maintenace_Tools_Ping_Traceroute_Whois
{
}
sub System_Maintenace_Tools_SystemOperation_Default_Factory
{
}
sub System_Maintenace_Tools_SystemOperation_Reboot_Shutdown
{
}
################################################
#--------------------------------------------
#System -> Notifications -> Setting
#--------------------------------------------
################################################
sub System_Notifications_Setting_List_All
{
}
sub System_Notifications_Setting_Groups_AddNew
{
}
sub System_Notifications_Setting_Edit
{
}
sub System_Notifications_Setting_Delete
{
}

################################################
#--------------------------------------------
#System -> Notifications -> Alerts
#--------------------------------------------
################################################
sub System_Notifications_Alerts_List_All
{
}
sub System_Notifications_Alerts_Groups_AddNew
{
}
sub System_Notifications_Alerts_Edit
{
}
sub System_Notifications_Alerts_Delete
{
}
################################################
#--------------------------------------------
#System -> User -> Radius
#--------------------------------------------
################################################
sub System_User_Current_Radius_Server_AddNew
{
}
sub System_User_Current_Radius_Server_Get
{
}

################################################
#--------------------------------------------
#System -> User -> Ldap
#--------------------------------------------
################################################

sub System_User_Current_Ldap_Server_AddNew
{
}
sub System_User_Current_Ldap_Server_Get
{
}

#&System_Status_Main_Service_Status_Intrusion_Prevention();For debuging reasons
1;

# 2  
Old 03-29-2009
Just guessing....

After cleaning up this sloppy code to make it readable you can see that $result[0] has the chance of never being defined:

Code:
rrequire "/Mehran/DSGateEngineLib/general.pl";
use strict;
sub System_Status_Main_Service_Status_Intrusion_Prevention {
   my %idpstatus;
   my @result;
   &General_ReadHash("/var/dsg/idp/settings",\%idpstatus);
   #print $idpstatus{'ENABLE_IPS'};
   if( $idpstatus{'ENABLE_IPS'} ){
      $result[0] = 'on';
      if( $idpstatus{'POLICY'} eq "default" ){
         $result[1] = "Default";
      }
      elsif ($idpstatus{'POLICY'} eq "alert"){
         $result[1] = "Alert";
      }
      else {
         $result[1] = "Action";
      }
   }
   elsif ($idpstatus{'ENABLE_IPS'} eq 'off'){
      $result[0] = 'off';
   }
   print $result[0]
}
&System_Status_Main_Service_Status_Intrusion_Prevention();

You should add a fall-through condition to the end of the if/elsif block to make sure $result[0] will always be defined:

Code:
require "/Mehran/DSGateEngineLib/general.pl";
use strict;
sub System_Status_Main_Service_Status_Intrusion_Prevention {
   my %idpstatus;
   my @result;
   &General_ReadHash("/var/dsg/idp/settings",\%idpstatus);
   #print $idpstatus{'ENABLE_IPS'};
   if( $idpstatus{'ENABLE_IPS'} ){
      $result[0] = 'on';
      if( $idpstatus{'POLICY'} eq "default" ){
         $result[1] = "Default";
      }
      elsif ($idpstatus{'POLICY'} eq "alert"){
         $result[1] = "Alert";
      }
      else {
         $result[1] = "Action";
      }
   }
   elsif ($idpstatus{'ENABLE_IPS'} eq 'off'){
      $result[0] = 'off';
   }
   else { #<-- fall-through condition 
      $result[0] = 'unknown status';
   }
   print $result[0]
}
&System_Status_Main_Service_Status_Intrusion_Prevention();


Last edited by KevinADC; 03-29-2009 at 07:07 PM..
# 3  
Old 03-29-2009
Its worth nothing that the below elsif condition will never be evaluated:

Code:
   elsif ($idpstatus{'ENABLE_IPS'} eq 'off'){
      $result[0] = 'off';
   }

I leave it up to you to figure out why that is.
# 4  
Old 03-30-2009
Kevin thanks.
it's sloppy code due to it is under constructor.Smilie
but my problem isn't fall through condition. I have two code. the first code work properly. my problem is method call. Mr kevin, when I call the first code, it works good due to I call that function at the end of same name space (file)."&System_Status_Main_Service_Status_Intrusion_Prevention();".

but when I call it from an other perl file (name space ) which System.pm used on it, an error occured. sake is strange!SmilieSmilie .
Error is use of uninitialized idpstatus{"ENABLE_IPS"}.

Last edited by Zaxon; 03-30-2009 at 02:28 AM..
# 5  
Old 03-30-2009
You should at least test the code with the fall-through condition added and see if that gets the script to run without reporting the warning (its not an error) about the unintialized variable. If it does not get rid of the warning you will know my suggestion is wrong, but if it does then your assumption of what is causing the warning could be wrong. And please note, it is a warning, not an error. Sometimes variables do have the chance of being uniitialized, which is why a default or fall-through condition is almost always a good idea.

Also, writing neat and readable code during constructions is very important, that is no excuse for writing sloppy code. Slopy code always makes tracking down bugs much harder than it should be. Its very good though that you are using "strict" and I assume "warnings".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to Call a Function

Hi I have created a function in a Shell Script test.sh function fnTest() { echo "My first Method } I have called this function in my test.sh cat abc.txt | grep "test" echo " test" fnTest But while running the shell script i got the following error: ... (2 Replies)
Discussion started by: nanthagopal
2 Replies

2. Emergency UNIX and Linux Support

Perl error: Can't call method "value" on an undefined value

Hi, I am running a perl script to automate a process and I keep running into a error can't find the "value" Can't call method "value" on an undefined value at process_file.pl line 44. file is CVS cell is ifdfdxrfmp.ksh Here is the script I have also attached it as well: ... (2 Replies)
Discussion started by: vpundit
2 Replies

3. Shell Programming and Scripting

Function call

Hi foiks i am unable to find what is wrong in my code mu functionality is to exit from shell when i give 99 but it is not calling function ext Could you please correct me. read option if ; then ext else echo "out" fi function ext { echo "tested 99 and exit... (12 Replies)
Discussion started by: kojo
12 Replies

4. Shell Programming and Scripting

Function Call

Hi, I have a string corresponding to a function. How I can call that function without if statement? Thanks in advance. (4 Replies)
Discussion started by: Zaxon
4 Replies

5. Shell Programming and Scripting

function call

hi, can any one help me to correct this function call. awk -F "," '{ {first=$1; sec=$2; tro=$3;quat=$4 } if (tro == "") { $3 = search "$file2" "$first" "$file3" {print $1","$2","$3","$4} } else {print $1","$2","$3 $4}}' $file1 > $file search () { (2 Replies)
Discussion started by: kamel.seg
2 Replies

6. Shell Programming and Scripting

call function

I have a function check_ok in my abc.sh. which return me 1 or 0 . I want to call this fuction through other shell script. this shell also send two parameter to calling function. Can you please tell me how. I am very new in unix. #!/bin/bash date_equal() { sqlplus -silent... (4 Replies)
Discussion started by: Jamil Qadir
4 Replies

7. Programming

Help with a function call

Hi, Can anyone help me figure out the problem I'm having with a function call? I have a header file, which sets an enum: typedef enum {INFO, WARNING, FATAL} Levels; int log_event (Levels, char *fmt, ...); ..then the function is called this way: log_event(INFO, "Message text"); ... (6 Replies)
Discussion started by: Stevhp
6 Replies

8. Shell Programming and Scripting

Recursive method call getting terminated ???

Hello people, Need help !!! What am I doing wrong here ? I am writing a function to recursively list the files under a folder and it's sub-folders. Problem is once it list the files under the innermost folder, it terminates. What do I need to do so that it returns and list files under the... (3 Replies)
Discussion started by: tipsy
3 Replies

9. Shell Programming and Scripting

function call

can I call a function in bash script just as in C++ while do function() done function() thanks, Steffen (3 Replies)
Discussion started by: forever_49ers
3 Replies

10. Shell Programming and Scripting

perl function call tracking

Assuming the following code sub foo { dosomething {...} else { foo } is the number of times foo has been called kept track of internally and how could I access that count? (1 Reply)
Discussion started by: thumper
1 Replies
Login or Register to Ask a Question