in reply to STDIN help for a new Perl coder

I see a couple of issues.
  1. You neglected to remove the trailing newline from your input. You can take care of this by chomping the value:

    chomp(my $dir = <STDIN>);

  2. You omitted a semicolon on line 8:

    chdir($dir);

  3. You neglected to declare your variables @files and $file on lines 9 and 10:

    my @files = <$dir/*>; foreach my $file (@files)
  4. You omitted a comma (Comma Operator) from your print statement. Without it, Perl will read that as printing "\n" to the lexical file handle in $file.

    print $file, "\n";

You also might want to read Markup in the Monastery since you haven't followed site protocol. Rather than using a glob, I'd also use opendir, readdir and closedir to protect yourself from typos and pathological file names.

#!/usr/bin/perl use strict; use warnings; print "Enter name of directory (fully qualified path): "; chomp(my $dir = <STDIN>); opendir my($dirhandle), $dir or die "No such directory: $dir"; my @files = readdir($dirhandle); closedir($dirhandle); foreach my $file (@files) { print $file, "\n"; }

Replies are listed 'Best First'.
Re^2: STDIN help for a new Perl coder
by perlJourney (Initiate) on Jul 15, 2009 at 18:13 UTC

    Thank you Kennethk, I will look over the Monestary Markup guidelines... I missed that when I originally posted and was not aware of them since this is my first visit.

    I will definitely take your advice. Some of what you mentioned I should no better than to miss, mainly the ; and the declaring of variables but neglected somehow.

    If you happen to know of any good resources for Perl besides Learning Perl the Hard Way or Picking up Perl I would be interested in finding some.