Search code examples
perlvariablesinputputtyexecute

Coding for external user input in perl


I am new to perl and coding and not entirely sure where/what to search for so this question may have been asked before.

I finnished writing a program in perl and would like to know what code to use that allows me to enter a variable (name) outside the program without needing to specify it in the coding - I was told there is a way to execute the program outside of putty (I use putty) and that it asks me to enter the variable/s beforehand.

in the coding the variable is specified in the beginning as:

my $name='xxx';

after which the name is used for specifying which files to use etc. I have over 30 different names that I need to run individually so it would be much easier if I can just type it in as part of the program instead of changing the coding each time in putty.

Hope my question is clear - I'm still learning the different terms and syntax.

Thanks!


Solution

  • use strict;
    use warnings; 
    
    open(IN,$ARGV[0]) or die "Cannot open $ARGV[0]:$!\n";
    my @in = <IN>;
    close(IN);
    
    my $name='';
    foreach my $in(@in){
       chomp($in);   
       $name = $in;
       ###your code here
     }
    

    your fle sample

    name1

    name2

    name3

    Run your programs as

    perl program.pl filename.txt

    Update:(after OP's comment)

    my $name = <STDIN>;
    

    this will prompt for a user input.