directory and files listing in perl

hi All,

i am tring to build a html page that display a directory listing using perl script...

first i have to use perl so i have use:

 my @files = glob("*");

to get all the files and display it in the page  the problem that i need to display also

the file last modify and size!

i have use stat($file)  but it disn'y display the length of files!

here is the code i have write:

my @files = glob("*");

foreach $file (@files) {
  @ABC = (stat($file));
  $filesLink = $filesLink."<tr><td valign=\"top\"></td><td><A href=\"maintenance/"."$file"."\">"."$file"."</A></td><td align=\"right\">-----------$ABC['File::stat'][9] ------------------------  </td><td align=\"right\">763K</td><td>&nbsp;</td></tr>";

i have also try to use this asample from the internet also it didn't work:

my $path = "maintenance/";

$opendir my $ls,$path or die $!;
while (my $file = readdir $ls ) {
    next if !-f "$path/$file";

    my @stat = stat("$path/$file");
    push @files, [ $file , localtime($stat[9]) ];
closedir $ls;

please help
thanks a lot
Who is Participating?
perl -W -Mdiagnostics -e '@ABC[7]'
Scalar value @ABC[7] better written as $ABC[7] at -e line 1 (#1)
    (W syntax) You've used an array slice (indicated by @) to select a
    single element of an array.  Generally it's better to ask for a scalar
    value (indicated by $).  The difference is that $foo[&bar] always
    behaves like a scalar, both when assigning to it and when evaluating its
    argument, while @foo[&bar] behaves like a list when you assign to it,
    and provides a list context to its subscript, which can do weird things
    if you're expecting only one subscript.
    On the other hand, if you were actually hoping to treat the array
    element as a list, you need to look into how references work, because
    Perl will not magically convert between scalars and lists for you.  See

print scalar localtime((stat $file)[9]);
(stat)[7]  is the length
" $ABC['File::stat'][9] " should give you the warning
Argument "File::stat"  isn't numeric in array element

There should not be a $ in $opendir
weissmanAuthor Commented:
hi ozo,

thank you for quick respond ... i have use $ABC['File::stat'][7] but didn't work either

what do you mean by :

Argument "File::stat"  isn't numeric in array element

sorry i am new to perl but i know java

should i use glob or readdir

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

perl -W -Mdiagnostics -e "\$ABC['File::stat'][9]"
Argument "File::stat" isn't numeric in array element at -e line 1 (#3)
    (W numeric) The indicated string was fed as an argument to an operator
    that expected a numeric value instead.  If you're fortunate the message
    will identify which operator was so unfortunate.

Array indices are numbers
'File::stat' is a string.  When you try to use it as a number, it will be treated as a 0
so $ABC['File::stat'] will be evaluated as $ABC[0]
but $ABC['File::stat'][9] doesn't even make sense, since  $ABC[0] is not an array

You may have meant to say
$filesLink .= qq(<tr><td valign="top"></td><td><A href="maintenance/$file">$file</A></td><td align="right">-----------$ABC[7] ------------------------  </td><td align="right">763K</td><td>&nbsp;</td></tr>);
Dave BaldwinFixer of ProblemsCommented:
Here's a simple working demo... though @ozo can tell you if there is anything wrong with it.

    use strict;
    use warnings;

    # my $dir = '/';
		use Cwd;
		my $dir = getcwd;

		print "Content-type:text/html\n\n";
		print "<b>File listing of $dir</b>\n\r<br>";
		print "<table border='0' cellpadding='0' cellspacing='0'>";

    opendir(DIR, $dir) or die $!;

    while (my $file = readdir(DIR)) {

		# Use a regular expression to ignore files beginning with a period
		next if ($file =~ m/^\./);
		next if ($file =~ m/\.bak/);
		my $fsize = -s $file;

		print "<tr><td>$file&nbsp;</td><td>&nbsp;$fsize</td></tr>";

		print "</table>";
    exit 0;

Open in new window

weissmanAuthor Commented:
thanks ozo,

although i didn't know what is the difference between $ABC[7] and @ABC[7]  they display the

same result :  (4096)

      test      1386676710      4096

can i ask you how to format the 4096 to be displayed in kb/mb instead

and how to displat last modify in date format ex. 10-Dec-2013 11:58

thanks a lot
weissmanAuthor Commented:
thanks a lot to you ozo, Dave

i have managed to display the date in format  using :

$datestring = localtime($ABC[9]);

although it is not my fevorite format but it is okay now

my last question how to format the bytes in MB /KB  i need a function that check the size and if it bigger than 1024 kb then display it in MB instead of kb ...

thanks a lot
You can customize the date format with POSIX::strftime
weissmanAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.