HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Improved remake of the Linux 'ls' command

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
theimprovedremakelinuxcommand

Problem

I have written a script, introduced here, that improves the Linux terminal experience. It basically displays the content of the terminals current folder in a nicer way than the 'ls' command does. I use it all the time myself.

```
#!/usr/bin/perl
###########################################################################
# role: this program shows all files and directories in current location.
# about: its a replacement to the 'ls' command.
# about: different from the ls command, this program is easily cutomize-able and improve-able.
# about: the code is available, readable and change-able on site.
# tofix: the 'length' function fails when there are icelandic letters. have to find another solution.
# tofix: could figure out beforehand what the largest filename is, and then position the file-size category accordingly.
# tofix: also, the header should be either singular or plural, not both.
# tofix: sorting week1 .. week12 does not work correctly.
# tofix: add: type(eg .pm) and line_quantity.
###########################################################################

#get all the pathfiles at current path.
chdir("$ENV{PWD}") or die "$!";
opendir(CDIR, ".") or die "$!";
@pathfiles_and_directories_here=grep {/.*/} readdir CDIR;
foreach(@pathfiles_and_directories_here) {$_="$ENV{PWD}"."/$_";}
close CDIR;
@pathfiles_and_directories_here=grep { !/\/\.{1,2}/ }
@pathfiles_and_directories_here;#dont want '.' and '..'.

#establish pathfiles, paths and files.
@pathfiles_here =grep(! -d,@pathfiles_and_directories_here);
@files_here =grep(! -d,@pathfiles_and_directories_here);
foreach(@files_here) {s/$ENV{PWD}\///g;}
@directories_here= grep( -d,@pathfiles_and_directories_here);
foreach(@directories_here) {s/$ENV{PWD}\///g;}

#sort the pathfiles, files and directories.. alphabetically.
@files_here=sort_alphabetically(@files_here);
$file_quantity=($#files_here+1);
@pathfiles_here=sort_alphabetically(@pathfiles_here);

#seperate directories into if they start w

Solution

use strict; and use warnings; are missing. Add them and declare all your variables.

opendir(CDIR, ".") or die "$!";


I believe that it’s advised to use variables in modern Perl:

opendir(my $cdir, '.') or die $!;




@pathfiles_and_directories_here=grep {/.*/} readdir CDIR;


grep {/.*/} is meaningless since it accepts everything.

@pathfiles_and_directories_here=grep { !/\/\.{1,2}/ }
@pathfiles_and_directories_here;#dont want '.' and '..'.


Why didn’t you grep for this pattern right at the beginning? I also dislike the line break here, it makes the lines look like unrelated statements =>

my @files_and_dirs = grep { !/^\.{1,2}/ } readdir $cdir;


I’ve also shortened the variable name. The previous name was too long, and prevented, rather than helped, readability. In general, the long variable names make it really hard to discern any structure when looking over your code. Furthmore, the suffix _here after each variable name isn’t conveying useful information.

Next,

foreach(@pathfiles_and_directories_here)  {$_="$ENV{PWD}"."/$_";}


The string concatenation is redundant. Furthermore, this look looks like a place for map.

@files_and_dirs = map { "$ENV{PWD}/$_" } @files_and_dirs;


But why are you doing this anyway? Further down the road, you remove the file path again from the files and paths.

#establish pathfiles, paths and files.


You should explain what the difference between pathfiles and regular files is. The comment, as it stands, doesn’t tell the reader anything interesting.

I’m not convinced that sort_alphabetically and sort_numerically help readability. Just replace the calls by sort with the appropriate comparer, that should be readable enough, and is just as short.

Furthermore, a note on formatting: you need to indent your code properly and remove dead code. This:

sub sort_numerically{
#my @sorted=sort @_;
#print STDOUT "";
#sleep 1;
my @sorted= sort {$a  $b} @_;
return @sorted;
}


cries “sloppy”. Writing clean code is a fundamental part of maintainability.

Code Snippets

opendir(CDIR, ".") or die "$!";
opendir(my $cdir, '.') or die $!;
@pathfiles_and_directories_here=grep {/.*/} readdir CDIR;
@pathfiles_and_directories_here=grep { !/\/\.{1,2}/ }
@pathfiles_and_directories_here;#dont want '.' and '..'.
my @files_and_dirs = grep { !/^\.{1,2}/ } readdir $cdir;

Context

StackExchange Code Review Q#13845, answer score: 4

Revisions (0)

No revisions yet.