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

Hi Guys
Below is a small chunk of code which hopefully should give me a list like the following.
1 <username1>
2 <username2>
What I would like to be able to do is select 1 or 2 in STDIN and assign that number to the user name listed in the array.
Once the correct user is selected It changes directory to that users home directory continues.
I get the following error
Use of uninitialized value in concatenation (.) or string at testenv line 31, <S TDIN> line 1.
foreach my $file (@ulist) {eval { print "\n"; $n++; print $n; print " $file\n"; }} print " Please select which user you would like to...\n"; my $user = <STDIN>; chomp $user; $user = $file; chdir("$hdir/$user") || die "Cant Change to directory $hdir/$user: $!\ +n";

Replies are listed 'Best First'.
Re: Creating a Menu List
by shmem (Chancellor) on Sep 15, 2008 at 14:55 UTC

    Since you have no numbers next to the lines, we have to guess. I suspect

    $user = $file;

    to be the culprit, since you just clobber the $user variable whose value you just read from STDIN and carefully chomped, with an undefined value: the my $file above is defined only for the foreach context. use strict would catch that. And so,

    chdir("$hdir/$user") || die "Cant Change to directory $hdir/$user: $!\ +n";

    causes the the warning to be thrown. Where's $hdir defined?

    Anyways, that should be

    my $number = <STDIN>; chomp $number; die "Invalid selection\n" if $number =~ /\D/; die "Number given too big\n" if $number > scalar(@ulist); my $user = $ulist[$number]; chdir("$hdir/$user") || die "Cant Change to directory $hdir/$user: $!\ +n";
      Hi shmem
      Thanks for your response it was really helpful.
      I thought I would send you all my code with your suggestion attached
      what is, I think is happening is its starting from the first entry in the array which is 0.
      However no matter which selection I use it can’t seem to change to that user directory which does exist.
      Any further help would be greatly appreciated.
      #!/usr/bin/perl -w use Cwd; my $dir = getcwd; my $userhome = $ENV{HOME}; my $hdir = "/export/home"; my $userlist = "/$userhome/etc/userlist.cfg"; open(MYFILE, "$userlist") || die "Cant open File $userlist: $!\n"; my @ulist=<MYFILE>; foreach my $file (@ulist) {eval { print "\n"; $n++; print $n; print " $file\n"; }} print " Please select which user you would like to...\n"; my $number = <STDIN>; chomp $number; die "Invalid Selection\n" if $number =~ /\D/; die "Number given is too big\n" if $number > scalar(@ulist); my $user = $ulist[$number]; chdir("$hdir/$user") || die "Cant Change to directory $hdir/$user: $!\ +n"; &first; sub first { system("su - $user -c whatsrunning"); } sub second { print "Would you like to see the options again?\n"; my $answer = <STDIN>; chomp $answer; if ($answer eq "Y" || $answer eq "y" || $answer eq "yes") { system("su - $user -c whatsrunning"); &second; } else {exit 0; }} &second;

        As already stated, it would be wise to add a use strict to the beginning of your script. You'll then have to add a my $n = 0 before the foreach loop.

        I think you're going to have to either decrement $number, since your menu starts at one, or instead start your menu at zero.

Re: Creating a Menu List
by eff_i_g (Curate) on Sep 15, 2008 at 14:15 UTC
    I recommend using Term::Prompt's menu feature. Which is line 31?
Re: Creating a Menu List
by lamp (Chaplain) on Sep 15, 2008 at 15:07 UTC
    Please note that what you getting is a warning, not an error. Warnings alert you of situations that can lead to possible problems, but they do not terminate your programs like errors do. For eg.

    my $string; # $string is declared but not initialized!

    so any operation you do on '$string' (except an assignment) will produce the warning message, if you use 'use warnings' or -w option.

    which one is line 31?
Re: Creating a Menu List
by Anonymous Monk on Sep 15, 2008 at 15:16 UTC
    On a side note, what is the purpose of wrapping the menu printing in an eval?