Use the same perl script with several different variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use the same perl script with several different variables
# 1  
Old 10-02-2009
Use the same perl script with several different variables

I'm new to perl and have to create these files for my job. I have a script that I can run that will create the file, but I have to manually pass the variables to it. It is dumb and it takes a lot of time. I want to be able to pass the existing perl script a set of variables (the same variables with different values) over and over again, until it runs through them.

Basically, I want to keep a file with the variables:

$variable1
$variable2
$variable3
run script with these > output to a file

$variable1
$variable2
$variable3
run script with these > output to a file

Anyone willing to give a newbie some help?

Thanks
# 2  
Old 10-02-2009
so say you put your variables in space-delimited lines in a file name 'in.txt'. The content of this file for example is:
Code:
abc def ghi
jkl mno pqr

you could invoke your script 'script.pl' like this:
Code:
script.pl in.txt out.txt

as you may know, in.txt is passed into the script in the default array ARGV.

inside your script, you would have something like this, then, to create file handles to open the input file, and a second arg for the output file

Code:
open FHIN,"<$ARGV[0]";
open FHOUT, ">$ARGV[1]";

then you would read the input file line by line, splitting each line on spaces into an array, to pass to your processing function for output:

Code:
#!/usr/bin/perl

# the function you will execute with your vars
sub proc_array
{
   # open the output filehandle
   open FHOUT, ">ARGV[1]";
   #iterate over the array you pass to the function
   foreach my $var (@_)
   {
          # here it just prints the value and a newline
          # but you would be doing something more interesting
	  print FHOUT $var."\n"
   }	
   # close the output filehandle
   close FHOUT;
}

#open the file handle
open FHIN, $ARGV[0];

#iterate over each line in the file
foreach my $line (<FHIN>)
{
        # split the space-delimited var strings and store each var in an array
	my @vars = split(/\s/,$line);
        # pass the array to your processing function
	proc_array(@vars);
}
# close the filehandle when done
close FHIN;

the output of the script in this example is:
Code:
abc
def
ghi
jkl
mno
pqr

if you want to output to multiple files, you could either pass in multiple args, or name your file with timestamps or something else programmatically.

Last edited by varontron; 10-03-2009 at 12:27 AM..
# 3  
Old 10-06-2009
ok...still need some help

I've tested this and it works, but I'm not able to put variables in my input file and have them read as variables. I'm not sure if that makes sense, but if I declare my variables in my input file, the other script doesn't recognize them as a variable, only a string.
# 4  
Old 10-06-2009
perhaps you can post your files?

If you're declaring variables in the first script and then calling the second from it, you need to use some sort of persistent memory, which is probably way overcomplicated. the second perl script has no knowledge of the memory used by it's caller.

alternatively you want to declare vars in the second script to hold the vals you need, and pass those values from the first script to it. you can do this through arguments, pipes, files, etc.
# 5  
Old 10-06-2009
May be you'd like to set and use envinonment variable?
Code:
bash-2.05$ export aaa='bla bla bla'
bash-2.05$ perl -e 'print "$ENV{aaa}\n"'
bla bla bla
bash-2.05$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

PERL: In a perl-scripttTrying to execute another perl-script that SETS SOME VARIABLES !

I have reviewed many examples on-line about running another process (either PERL or shell command or a program), but do not find any usefull for my needs way. (Reviewed and not useful the system(), 'back ticks', exec() and open()) I would like to run another PERL-script from first one, not... (1 Reply)
Discussion started by: alex_5161
1 Replies

2. UNIX for Dummies Questions & Answers

Variables in perl script

Hi Chaps, Im after some advise with a script i've written however doesnt appear to work how I would like. Basically I have a perl script which sucessfully pulls an expect script to login to multiple nodes and obtain some output from the nodes (total number of nat ports that are in use... (0 Replies)
Discussion started by: mutley2202
0 Replies

3. Shell Programming and Scripting

Excuting perl script from within a perl script with variables.

Not sure what I am doing wrong here, but I can print the list with no issue. Just a blank screen with the 'do'. #!/usr/bin/perl open FILE, "upslist.txt"; while ($line=<FILE>){ if ($line=~/^(.*?),(.*?)$/){ #print "ups:$1 string:$2\n"; do 'check_snmp_mgeups-0.1.pl -H $1 -C $2'; } ... (1 Reply)
Discussion started by: mrlayance
1 Replies

4. Shell Programming and Scripting

Running a script with multiple variables like 25 variables.

Hi All, i have a requirement where i have to run a script with at least 25 arguements and position of arguements can also change. the unapropriate way is like below. can we achieve this in more good and precise way?? #!/bin/ksh ##script is sample.ksh age=$1 gender=$2 class=$3 . . .... (3 Replies)
Discussion started by: Lakshman_Gupta
3 Replies

5. Shell Programming and Scripting

Variables in perl

I don't fully understand variables in perl. If we have a variable defined like this "my $number = 1" then this is called a lexical variable? But if you define this at the top of a script then why isn't it a global variable because it would be available throughout the file? Sorry if this is... (1 Reply)
Discussion started by: P3rl
1 Replies

6. Shell Programming and Scripting

using variables in perl not working

sdcprd@dotstoas110:$ echo $F2 1327332411 this works ---------- sdcprd@dotstoas110:$ perl -MPOSIX -le 'print strftime ("%m%d%y",localtime (1327332411))' 012312 <<<< correct date this doesnt ----------- sdcprd@dotstoas110:$ perl -MPOSIX -le 'print strftime ("%m%d%y",localtime... (10 Replies)
Discussion started by: aliyesami
10 Replies

7. Shell Programming and Scripting

pass perl variables to shell script

I have a perl script that opens a text file containing numbers on each line: for example: 755993 755994 755995 755996 755997 755998 The perl script takes these numbers and store them as an array @raw_data, where I can access individual numbers by using $raw_data for the value 755993.... (2 Replies)
Discussion started by: xchen89x
2 Replies

8. Shell Programming and Scripting

Comparing Variables in Perl

Hi. I have three arrays. @a=('AB','CD','EF'); @b=('AB,'DG',HK'); @c=('DD','TT','MM'); I want to compare the elements of the first two array and if they match then so some substition. I tried using the if statement using the scalar value of the array but its not giving me any output. ... (7 Replies)
Discussion started by: kamitsin
7 Replies

9. Shell Programming and Scripting

Need help passing variables in shell script to perl one-liner

I'm writing a script to automate some post-install tasks on RHEL4 servers. I need the following code to insert an 'A' in the middle of a string, then replace the string in a file. I know I can use sed to do this, but I'd like to use perl's in place edit so I don't have to write to a temp file,... (1 Reply)
Discussion started by: Xek
1 Replies

10. Shell Programming and Scripting

accessing variables declared in another perl script

Hi all, I have a perl script which declares two variables and calls another perl script which accesses those variables. But I am unable to access the variables in the called script. My script is as follows: my $ENV{a}="20"; system("perl called.pl"); and my called.pl contains: print... (3 Replies)
Discussion started by: gurukottur
3 Replies
Login or Register to Ask a Question