[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 460
  • Last Modified:

How to pass variables to a subfunction in Perl?

Hi,
I have two function calls in Perl to the same sub function. I pass some variables to this sub function.

However, in these two calls to this sub function, the list of variables change. Depending on the list of variables I do make another function call to a Java call with these input variables.

So, how am I gonna pass these variables to talk2G sub function?

Example code:

some code in here

talk2G($a, $b, $c, $d);

some code in here

talk2G($e, $f, $g, $h, $k, $m, $n);

some code in here

sub talk2G{
    if() {
        my $javaCmd = "java -jar $mydir/myTool/GPreq.jar $a $b $c $d";
    }
    else {
         my $javaCmd = "java -jar $mydir/myTool/GPreq.jar $e $f $g $h $k $m $n";
    }

system($javaCmd);

}

Open in new window

0
Tolgar
Asked:
Tolgar
  • 3
  • 2
1 Solution
 
farzanjCommented:
sub talk2G
{
     my ($a, $b, $c, $d) = @_;


Second method
my $a = shift;
my $b = shift;
my $c = shift;
my $d = shift;
0
 
TolgarAuthor Commented:
ok. I have a quick additinal question.

Let's assume in line 12 in the original code, I need to check a variable that I pass to this sub function. And let's assume, this variable is the first one. (Or it can be the last one.) and I don't need that variable afterwards.

How can get only this variable from the list pf passed variables and use the rest in the Java call?

Example: In here, how can I get $x only before the others
some code in here

talk2G($x, $a, $b, $c, $d);

some code in here

talk2G($x, $e, $f, $g, $h, $k, $m, $n);

sub talk2G{
if($x) {
                    my($b, $c, $d) = @_
                    my $javaCmd = "java -jar $mydir/myTool/GPreq.jar $a $b $c $d";
            else {
                    my($e, $f, $g, $h, $k, $m, $n) = @_;
                    my $javaCmd = "java -jar $mydir/myTool/GPreq.jar $e $f $g $h $k $m $n";
            }

system($javaCmd);
}

Open in new window


Thanks,

0
 
farzanjCommented:
If I understand you correctly, you can do just one shift.  This would pull the first variable passed

my $x = shift;
0
 
TolgarAuthor Commented:
ok thanks
0
 
farzanjCommented:
You are most welcome!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

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