[Ilugc] PERL assigning variables

  • From: girishvenkatachalam@xxxxxxxxx (Girish Venkatachalam)
  • Date: Mon Jan 25 13:21:38 2010

On Mon, Jan 25, 2010 at 12:30 PM, Ashish Verma <ilugid@xxxxxxxxx> wrote:

If user has stored his options in a file, then the code should open that
file, pick the values and assign them to variable names stored in the array
@vrvariables.
*Basically the variables should get assigned from either user input, or from
a stored file.*

my @myvariables = ('url','realm','username','password','proxy','cookies');
*my @vrvariables =
('$url','$realm','$username','$password','$proxy','$cookies'); # THESE ARE
THE STORED VARIABLE NAMES*

Something wrong in the above line.

Why would you want to do

$vrvariable[0] = '$url';

$vrvariable[1] = '$realm';

Can't you find a better way?

Anyway see below.

my $varcount = @myvariables;
my $ac = 0;
my $store_file = 'files-dash/cds.html';
sub grab_input
{
? ? ? ?print "Store choice made (Y/N): "; chomp(my $choice = <STDIN>);
? ? ? ?print "URL to access ? ? ? ? ?: "; chomp(my $url = <STDIN>);
chomp(my $url_credentials = `echo $url | cut -d "/" -f3`);
? ? ? ?print "Realm (If required) ? ?: "; chomp(my $realm = <STDIN>);
? ? ? ?print "Username (If required) : "; chomp(my $username = <STDIN>);
? ? ? ?system(`stty -echo`);
? ? ? ?print "Password ? ? ? ? ? ? ? : "; chomp(my $password = <STDIN>);
? ? ? ?system(`stty echo`);
? ? ? ?print "\n";
? ? ? ?print "Proxy server ? ? ? ? ? : "; chomp(my $proxy = <STDIN>);
? ? ? ?print "Cookies File to use ? ?: "; chomp(my $cookies = <STDIN>);

You can replace all <STDIN> with <>.

Default is that. ;)

? ? ? ?if($choice eq "Y" || "y")
? ? ? ?{
? ? ? ?my %credentials_store = ( url => $url, realm => $realm, username =>
$username, password => $password, proxy => $proxy, cookies => $cookies );
? ? ? ?open(STORE, ">store.txt");
? ? ? ?my $i = 0;
? ? ? ?foreach(%credentials_store)
? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ?while ($i < $varcount)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?print STORE
$credentials_store{"$myvariables[$i]"}, "\n";
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?$i++;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?}
? ? ? ?}
}

Is the above code printing correctly?

You did not mention that.

sub make_input_global
{
? ? ? ?`cp store.txt temp.txt`;

You could use File::Copy module instead.

? ? ? ?open(MKGLB, "<temp.txt");
? ? ? ?my $i = 0;
? ? ? ?while ($stored_var = <MKGLB>)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?{
* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"$vrvariables[$i]" = $stored_var;
#THIS IS NOT WORKING
* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?$i++;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ?#`rm -f temp.txt`;
}

This is a weird way to assign variables from a file.

Do you want to store each variable in a line?

Have you tried

$ cat temp.txt

and found out what it contains?

I think you are not using the perl idioms correctly. Perl eases program
development in a big way though the advanced skills come only with
experience.

But overall there are many simple ways to achieve complex tasks in perl.

-Girish
-- 
Gayatri Hitech
web: http://gayatri-hitech.com

SpamCheetah Spam filter:
http://spam-cheetah.com

Other related posts: