I have two variables. I need to call them using the same function
my $dat1 ="abc";
my $dat2 ="def";
my $check;
filecheck($dat1,$dat2);
sub filecheck($)
{
$check = shift;
print "first name is $check\n";
}
print "this is fine\n";
sub filecheck($)
{
$check = shift;
print "second name is $check\n";
}
[output]
second name is abc
this is fine
[/output]
required output:
[required output]
first name is abc
this is fine
second name is def
[/required output]
my $dat1 ="abc";
my $dat2 ="def";
my $check;
filecheck($dat1,$dat2);
sub filecheck {
$check = shift;
print "first name is $check\n";
print "this is fine\n";
$check = shift;
print "second name is $check\n";
}
You could also make use of the local array "@_" inside a subroutine.
$
$ cat -n function.pl
1 #!perl -w
2 my $dat1 = "abc";
3 my $dat2 = "def";
4 filecheck ($dat1, $dat2);
5
6 sub filecheck {
7 my @names = @_;
8 print "first name is $names[0]\n";
9 print "this is fine\n";
10 print "second name is $names[1]\n";
11 }
$
$ perl function.pl
first name is abc
this is fine
second name is def
$
$
You do not have to assign the array to a local array variable inside the subroutine, as the following program shows.
But doing so may be a good idea for purposes of clarity and maintainability.
$
$ cat -n function_1.pl
1 #!perl -w
2 my $dat1 = "abc";
3 my $dat2 = "def";
4 filecheck ($dat1, $dat2);
5
6 sub filecheck {
7 # The "@_" array already holds the parameter values
8 # at this point. You can access its elements.
9 print "first name is $_[0]\n";
10 print "this is fine\n";
11 print "second name is $_[1]\n";
12 }
$
$
$ perl function_1.pl
first name is abc
this is fine
second name is def
$
$