Unix/Linux Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Variable Scope in Perl

Programming


Tags
perl, perl array, scope

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-14-2015   -   Original Discussion by bakunin
bakunin's Unix or Linux Image
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 14 December 2017, 5:06 PM EST
Location: In the leftmost byte of /dev/kmem
Posts: 5,647
Thanks: 109
Thanked 1,613 Times in 1,184 Posts
Variable Scope in Perl

I have to admit that i have not used Perl at all and this is a singular occasion where i have to patch an existing Perl script. I dearly hope i do not have to do it again for the next 15 years and therefore try to avoid having to learn the programming language in earnest.

The OS is AIX 7.1, the Perl Version is 5.01.

I have a script using a (user-written) library:


Code:
#!/usr/bin/perl

use warnings;
use strict;

use v5.10.1;    # state, say, given ...
use Vmax;

[...]

In the mentioned library "Vmax" there are some constants (well, at least they look to be constants for my untrained eye) declared. These are used further on in some subfunctions:


Code:
use v5.10.1;
use strict;
use warnings;

my %pg = ( $SIDS[0] => 'stringA',
           $SIDS[1] => 'stringB' );

[...]
sub work_pg
{
     [...]

     for my $sid (@SIDS)
     {
          say "INFO: do something with $pg{$sid}";
          my @cmd = ( 'command', [...], $pg{$sid}, [...] )
          RunCmd \@cmd, \$in, \$out, \$err, $LONG;
     }
}

This all works well. The "pg" array above represents port groups in two EMC VMax storage systems. As we added new port groups now i need to introduce a switch to the main program to select one set of port groups instead of using these two. Something like (pseudo-code):


Code:
case condition in
1) 
     my %pg = ( $SIDS[0] => 'stringA',
                $SIDS[1] => 'stringB' );

2) 
     my %pg = ( $SIDS[0] => 'stringC',
                $SIDS[1] => 'stringD' );

[...]
end case

Now i have already found out how to add a command line option to the main script but i am not sure how to pass this option to the library and how to tackle the problem with selecting the right set of array values depending on the options value.

I'd like to have the script to be called like this:


Code:
script -foo -bar ... -pg [1|2|3...]

and then select the corresponding value sets for pg[] in the called lib.

Thanks for your help.

bakunin
Sponsored Links
    #2  
Old Unix and Linux 10-16-2015   -   Original Discussion by bakunin
balajesuri's Unix or Linux Image
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 8 November 2017, 2:32 AM EST
Location: India
Posts: 1,976
Thanks: 31
Thanked 574 Times in 547 Posts
1. There is no switch case feature in perl by default. So you may have to use if-elsif-else blocks. Or you could install the Switch.pm module and use switch case.

2. To pass option to Vmax lib:

in main.pl:


Code:
use Vmax;
[...]

my $option = <option_from_command_line>;

Vmax::set_option($option);
Vmax::work_pg(<args>);
[...]

in Vmax.pm:


Code:
my $option = "";
sub set_option {
    $option = shift;
}

if ($option == 1) {
    %pg = ( ... );
}
elsif ($option == 2) {
    %pg = ( ... );
}
[...]

sub work_pg {
    [...]
}

Please correct me if you find a better method. Thanks.
The Following User Says Thank You to balajesuri For This Useful Post:
bakunin (12-04-2015)
Sponsored Links
    #3  
Old Unix and Linux 10-16-2015   -   Original Discussion by bakunin
Aia's Unix or Linux Image
Aia Aia is offline
Registered User
 
Join Date: May 2008
Last Activity: 20 November 2017, 9:27 AM EST
Posts: 1,673
Thanks: 49
Thanked 641 Times in 601 Posts
Since Perl 5.10 is being used, the given statement can do the work of a switch.


Code:
given($option) {
   when(1) { %pg = (...)}
   when(2) { %pg = (...)}
   when(3) { %pg = (...)}
   default { say "Unexpected value given"; }
}

The Following User Says Thank You to Aia For This Useful Post:
bakunin (12-04-2015)
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Variable scope in bash haaru Shell Programming and Scripting 3 01-11-2012 02:24 PM
[SOLVED] Scope of KSH Variable in Perl? dahlia84 Shell Programming and Scripting 1 07-30-2010 12:48 PM
variable scope shellwell Shell Programming and Scripting 3 07-15-2009 12:38 PM
scope of the variable - Naga nagnatar Shell Programming and Scripting 13 04-27-2009 03:26 PM
C++ variable scope and mutexes Corona688 Programming 0 10-05-2005 02:29 PM



All times are GMT -4. The time now is 08:30 PM.