#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2009
    Posts
    17
    Rep Power
    0

    Need Help in creating a data structure for my input file using OOP in Perl


    Hi all,

    I am trying to automate and trying to write a perl scirpt in OOPS.

    I have written the perl script without using OOPS. Now I want to add few more things to my perl script. I want to try and learn OOP in perl.

    Here is what have done so far. my script uses 3 files
    1. perlscript.pl.
    2. input_filel( This file has all the inputs)
    3. Output_file (After running the script all the commands are send to this file)

    cat input_file
    Code:
    Pradeeps-MacBook:coding mayur$ cat input 
    cluster=y 
    symm_id=4317
    FA_A=05FA,11FA,CX_0963_SPA2,CX_0963_SPB2
    FA_B=12FA,06FA,CX_0963_SPA3,CX_0963_SPB3
    ABEECCEDDD6|21 00 00 24 FF 23 58 CA,21 00 00 24 FF 23 58 CB|21 00 00 24 FF 23 58 CC,21 00 00 24 FF 23 58 CD
    ABEECCEDDD7|21 00 00 24 FF 23 58 AE,21 00 00 24 FF 23 58 9C|21 00 00 24 FF 23 58 AF,21 00 00 24 FF 23 59 AG
    ABEECCEDDD8|21 00 00 24 FF 23 58 96|21 00 00 24 FF 23 58 EC
    PORT_GROUP=linx_pord_FA05ea_12ea_PRTGRP
    Policy=Gold
    Pradeeps-MacBook:coding mayur$
    My script will parse the input_file and get all the details which are in the right hand side and generate commands accordingly. Right now this script only runs for one frame. I want to add a one value to the frame sepearted by "," and my script should generate value for both the frames.

    Now am trying to learn OOP in perl I am not sure how to create dynaic object and what kind of relationship I have to create.

    Code:
    #!/usr/bin/perl -w 
    use Switch; 
    use Shell; 
    ############################################################################ 
    ## This scirpt will generate the i/p file for all switch and vmax EXECUTE. 
    ### You have to run the this file in the following format ./command_file input Final_op_location 
    ### Eg: ./command.pl /usr/tmp/input_file /var/tmp/Pradeep/EXECUTE 
    ### Input file can in the following format.
    #cluster=y 
    #symm_id=4317
    #FA_A=05FA,11FA,CX_0963_SPA2,CX_0963_SPB2
    #FA_B=12FA,06FA,CX_0963_SPA3,CX_0963_SPB3
    #ABEECCEDDD6,21 00 00 24 FF 23 58 CA,21 00 00 24 FF 23 58 CB
    #ABEECCEDDD7,21 00 00 24 FF 23 58 AE,21 00 00 24 FF 23 58 9C
    #ABEECCEDDD8,21 00 00 24 FF 23 58 96,21 00 00 24 FF 23 58 EC
    #ABCDEFGH021      21000024ff2fc59c 21000024ff2fc5c6 05FA 11EA
    #ABCDEFGH022      21:00:00:24:ff:2f:c5:be 21:00:00:24:ff:2f:c5:c2 
    #ABCDEFGH023      21:00:00:24:ff:2f:c5:bc 21:00:00:24:ff:2f:c5:c0
    #ABCDEFGH024      21:00:00:1b:32:94:55:98 21:00:00:1b:32:13:f8:24
    #ABCDEFGH025      21:00:00:24:ff:2f:c6:3a 21:00:00:24:ff:2f:c5:de
    #ABCDEFGH026      21:00:00:1b:32:9a:ed:03 21:00:00:1b:32:93:75:ee 
    #ABCDEFGH027      21:00:00:24:ff:2f:c4:c2 21:00:00:24:ff:2f:c5:cc
    #ABCDEFGH028      21:00:00:24:ff:2f:c4:b8 21:00:00:24:ff:2f:c4:c4 
    #ABCDEFGH029      21:00:00:24:ff:2f:ee:58 21:00:00:24:ff:2f:eb:d4 
    #ABCDEFGH030      21:00:00:24:ff:2f:eb:72 21:00:00:24:ff:2f:ee:2c 
    # As you can see above firist coloum should be host name and seperated bye pwwn_Switch_side_A, they can be seperated bye 
    ### either , or sigle or multiple spaces like tab. Followed by pwwn that is connected to on swithc B, followed by FA on switch side A,FA to be zoned on switch side B
    ### If it is for a cluster you have to give y or for single host n. Followed by serial no
    #############################################################################
    my $cluster;
    my $ip_file=shift @ARGV;
    my $temp_file="/export/home/pjinde/Pradeep/temp_file";
    my $op_file=shift @ARGV;
    my $host_name;
    my $symm_id;
    my %host_details;
    my $host_count;
    my $loc_host_last;
    my $leng_host;
    my $loc_three_letters;
    my @FA_CX_A;
    my @FA_CX_B;
    my $pg;
    my $policy;
    
    ############################################################################
    ### Deleting tempfile if exists
    ############################################################################
    if(-f $temp_file)
    {
      `rm -f /export/home/pjinde/Pradeep/temp_file`;
    }
    
    #########################################################################
    ## Checking the iput file and the command input
    #########################################################################
    if ($ip_file eq ""or $temp_file eq "" or $op_file eq "")
    {
     print "IN ORDER TO EXECUTE THIS PERL PROGRAM YOU HAVE TO ENTER THE COMMAND IN THE FILLOWING FORMAT
    ***************************************************************************************************
    ./command_geneartor.pl INPUTFILE OUTPUT_FILE_LOCATION
    EG:./command_generator.pl sigle_host_ip /var/tmp/Pradeep/EXECUTE 
    *************************************************************************************************** \n";
    die;
    }
    open ("IP_F", "$ip_file") or die "unable to open file $ip_file please check the input file \n";
    while(<IP_F>)
    {
     #print "$_ \n";
    
     if(/[Cc][Ll][Uu][Ss][Tt][Ee][Rr]=(.*)/)
     {
       my $value=$1;
     # print "$value \n";
       if($value !~ /^[YyNn][Ee]?[Ss]?[Oo]?/)
       {
          print "Cluster vlaue is $value. In order to run the program successfully you have to enter Yes or No \n";
       }
    }
    }
    close(IP_F);
    #my @host_names;
    #print "$ip_file\n";
    open ("IP", "$ip_file") or die "unable to open the file $!\n";
    open ("TEMP", ">> $temp_file");
    while (<IP>)
    {
    # print "$_\n";
     tr/[a-z]/[A-Z]/;
     if(/CLUSTER=(\w+)/)
     {
     $cluster=$1;
     # print "I am trying to print cluster vlalue $cluster \n";
     }
    
     if(/SYMM.*=(\d+)/)
     {
     $symm_id=$1;
     #print "$symm_id \n";
     }
      if(/PORT_GROUP=(.*)/)
      {
        $pg=$1;
        #print "port group $pg \n";
      }
      if(/POLICY=(.*)/)
      {
        $policy=$1;
        #print "port group $pg \n";
      }
    
    
    
     # @FA_CX_A=0;
      if(/^FA_A=(.*)/)
      {
        tr/[a-z]/[A-Z]/;
        my $temp=$1;
        @FA_CX_A=split (/,/, $temp);
      }
     # @FA_CX_B=0;
      if(/^FA_B=(.*)/)
      {
       tr/[a-z]/[A-Z]/;
       my $temp=$1;
       @FA_CX_B=split(/,/, $temp);
      }
     foreach (@FA_CX_A,@FA_CX_B)
     {
       $_=~s/[\s:]*//g;
      if (!length($_)== 16)
      {
       print " WWN $_ is not 16 digit's please check \n";
       die;
      }
      }## forecah for 
     if(!/^(FA|CLUS|SYMM)/)
     {
      #print "$_ \n";
     if(/(\w+)[,\s]+(\w+[\s:]*\w+[\s:]*\w+[\s:]*\w+[\s:]*\w+[\s:]*\w+[\s:]*\w+[\s:]*\w+)[,\s]+(\w+[\s:]*\w+[\s:]*\w+[\s:]*\w+[\s:]*\w+[\s:]*\w+[\s:]*\w+[\s:]*\w+)/)
     {
     #  print "$_";
     $host_name=$1;
     my $pwwn_A=$2;
     my $pwwn_B=$3;
     $pwwn_A=~s/[\s:]*//g;
     $pwwn_B=~s/[\s:]*//g;
     push (@$host_name, ($symm_id,$cluster,$pwwn_A,$pwwn_B));
     print " symm_id=$symm_id cluster=$cluster \$1 value=$host_name, \$2 value=$pwwn_A,\$3 value=$pwwn_B,\$4 value=@FA_CX_A,\$5 value=@FA_CX_B \n";
     $host_details{$host_name}=[ @$host_name ];
     }
     } ### If ending
     } ## Main while
    foreach (sort keys %host_details)
    {
      switch_EXECUTE($_);
      vmax_allocation($_);
       print TEMP" ###################################### \n";
    }
     switch ($cluster)
     {
     case /^[Nn][Oo]?/
     {
    #  print "I am in no cluster \n";
    
      } ## END of NO 
    
    
    ####################################################################
    ## Cluster Host
    ####################################################################
     case /^[Yy][Ee]?[Ss]?/ 
     {
      my @loc_keys=sort keys %host_details;
      $host_count=$#loc_keys;
      $loc_host_last=${loc_keys[$host_count]};
      $leng_host=length($loc_host_last);
      $loc_three_letters=substr($loc_host_last,$leng_host-3,3);
     #print "$loc_host_last,$leng_host,$loc_three_letters \n";
     print TEMP"symaccess -sid $symm_id create -type initi -name ${loc_keys[0]}-${loc_three_letters}_INITGRP -ig ${loc_keys[0]}_INITGRP -consistent_lun \n";
     for( my $i=1;$i<=$host_count;$i++)
     {
        print TEMP"symaccess -sid $symm_id -type initi -name ${loc_keys[0]}-${loc_three_letters}_INITGRP add -ig ${loc_keys[$i]}_INITGRP \n";
     }
      print TEMP "symaccess -sid $symm_id list assign -devs \n";
      print TEMP "symaccess -sid $symm_id -type storage list -devs \n";
      print TEMP"symaccess -sid $symm_id create -type storage -name ${loc_keys[0]}-${loc_three_letters}_FAST_${policy}_SGRP -devs \n ";
      print TEMP"symfast -sid $symm_id associate -sg ${loc_keys[0]}-${loc_three_letters}_FAST_${policy}_SGRP -fp_name ${policy} -priority 2 \n";
      print TEMP"symaccess -sid $symm_id create view -name ${loc_keys[0]}-${loc_three_letters}_FAST_${policy}_VIEW -sg ${loc_keys[0]}-${loc_three_letters}_FAST_${policy}_SGRP -ig ${loc_keys[0]}-${loc_three_letters}_INITGRP -pg $pg \n ";
      print TEMP "symaccess -sid $symm_id show view ${loc_keys[0]}-${loc_three_letters}_FAST_${policy}_VIEW \n";
      print TEMP "symaccess -sid $symm_id list -type storage devs `symaccess -sid $symm_id show view ${loc_keys[0]}-${loc_three_letters}_FAST_${policy}_VIEW |awk '/^[0-9]/ {print \$1}'|xargs|sed 's/ /,/g'` \n";
      print TEMP "symaccess -sid $symm_id show view ${loc_keys[0]}-${loc_three_letters}_FAST_${policy}_VIEW |awk '/^[0-9]/ {if(NF==6) print \$1,\$6;else if(NF==7) print \$1,\$7;} /Storage Group Name/ {print \$0;}' \n;"
     } ## case ending
     } ## switch
    
    #foreach (sort keys %host_details)
    #jj{
    #  print TEMP"host name $_, and it's values @{$host_details{$_}} \n";
    #}
    close (IP);
    
    sub switch_EXECUTE
    {
     my $loc_host_name=shift @_;
     my $loc_symm_id=$host_details{$loc_host_name}[0];
     my $loc_pwwn_A=$host_details{$loc_host_name}[2];
     my $loc_pwwn_B=$host_details{$loc_host_name}[3];
     my @loc_FA_A=@FA_CX_A;
     my @loc_FA_B=@FA_CX_B;
     my $loc_node_value;
     #print "$loc_symm_id,$loc_host_name,$loc_pwwn_A,$loc_pwwn_B i am in switch sub \n ";
     print TEMP"###################################################################
    ############################ Commands for Switch A ######################### \n";
     $loc_pwwn_A=~ s/(..)(..)(..)(..)(..)(..)(..)(..)/$1:$2:$3:$4:$5:$6:$7:$8/;
     $loc_pwwn_B=~ s/(..)(..)(..)(..)(..)(..)(..)(..)/$1:$2:$3:$4:$5:$6:$7:$8/;
    # print "$loc_symm_id,$loc_pwwn_A,$loc_pwwn_B i am in switch sub \n ";
     #print TEMP"$loc_pwwn_A,$loc_pwwn_B \n";
     print TEMP"alicreate \"${loc_host_name}_A\", \"$loc_pwwn_A\" \n";
    foreach (@loc_FA_A) 
    {
     #print " $_ is FA values on switch \n";
     my $loc_FA=$_;
    # print "$loc_FA \n";
      if(/(\d\d)\w+/)
      {
      $loc_node_value=$1;
     # print "$loc_node_value fa value \n";
      }
      if($loc_node_value==05)
       {
         $node=NODE3;
     #   print "$node value \n";
       }
      elsif($loc_node_value==07)
      {
         $node=NODE4;
      }
      elsif($loc_node_value==9)
      {
         $node=NODE5;
       }
      elsif($loc_node_value==11)
      {
         $node=NODE6;
         #print " $node \n";
      }
    #if(/(\d\d)\w+/)
    # {
    #  $loc_node_value=$1;
    #  #print " vlaue of FA in switch B $loc_node_value \n";
    # }
      if($loc_node_value==06)
       {
         $node=NODE3;
         #print " $node \n";
       }
      elsif($loc_node_value==8)
      {
         $node=NODE4;
      }
      elsif($loc_node_value==10)
      {
         $node=NODE5;
       }
      elsif($loc_node_value==12)
      {
         $node=NODE6;
      }
    
    # print "zonecreate \"${loc_host_name}_A_VMAX_${loc_symm_id}_${node}_FA${loc_FA}\", \"${loc_host_name}_A;FA${loc_FA}_${node}_${symm_id}\"\n";
     print TEMP"zonecreate \"${loc_host_name}_A_VMAX_${loc_symm_id}_${node}_FA${loc_FA}\", \"${loc_host_name}_A;FA${loc_FA}_${node}_${symm_id}\"\n";
     print TEMP"cfgadd \"aaaa\", \"${loc_host_name}_A_VMAX_${loc_symm_id}_${node}_FA${loc_FA}\" \n";
    }
     print TEMP"cfgsave \n";
     print TEMP"cfgenable \"aaaa\" \n"; 
     print TEMP"######################################################################
    ##################### Commands for Switch B #################################  \n";
     print TEMP"alicreate \"${loc_host_name}_B\", \"$loc_pwwn_B\" \n";
    foreach (@loc_FA_B) 
    {
    # print " $_ is FA values on switch \n";
     my $loc_FA=$_;
    # print "$loc_FA \n";
      if(/(\d\d)\w+/)
      {
      $loc_node_value=$1;
     # print "$loc_node_value fa value \n";
      }
      if($loc_node_value==05)
       {
         $node=NODE3;
     #   print "$node value \n";
       }
      elsif($loc_node_value==07)
      {
         $node=NODE4;
      }
      elsif($loc_node_value==9)
      {
         $node=NODE5;
       }
      elsif($loc_node_value==11)
      {
         $node=NODE6;
         #print " $node \n";
      }
    #if(/(\d\d)\w+/)
    # {
    #  $loc_node_value=$1;
    #  #print " vlaue of FA in switch B $loc_node_value \n";
    # }
      if($loc_node_value==06)
       {
         $node=NODE3;
         #print " $node \n";
       }
      elsif($loc_node_value==8)
      {
         $node=NODE4;
      }
      elsif($loc_node_value==10)
      {
         $node=NODE5;
       }
      elsif($loc_node_value==12)
      {
         $node=NODE6;
      }
    # print "zonecreate \"${loc_host_name}_B_VMAX_${loc_symm_id}_${node}_FA${loc_FA}\", \"${loc_host_name}_B;FA${loc_FA}_${node}_${symm_id}\"\n";
     print TEMP"zonecreate \"${loc_host_name}_B_VMAX_${loc_symm_id}_${node}_FA${loc_FA}\", \"${loc_host_name}_B;FA${loc_FA}_${node}_${symm_id}\"\n";
     print TEMP"cfgadd \"bbbb\", \"${loc_host_name}_B_VMAX_${loc_symm_id}_${node}_FA${loc_FA}\"\n";
    } ### End of foreach loop for Switch Side B
     print TEMP"cfgsave \n";
     print TEMP"cfgenable \"bbbb\" \n";
    }
    
    sub vmax_allocation
    {
     my $loc_host_name=shift @_;
     my $loc_symm_id=$host_details{$loc_host_name}[0];
     my $loc_pwwn_A=$host_details{$loc_host_name}[2];
     my $loc_pwwn_B=$host_details{$loc_host_name}[3];
     #my $loc_FA_A= shift @FA_CX_A;
     #my $loc_FA_B= shift @FA_CX_B;
    #print " $loc_symm_id $loc_host_name $loc_pwwn_A $loc_pwwn_B @FA_CX_A @FA_CX_B \n";
    print TEMP"symaccess -sid $loc_symm_id rename -wwn $loc_pwwn_A -alias $loc_host_name/FA_VALUE_A \n";
    print TEMP"symaccess -sid $loc_symm_id rename -wwn $loc_pwwn_B -alias $loc_host_name/FA_VALUE_B \n";
    print TEMP"symaccess -sid $loc_symm_id list logins -wwn $loc_pwwn_A \n";
    print TEMP"symaccess -sid $loc_symm_id list logins -wwn $loc_pwwn_B \n";
    print TEMP"symaccess -sid $loc_symm_id create -type initi -name ${loc_host_name}_INITGRP -wwn $loc_pwwn_A -consistent_lun \n";
    print TEMP"symaccess -sid $loc_symm_id -type initi -name ${loc_host_name}_INITGRP add -wwn $loc_pwwn_B \n";
    print TEMP"symaccess -sid $loc_symm_id list assign -devs \n"; 
    print TEMP"symaccess -sid $loc_symm_id -type storage list -devs \n";
    print TEMP"symaccess -sid $loc_symm_id create -type storage -name ${loc_host_name}_FAST_${policy}_SGRP -devs \n";
    print TEMP"symfast -sid $symm_id associate -sg ${loc_host_name}_FAST_${policy}_SGRP -fp_name ${policy} -priority 2 \n";
    print TEMP"symfast -sid $symm_id list -association|grep -i ${loc_host_name}_FAST_${policy}_SGRP \n";
    print TEMP"symaccess -sid $loc_symm_id create view -name ${loc_host_name}_FAST_${policy}_VIEW -ig ${loc_host_name}_INITGRP -sg ${loc_host_name}_FAST_${policy}_SGRP -pg $pg \n";
    print TEMP "symaccess -sid $loc_symm_id show view ${loc_host_name}_FAST_${policy}_VIEW \n";
    print TEMP "symaccess -sid $loc_symm_id list -type storage devs `symaccess -sid $loc_symm_id show view ${loc_host_name}_FAST_${policy}_VIEW|awk '/^[0-9]/ {print \$1}'|xargs|sed 's/ /,/g'` \n";
    #print TEMP "symaccess -sid $loc_symm_id list -type storage devs `symaccess -sid $loc_symm_id show view ${loc_host_name}_FAST_${policy}_VIEW|awk '/^[0-9]/ {print \$1}'|xargs|sed 's/ /,/g'`|awk '/^[0-9]/ {print \$1,\$2}'|sed 's/_.*//g' \n";
    print TEMP "symaccess -sid $loc_symm_id show view ${loc_host_name}_FAST_${policy}_VIEW|awk '/^[0-9]/ {if(NF==6) print \$1,\$6;else if(NF==7) print \$1,\$7;} /Storage Group Name/ {print \$0;}' \n;"
    }
    close (TEMP);
    
    `cat $temp_file |grep "alicreate"|grep "_A" >$op_file` ;
    `cat $temp_file |grep "zonecreate"|grep "_A">> $op_file` ;
    `cat $temp_file |grep "cfgadd"|grep "_A" >>$op_file`;
    `echo "cfgsave">>$op_file`;
    `echo "cfgenable \"aaaa\" ">>$op_file`;
    `echo "############################################################################## ">> $op_file`;
    `cat $temp_file |grep "cfgenable \"aaaa\"">> $op_file`;
    `cat $temp_file |grep "alicreate"|grep "_B" >>$op_file` ;
    `cat $temp_file |grep "zonecreate"|grep "_B">> $op_file` ;
    `cat $temp_file |grep "cfgadd"|grep "_B" >>$op_file`;
    `echo "cfgsave">>$op_file`;
    `echo "cfgenable \"bbbb\" ">>$op_file`;
    `echo "############################################################################## ">> $op_file`;
    `cat $temp_file|egrep "rename \-wwn|list logins|-type initi|\add -ig" >> $op_file`;
    `cat $temp_file|egrep "list assign |create -type storage|-type storage list -devs|associate" >> $op_file`;
    # if($cluster=~/[Yy][Ee]?[Ss]?/)
    # {
       `cat $temp_file|egrep "create view" >> $op_file`;
       `cat $temp_file|egrep "list assign |association|show view|-type storage devs|storage list -devs" >> $op_file`;
    # }
    close($op_file);

    Thanks in advance...
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,940
    Rep Power
    1225
    Your first step should be to pick up a copy of Perl Best Practices and thoroughly read it. It will teach you a lot of the basics that you're missing and will need before writing OO scripts.

    Next step is a toss up between fixing your current script based on the knowledge you learned from the PBP book or to first read Modern Perl before rewriting/fixing your script. Modern Perl will introduce you to OO, but there are many other things in there that you should learn before doing OO.

    The Switch module that you're using is a source filter module and is known to be buggy and is not recommended to be used in production code.

    Likewise, (Larry Wall) the author of the Shell module does not recommend using that module in production code.

    Your script is shelling out way too much and using backticks in void context is also a poor practice/habit.

    Your script is lacking the usage of the strict pragma, which is not good to leave out and it's using the global -w switch instead of the preferred lexical use warnings; pragma.
    Last edited by FishMonger; January 5th, 2014 at 12:29 AM.

IMN logo majestic logo threadwatch logo seochat tools logo