LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-19-2008, 07:28 AM   #1
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Rep: Reputation: 17
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
 
Old 03-19-2008, 07:42 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

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


perldoc strict
 
Old 03-19-2008, 10:59 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,355

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
Old 03-21-2008, 02:45 AM   #4
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Original Poster
Rep: Reputation: 17
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep and assign it's output to variables inside script itself problem xxx_anuj_xxx Programming 3 09-22-2007 11:24 PM
problem is posting variables in CGI script girish_hilage Programming 8 05-04-2007 10:07 AM
Perl: Source shell script to for environment variables? stefanlasiewski Programming 3 02-07-2006 06:27 PM
Perl Script: Variable Names composed of Variables wwnexc Programming 4 02-06-2006 03:47 AM
Unable to export variables declared in a script joehansen12 Linux - Newbie 2 10-26-2005 02:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration