perl_newbie99 has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Perl Experts, I have a script which is already running fine for a small number of servers monitoring. The name of servers are given in script itself (because as of now those were very less to monitor). Now I need to add 100+ server names and trying to do it using external config file which will contain only name of servers.
my @services = ( #[ 'servername-001', '10.68.197.91', 1666 ], [ 'servername-001', 'imm-servername-001.companyname.com' ], [ 'servername-002', 'imm-servername-002.companyname.com' ], );
The question is how can I change this script to read following part from an external text file in the same variable name as mentioned above:
[ 'servername-001', 'imm-servername-001.companyname.com' ], [ 'servername-002', 'imm-servername-002.companyname.com' ],
I know it is not that difficult but I dont have perl expertise either :)) So, any help will be appreciated. Thanks.

Replies are listed 'Best First'.
Re: How to input variable value from a file?
by wind (Priest) on Jun 09, 2011 at 18:54 UTC
    Assuming the data file is delimited with spaces like so:
    servername-001 imm-servername-001.companyname.com servername-002 imm-servername-002.companyname.com
    Then the following would work:
    open my $fh, $serverfile or die "Can't open $serverfile: $!"; my @services = map {chomp; [split]} <$fh>; close $fh;
      Hi Wind, Thanks.. it worked (except for a minor fix). Thanks again! :)
      #open my $fh, $serverfile or die "Can't open $serverfile: $!"; open my $fh, "serverfile" or die "Can't open $serverfile: $!"; my @services = map {chomp; [split]} <$fh>; close $fh;

        You just need to initialize the variable.

        Don't forget use strict; and use warnings at the top of every script.

        use strict; use warnings; my $serverfile = 'foo.bar';
Re: How to input variable value from a file?
by mr_mischief (Monsignor) on Jun 09, 2011 at 20:03 UTC