I see a couple of issues.
- You neglected to remove the trailing newline from your input. You can take care of this by chomping the value:
chomp(my $dir = <STDIN>);
- You omitted a semicolon on line 8:
chdir($dir);
- You neglected to declare your variables @files and $file on lines 9 and 10:
my @files = <$dir/*>;
foreach my $file (@files)
- 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";
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.