perl - how to make output of a command a numbered list

Hi there, just wondered if somebody could help me with a problem I have

I have a program that when run from the command line will output a list of objects

# list_servers
server1
server5
server7
server8
# 

I just wanted to know, in perl, how can i make each line of output from the above coimmand, a selectable option in an interactive perl script, so for example

# example.pl

Please select from the following available servers

1) server1
2) server5
3) server7
4) server8

enter selection: 

is this possible in perl ?

Yes, it is possible in Perl -

$
$
$ cat -n example.pl
     1  #perl -w
     2
     3  # define the main menu as a multiline string
     4  $main_menu = <<END
     5  Please select from the following available servers
     6
     7  1) server1
     8  2) server5
     9  3) server7
    10  4) server8
    11
    12  END
    13  ;
    14
    15  # print the main menu
    16  print $main_menu;
    17
    18  # prompt for user's choice
    19  printf("%s", "enter selection: ");
    20
    21  # capture the choice
    22  $choice = <STDIN>;
    23
    24  # and finally print it
    25  print "You entered: ",$choice;
    26
$
$ perl example.pl
Please select from the following available servers

1) server1
2) server5
3) server7
4) server8

enter selection: 4
You entered: 4
$
$

tyler_durden

Thank you for responding, however, it wasnt so much the capture of the input (ive already got that bit written, but thanks anyway :b:) but how to turn the output of a command 'dynamically' into a list

for example, the the output of the list_servers script I mentioned before is variable, and could produce a different list of servers each time.

Sorry for not paying attention to your question.
Here's an idea to implement a dynamic list -

$ 
$ 
$ # run the program that prints list of servers
$ ./list_servers
server1
server5
server7
server8
$ 
$ #
$ # display the content of the Perl program
$ cat -n example.pl
     1    #perl -w
     2    
     3    # fill up the server list
     4    @servers = `./list_servers`;
     5    $num = 0;
     6    
     7    # print the main menu
     8    print "Please select from the following available servers\n\n";
     9    foreach $i (@servers) {
    10      print ++$num,")  $i";
    11    }
    12    
    13    # prompt for user's choice
    14    printf("\n%s", "Enter selection: ");
    15    
    16    # capture the choice
    17    $choice = <STDIN>;
    18    
    19    # and finally print it
    20    print "You entered    : ",$choice;
    21    print "So you chose   : ",$servers[$choice-1];
    22    
$ 
$ # now run the Perl program
$ perl example.pl
Please select from the following available servers

1)  server1
2)  server5
3)  server7
4)  server8

Enter selection: 3
You entered    : 3
So you chose   : server7
$ 
$ 

HTH,
tyler_durden

I haven't added any code for input validation though.

using POSIX module kindly see below code:-

perl -M'Shell::POSIX::Select' -wle '
@servers=`cat ./list_servers`;
chomp(@servers) ;
select (@servers){print "You choose server $_"}
'

BR

;);):wink: