• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 480
  • Last Modified:

Passing by parameter to Perl

Hi,
Ifrom Korn I call a perl script:

this works:

$SCRIPTPATH/bin/my_perl_script.pl $DATE $DIR/$FILENAME2 $FILEFLAG > $DOWNLOADDIR/$FILENAME2 $FILEFLAGMOB


in the perl script I do:

my $date = $ARGV[0];
my $filename = $ARGV[1];
my $flag = $ARGV[2];

I only read $date and $flag
I do modify $filename as follows;

   system "/usr/bin/mv $get_file*.xml.gz  $filename";

and this works fine.
 

Now I want to find a way to change the value of $flag ($ARG[2]) that I received and pass it back.

I tried the following in Korn but this did not work:

$SCRIPTPATH/bin/my_perl_script.pl $DATE $DIR/$FILENAME2 $FILEFLAG > $DOWNLOADDIR/$FILENAME2 $FILEFLAG


in perl:

   system "/usr/bin/mv $get_file*.xml.gz  $filename";
   system "/usr/bin/mv $success_flag $flag";

how can I change the value of $ARGV[2] and pass it back?

0
Johannne1
Asked:
Johannne1
  • 7
  • 7
1 Solution
 
arnoldCommented:
Variable references can consists of spaces which would mean you would have under those conditions more arguments that you expect.
You should have double quotes surrounding each argument.
Could you explain what it is your are trying to do?
rename ($filename,"newname");

a call to system() is ineficient when other methods are available.

Instead of using shell redirect to file (>)
taken in five arguments
$SCRIPTPATH/bin/my_perl_script.pl "$DATE" "$DIR/$FILENAME2" "$FILEFLAG" "$DOWNLOADDIR/$FILENAME2" "$FILEFLAG"
Then within your script you can do what you need which is unclear to where do you want to pass $ARGV[2] back to?
0
 
Johannne1Author Commented:

Hi Arnold,

I want to pass $DATE  as $ARGV[0] and I only read $DATe

I pass /dir/filename2 as $ARGV[1] and I want to change the contents of this file. What happens
in my perl is i am using ftps to get a file, everything is okay then at the end I move the file to
to filename2. If everything is not ikay then i do not move.

I want to pass $FILEFLAG as $ARGV[2] in the script I want to change the value of $FILEFLAG and pass it back.

I have a total of 3 arguements.

I will try the double quotes as you suggest and let you know.



$SCRIPTPATH/bin/my_perl_script.pl "$DATE" "$DOWNLOADDIR/$FILENAME2" "$FILEFLAG"

0
 
arnoldCommented:
I still have no idea what you mean.
$DATE presumably is defined either within the script or within the shell.
do you want ARGV[0] to literally be $DATE? in this case, you would need to escape "\$DATE" or pass the arguments as '$DATE'

If you want to access Evnironment variables from the perl script use $ENV{'DATE'} to access the $DATE in ARGV[0].

ref
http://oreilly.com/openbook/cgi/ch02_02.html
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Johannne1Author Commented:

Hi Arnold,
Don't seem to be able to change the value of FILEFLAG:


$SCRIPTPATH/bin/myperl_test.pl "$DATE" "$DOWNLOADDIR/$FILENAME2" "$FILEFLAG"


in my perl:
my $date = $ARGV[0];
my $filename = $ARGV[1];
my $flag = $ARGV[2];

#test hard code to 1
$flag = 1;

end perl

back to korn


if [ $FILEFLAG=0 ]; then
     logger "flag has value $FILEFLAG"
     exit 1
fi

result:

logger flag mobile has value 0


0
 
Johannne1Author Commented:

Hi Arnold,
I don't want to change $DATE and I don't want to change environment vars. I want to pass
a flag, change the value of the flag and pass it back as above. Is it clearer?
Johanne
0
 
arnoldCommented:
Note that items passed on the command line are values for perl that you then assign to variables ($flag) in this case.  Altering the value stored in $flag does not alter the variable that was used when this perl script was called.

To do that you have to do:
system "export FILEFLAG=newvalue";
0
 
arnoldCommented:
Oops, should have checked first, the above system export does not work.
tried looking
http://www.perlmonks.org/?node_id=684685

Instead of doing this piecemeal.
Could you outline what it is you want to do.

So far I got you have a korn shell script within which it calls a perl script and you want the perl script to change the value of $FILEFLAG which perl does not see since the shell using the > filename fileflag is stripping out.
If you want perl to see all the entries as arguments you need to escape the redirect (\>).

This will mean that the perl script has 5 arguments (0-4)
0
 
Johannne1Author Commented:
Hi Arnold,
So i would have to export in the perl script, right;
suppose I wanted to do a test, and hard code the $flag to one in the perl script;

:
my $flag = $ARGV[2];
:
system "export $flag=1";
right?
the FILEFLAG is the korn...
0
 
arnoldCommented:
You need to use the status output from the perl script as a way to recover the data in the shell
echo $?
for exit code
Alternatively, assign the output of perl to a variable.
0
 
Johannne1Author Commented:
Hi Arnold,
OK. I tried above export and the value did not change.
My korn scripts sends 3 arguements and my perl scripts receives 3 arguement.
The first argument is $date defined in Korn. In the perl i just receive it and change it.
The second argument is $filename2 I was able to change the value when I used the redirect.
The third argument was the $fileflag I was able to read in the perl but unable to change its value.
So I guess what you are saying is to use the redirect when calling if I want to change the value of the parameter...something like. is this right?


$SCRIPTPATH/bin/myperl_test.pl "$DATE" >"$DOWNLOADDIR/$FILENAME2" >"$FILEFLAG"
0
 
Johannne1Author Commented:
Hi Arnold,
I can't get it to work. I will try again tomorrow morning. Maybe as you sugges to put the output
of perl in a variable.  For your question above in the korn, I declare the FILEFLAG as 0 then
later (after calling the perl) I need to check its value. I also tried calling with \> but no luck.
Thanks for your help.

korn:
FILEFLAG=0
:
:

$SCRIPTPATH/bin/myperl_test.pl "$DATE" >\"$DOWNLOADDIR/$FILENAME2" >\"$FILEFLAG"


in my perl:
my $date = $ARGV[0];
my $filename = $ARGV[1];
my $flag = $ARGV[2];

#test hard code to 1
$flag = 1;

end perl

back to korn


if [ $FILEFLAG=0 ]; then
     logger "flag has value $FILEFLAG"
     exit 1
fi

FILEFLAG is still 0 even if hard coded to 1 in the perl.
0
 
arnoldCommented:
the double redirect makes no sense.
since all you really be doing is writing the data into the filename whose name is based on the value stored in $FILEFLAG so it will be 0, 1 etc.

Why not use the output from the perl script and the exit code to determine the new value for the $FILEFLAG?
i.e. perl_script '$argument" "$argument1" "$argument2" "$downloaddir/$FILENAME2" $FILEFLAG > /tmp/fileflagvalue
$filename=$ARGV[2];

open (OUTPUT, ">$filename") || die "Unable to open $filename for writing: $!\n";
now instead of doing print for the redirect in the shell to work, you would use print OUTPUT
you can then print "newfileflagvalue\n";
in the korn shell you will check the status of the perlscript execution
if [ "$?" = "0" ] ; then
  # execution was successful
   FILFLAG=`cat /tmpfileflagvalue`;
fi


0
 
Johannne1Author Commented:
Hi Arnold,
You are right the double re-direction does not make sense. Only 1 redirection can take place.
You are right that I am writing data into the filename based on FILELAG
It looks like a parameter s value  can change only if it is a file.

for your line :
open (OUTPUT, ">$filename") || die "Unable to open $filename for writing: $!\n";
I am already doing this in the perl.

The problem is that I dont want to add FILELAG in the file, because if the perl is successful
I want to keep the file contents as is, I do not want a flag inside the file contents.
So it looks like I would do a second file to hold the result.


for your line:
i.e. perl_script '$argument" "$argument1" "$argument2" "$downloaddir/$FILENAME2" $FILEFLAG > /tmp/fileflagvalue
$filename=$ARGV[2]

would become:
in korn:

$SCRIPTPATH/bin/myperl_test.pl "$DATE" "$DOWNLOADDIR/$FILENAME2" "$FILEFLAG" > /tmp/fileflagvalue


in my perl:
my $date = $ARGV[0];
my $filename = $ARGV[1];
my $flagfile = $ARGV[2];

open (OUTPUT, ">$filename") || die "Unable to open $filename for writing: $!\n";
open (OUTPUT2, ">$flagfile") || die "Unable to open $flagfile for writing: $!\n";
:
:
print OUTPUT2 "newfileflagvalue\n";



in korn I would do:

if [ "$?" = "0" ] ; then
  # execution was successful
   FILFLAG=`cat /tmpfileflagvalue`;
fi


or your suggestion of the exit is interesting....

solution 2:

$SCRIPTPATH/bin/myperl_test.pl "$DATE" "$DOWNLOADDIR/$FILENAME2"

in perl:
my $date = $ARGV[0];
my $filename = $ARGV[1];

open (OUTPUT, ">$filename") || die "Unable to open $filename for writing: $!\n";

:
:
later if unsucessful use exit(1)

then in korn:

if [ "$?" = "0" ] ; then
  # execution was successful
   FILFLAG=`cat /tmpfileflagvalue`;
fi


I think the lesson learned is that in perl when a passed argument needs to be changed then that argument
needs to be a file.
0
 
arnoldCommented:
It all depends on what it is you are doing within korn shell.
You might as well do all the things you need within a single perl script.

It also is unclear what the fileflag is supposed to do i.e. is it supposed to alter the behavior of the perl script. Or what is the point for perl to alter it does the subsequent korn lines do something else based on that value?
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now