On day9/3/01 at 12:13 pm -0800, Christopher Palmer wrote:

>So I have gotten to this point with this script...it reads the
>current directory, lists the files with a number next to them,
>accepts user input as to what file he/she wants to open, but then
>does not seem to open the file.  I have tried a couple of ways to
>print the file to <STDOUT> this just happens to be the last way in
>the series.
>
>
>#!/usr/bin/perl
>
>opendir ( CURRDIR, '' ) || die "Can't open it!";
>@file_list = readdir ( CURRDIR );
>
>$i = 0;
>
>foreach $file_name ( @file_list ) {
>print "$i - " . "$file_name\n";
>$i ++;
>}
>
>print "Which file would you like to open?: ";
>chomp ( $openme = <STDIN> );
>print "I will open $file_list[$openme]\n";
>open (THATFILE , '$file_list[$openme]');
>print while (<THATFILE>);
>close (THATFILE);
>
>closedir CURRDIR;


The UNIX shebang #!/usr/bin/perl is pointless.

readdir will list all items in the directory including directories
and the Icon\r; you want only text files, so build a list using -T
and excluding Icon\r.

Your $file_list[$openme] gives only the name of the file and not the pathname.



        #!perl -w
        $dir = $ENV{MACPERL};
        opendir (DIR, $dir) || die $!;
        @all = readdir DIR;
        foreach(@all) {
         -T and !/Icon\n/ and push @flist, $_
        }
        $i = 1;
        foreach(@flist){
        print "$i - $_$/";
        $i++
        }
        print "$/Type a number: ";
        chomp ($n = <STDIN>);
        print $/;
        open (F , "$dir$flist[$n-1]");
        print while (<F>);

JD

Reply via email to