LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   problem in accessing variables declared in another perl script (https://www.linuxquestions.org/questions/programming-9/problem-in-accessing-variables-declared-in-another-perl-script-629175/)

john83reuben 03-19-2008 07:28 AM

problem in accessing variables declared in another perl script
 
hi guys, i have a problem in perl. I want to access variables from a different script.

i give an example script

first script
Code:

#!/usr/bin/perl -w

use strict;
print "enter username\n";
$name = <STDIN>;

$ENV{a}="$name";
system("/perlfiles/receive.pl");

second script

Code:

#!/usr/bin/perl

print "$ENV{a}\n";

the error shows is

Code:

Global symbol "$name" requires explicit package name at ./sending.pl line 5.
Global symbol "$name" requires explicit package name at ./sending.pl line 16.
Execution of ./sending.pl aborted due to compilation errors.


but if i try this, it works

first script

Code:

#!/usr/bin/perl

$ENV{a}="20";
system("perl called.pl");

second script

Code:

#!/usr/bin/perl
print "$ENV{a}\n";

pls help. i need to access variables from another script

bigearsbilly 03-19-2008 07:42 AM

well, that's when you use strict
you need to declare variables, try
my $name = <>;


perldoc strict

chrism01 03-19-2008 10:59 PM

Actually, system() doesn't provide the output from the called prog.

Try backticks:

my $result=`/dir/some.pl`;

or you can use a piped open : http://perldoc.perl.org/IPC/Open2.html

alternately, if the programs / functions are always associated with each other, but need to be in different files, eg you want 2nd prog to be usable by other programs as well, convert it to a Perl module.
http://perldoc.perl.org/perlmod.html, specifically the section entitled 'Perl Modules' approx 2/3 down the page.

john83reuben 03-21-2008 02:45 AM

Please check my code, something is wrong somewhere

Quote:

this script is call firstrequire.pl
#!/usr/bin/perl -w

$username = "john";
$password = "123";


Quote:

this script is call file.pl

#!/usr/bin/perl -w

require "firstrequire.pl";

print "My name is $username\n";
print "My password is $password\n";

when I execute, it says

Quote:

Global symbol "$username" requires explicit package name at file.pl line 7.
Global symbol "$password" requires explicit package name at file.pl line 8.
Execution of file.pl aborted due to complilation errors.
but if i take off strict and the -w in file.pl means, it can be executed. So how to solve this problem by using strict..Pls help me


All times are GMT -5. The time now is 08:34 PM.