Global Variables -> Local Variables

Posted by Solaras on Tue 14 Jun 2005 03:18 AM — 4 posts, 19,469 views.

#0
I am trying to define my global Variables in mushclient to Local Variables within my sub routine and cannot for the life of figure it out.

Have I mentioned I am new to Mushclient? *hangs his head in shame after 32 years of never learning simple coding"

This is what I have and please someone point out what I am doing wrong.

sub testsub {
my $a = $world->GetVariable("test1");
my $b = $world->GetVariable("test2");
my $c = $world->GetVariable("test3");
if ([$a] == 1)
{
SetVariable([$a], 0)
}
if ([$b] == 2)
{
SetVariable([$b], 0)
}
if ([$c] == 3)
{
SetVariable([$c], 0)
}
}
USA #1
Why are you using square brackets?

You're just trying to access $a, $b, and $c, correct?

and then I understand that you're trying to set test1,2,3 to zero? Since you're not using a hash or anything, you'll need to just hardcode the names into you setvariable statements.
And you will need to use the world object for setting as well.


sub testsub
{
  my $a = $world->GetVariable('test1');
  my $b = $world->GetVariable('test2');
  my $c = $world->GetVariable('test3');
  if ($a == 1)
  {
    $world->SetVariable('test1', 0)
  } 
  if ($b == 2)
  {
    $world->SetVariable('test2', 0)
  }
  if ($c == 3)
  {
    $world->SetVariable('test3', 0)
  }
}
#2
So I cannot modify the local object "a" and have it update the global object "test1"?

I would prefer to have my subroutines work completly with local variables if I can, just calling and assigning them at the start of the routine and everything is set. That way should I ever decide down the road that a=something other than test1, I need only change the identifier, and not the entire code.
USA #3
No, GetVariable merely returns a value, it doesn't return a reference, or anything fancy like that.

You could use a hash, to keep track of the perl variables and their corresponding MC variables, like this:
sub testsub
{
  my %variables = ('a' => 'test1','b' => 'test2', 'c' => 'test3');
  my ($a, $b, $c); #make local variables (my ${$id}) wont work)
  foreach $id (keys %variables)
  {
    ${$id} = $world->GetVariable($variables{$id});
  }
  if ($a == 1)
  {
    $world->SetVariable($variables{'a'},0);
  }
  if ($b == 2)
  {
    $world->SetVariable($variables{'b'},0);
  }
  if ($c == 3)
  {
    $world->SetVariable($variables{'c'},0);
  }
}

I suppose you COULD use tie and things like that to link local variables to mushclient variables, but that seems like an awful lot of overhead.
But once you would have it set up, the local variables and the mushclient variables would be linked for getting and setting.

I don't think you'll be doing enough variable name changing to make either of these things worthwhile, well, maybe at the beginning while you’re getting comfortable with scripting, but once you've gotten the hang of it, all it would do is slow down your scripts. Especially since you won't need to change the variable names at all once you’re more comfortable and your scripts are more mature.

Heck, variables can all be changed with a simple find replace in the world file. Which wouldn't be too much of a hassle for the coder, and would incur no penalty for the user.