2

I'm learning Perl, and I made a script that will use defined variables in the beginning of my script to establish connection,pull records, modify them, then close connection.

the second part of my work involve repeating the same steps but for different server.

is there is a way to un-set whatever variables has been set before? and then use new defined settings and repeat the steps?

Thank you

7
  • 1
    You should consider the type of variable that you refers, is it an array? or a hash? or a scalar?. Generally I reset this with @array = (); or %hash = (); Commented Feb 26, 2013 at 22:25
  • 5
    It sounds like what you want is a subroutine (a.k.a. function) with parameters. Commented Feb 26, 2013 at 22:27
  • 2
    I usually just declare types at the beginning and assign as I go. Just reassign them but a function with parameters would be better. Commented Feb 26, 2013 at 22:29
  • 1
    @bwtrent, can I assume you come from a C background? Commented Feb 26, 2013 at 22:55
  • 2
    Reusing variables is horrible practice. Then you have to scan backwards in the code to see where they have been changed. Declare variables with a limited scope, and don't repeat your code: make a subroutine instead. Commented Feb 26, 2013 at 23:13

1 Answer 1

7

Define your variables in their own scope.

{
    my $server = '123.123.123.123';
    my $username = 'user1';
    ping($server);
    login($username);
}
{
    my $server = '222.222.123.123';
    my $username = 'user2';
    ping($server);
    login($username);
}

Even better, use a function definition:

sub doSomethingToServer
{
    my ($server, $username) = @_;
    ping($server);
    login($username);
}

doSomethingToServer('123.123.123.123', 'user1');
doSomethingToServer('222.222.123.123', 'user2');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.