Madhu Reddy wrote:

> Hi,
>    I want to find out how to pass perl variables
> between files and functions...
> in C, we declare with "extern" to work in different
> files.... how to do in perl...
> between files means , i declare variable in file1 and
> file2 will update those variables and want to access
> in file1
> Ex: like $var1 and $var2 in following example
> 
> between functions means, from main file i need to pass
> variables to function..
> that function will update those arguements
> like $var3 and var4 in following example
> 
> like in C, we pass the address of variable to
> functions
> 
> following is the scenerio...
> 
> File1 : Script1.pl
> ---------------------
> #!C:\perl\bin\perl -w
> require "script2.pl";
> 
> $var1 = 0;
> $var2 = 0;
> 
> # this is a function from another file, this function
> #needs to update $var1 and $var2
> my $ret = VAL::Validate();
> 
> print "var1 is $var1\n";
> print "var2 is $var2\n";
> 
> 
> VAL::func1($var3, $var4);
> 
> print "var3 is $var3\n";
> print "var4 is $var4\n";
> 

there are many ways to do this in Perl. i would recommend passing a 
reference to the function and let the function manipulate your variables:

#!/usr/bin/perl -w
use strict;

#--
#-- A_Test.pm
#--
package A_Test;

use Exporter;

our @ISA = qw(Exporter);
our @EXPORT_OK = qw(fun);

sub fun{
        my $var1 = shift;
        my $var2 = shift;
        $$var1 = -1;
        $$var2 = -2;
}

1;

__END__

#!/usr/bin/perl -w
use strict;

use A_Test qw(fun);

my $vara = 1;
my $varb = 2;

print "$vara $varb\n";
fun(\$vara,\$varb);
print "$vara $varb\n";

__END__

prints:

1 2
-1 -2

david

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to