Home Man
Search
Today's Posts
Register

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

Variable Scope in Perl

Tags
perl, perl array, programming, scope

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 10-14-2015
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
# 2  
Old 10-16-2015
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)
# 3  
Old 10-16-2015
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)
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
BASH: variable and function scope and subscripts rse Shell Programming and Scripting 6 10-07-2013 04:41 AM
Maintain Scope of the variable in UNIX parthmittal2007 Shell Programming and Scripting 8 06-25-2012 11:41 AM
Variable scope in bash haaru Shell Programming and Scripting 3 01-11-2012 01:24 PM
Help with retaining variable scope justchill Shell Programming and Scripting 6 09-16-2011 06:58 AM
Bash loops and variable scope 1skydive UNIX for Dummies Questions & Answers 1 03-11-2011 03:02 PM
[SOLVED] Scope of KSH Variable in Perl? dahlia84 Shell Programming and Scripting 1 07-30-2010 11:48 AM
variable scope shellwell Shell Programming and Scripting 3 07-15-2009 11:38 AM
scope of the variable - Naga nagnatar Shell Programming and Scripting 13 04-27-2009 02:26 PM
problem with shell variable's scope cjjoy Shell Programming and Scripting 2 01-04-2009 10:25 AM
C++ variable scope and mutexes Corona688 Programming 0 10-05-2005 01:29 PM


All times are GMT -4. The time now is 02:29 AM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password