Sponsored Content
Full Discussion: Variable Scope in Perl
Top Forums Programming Variable Scope in Perl Post 302957701 by bakunin on Wednesday 14th of October 2015 05:27:07 AM
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
 

10 More Discussions You Might Find Interesting

1. Programming

C++ variable scope and mutexes

I've been wondering if I can make mutexes much easier to use in C++ with creative use of a locking class and variable scope, but I'm not sure if things happen in the order I want. Here's pseudocode for something that could use the class: int someclass::getvalue() { int retval; ... (0 Replies)
Discussion started by: Corona688
0 Replies

2. Shell Programming and Scripting

problem with shell variable's scope

Hi, I am stuck while developing a shell sub-routine which checks the log file for "success" or "failure". The subroutine reads the log file and checks for key word "success", if found it set the variable (found=1). It returns success or failure based on this variable. My problem is, I can... (2 Replies)
Discussion started by: cjjoy
2 Replies

3. Shell Programming and Scripting

scope of the variable - Naga

Hi All, I am new to unix shell scripting, in the below script "num" is an input file which contains a series of numbers example : 2 3 5 8 I want to add the above all numbers and want the result finally outside the while loop. it prints the value zero instead of the actual expected... (13 Replies)
Discussion started by: nagnatar
13 Replies

4. Shell Programming and Scripting

variable scope

Hi, I want to know about the variable scope in shell script. How can we use the script argument inside the function? fn () { echo $1 ## I want this argument should be the main script argument and not the funtion argument. } also are there any local,global types in shell script? if... (3 Replies)
Discussion started by: shellwell
3 Replies

5. Shell Programming and Scripting

[SOLVED] Scope of KSH Variable in Perl?

cat test.ksh #!/usr/bin/ksh VAR="Dear Friends \n How are you? \n Have a nice day \n" export VAR echo "Inside test.ksh"; ./test.pl cat test.pl #!/usr/bin/perl print "Inside test.pl \n"; print "$VAR"; Output: ./test.ksh Inside test.ksh Inside test.plWhat I want to achieve is, I... (1 Reply)
Discussion started by: dahlia84
1 Replies

6. UNIX for Dummies Questions & Answers

Bash loops and variable scope

Hi All, I've been researching this problem and I am pretty sure that the issue is related to the while loop and the piping. There are plenty of other threads about this issue that recommend removing the pipe and using redirection. However, I haven't been able to get it working using the ssh and... (1 Reply)
Discussion started by: 1skydive
1 Replies

7. Shell Programming and Scripting

Help with retaining variable scope

Hi, I use Korn Shell. Searched Forum and modified the way the file is input to the while loop, but still the variable does not seem to be retaining the final count. while read name do Tmp=`echo $name | awk '{print $9 }'` Count=`cat $Tmp | wc -l`... (6 Replies)
Discussion started by: justchill
6 Replies

8. Shell Programming and Scripting

Variable scope in bash

Hello! Before you "bash" me with - Not another post of this kind Please read on and you will understand my problem... I am using the below to extract a sum of the diskIO on a Solaris server. #!/bin/sh PATH=/usr/bin:/usr/sbin:/sbin; export PATH TEMP1="/tmp/raw-sar-output.txt$$"... (3 Replies)
Discussion started by: haaru
3 Replies

9. Shell Programming and Scripting

Maintain Scope of the variable in UNIX

Hi All Is there is any way to maintain the scope of the variable in unix Example x=1 j=1 while do .. .... .... while do .. .. x=x+1 done #inner most while loop ends here done #outer loop ends here (8 Replies)
Discussion started by: parthmittal2007
8 Replies

10. Shell Programming and Scripting

PERL annoying scope problem

Hello, I met a problem with following code: #!/usr/bin/perl -w # test.pl use strict; use diagnostics; use DBI; my $dbh = DBI->connect( "DBI:mysql:BibleBook","yifangt","password") or die("Cannot connect: $DBI::errstr"); my $sql = qq(SELECT * FROM library WHERE isbn =... (2 Replies)
Discussion started by: yifangt
2 Replies
Test::TCP(3pm)						User Contributed Perl Documentation					    Test::TCP(3pm)

NAME
Test::TCP - testing TCP program SYNOPSIS
use Test::TCP; my $server = Test::TCP->new( code => sub { my $port = shift; ... }, ); my $client = MyClient->new(host => '127.0.0.1', port => $server->port); undef $server; # kill child process on DESTROY Using memcached: use Test::TCP; my $memcached = Test::TCP->new( code => sub { my $port = shift; exec $bin, '-p' => $port; die "cannot execute $bin: $!"; }, ); my $memd = Cache::Memcached->new({servers => ['127.0.0.1:' . $memcached->port]}); ... And functional interface is available: use Test::TCP; test_tcp( client => sub { my ($port, $server_pid) = @_; # send request to the server }, server => sub { my $port = shift; # run server }, ); DESCRIPTION
Test::TCP is test utilities for TCP/IP programs. METHODS
empty_port my $port = empty_port(); Get the available port number, you can use. test_tcp Functional interface. test_tcp( client => sub { my $port = shift; # send request to the server }, server => sub { my $port = shift; # run server }, # optional port => 8080 ); wait_port wait_port(8080); Waits for a particular port is available for connect. OO-ish interface my $server = Test::TCP->new(%args); Create new instance of Test::TCP. Arguments are following: $args{auto_start}: Boolean Call "$server->start()" after create instance. Default: true $args{code}: CodeRef The callback function. Argument for callback function is: "$code->($pid)". This parameter is required. $server->start() Start the server process. Normally, you don't need to call this method. $server->stop() Stop the server process. my $pid = $server->pid(); Get the pid of child process. my $port = $server->port(); Get the port number of child process. FAQ
How to invoke two servers? You can call test_tcp() twice! test_tcp( client => sub { my $port1 = shift; test_tcp( client => sub { my $port2 = shift; # some client code here }, server => sub { my $port2 = shift; # some server2 code here }, ); }, server => sub { my $port1 = shift; # some server1 code here }, ); Or use OO-ish interface instead. my $server1 = Test::TCP->new(code => sub { my $port1 = shift; ... }); my $server2 = Test::TCP->new(code => sub { my $port2 = shift; ... }); # your client code here. ... How do you test server program written in other languages like memcached? You can use "exec()" in child process. use strict; use warnings; use utf8; use Test::More; use Test::TCP 1.08; use File::Which; my $bin = scalar which 'memcached'; plan skip_all => 'memcached binary is not found' unless defined $bin; my $memcached = Test::TCP->new( code => sub { my $port = shift; exec $bin, '-p' => $port; die "cannot execute $bin: $!"; }, ); use Cache::Memcached; my $memd = Cache::Memcached->new({servers => ['127.0.0.1:' . $memcached->port]}); $memd->set(foo => 'bar'); is $memd->get('foo'), 'bar'; done_testing; AUTHOR
Tokuhiro Matsuno <tokuhirom@gmail.com> THANKS TO
kazuhooku dragon3 charsbar Tatsuhiko Miyagawa lestrrat SEE ALSO
LICENSE
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.14.2 2012-02-01 Test::TCP(3pm)
All times are GMT -4. The time now is 02:01 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy