Cool! Here's a minor patch to make it ignore hidden entries (those starting with a dot) by default, and add an -a flag for showing those after all, similar to the one that ls has:

--- tree.pl 2015-10-13 11:59:07.665138800 +0200 +++ tree_afr.pl 2015-10-13 11:58:27.145867300 +0200 @@ -5,11 +5,13 @@ use Pod::Usage; my ( + $showall, # show all, including entries starting with . $showfiles, # show also files $showlinks, # show also a symlink's target ); help() unless GetOptions( + a => \$showall, l => \$showlinks, f => \$showfiles, 'h|help' => \&help, @@ -33,6 +35,7 @@ opendir my $dh, $path or die "Couldn't read $path: $!\n"; # Get all directory content - leaving out files unless asked for my(@content) = grep { !/^\.\.?$/ and ( $showfiles or not -f "$pat +h/$_" ) } readdir $dh; + @content = grep { !/^\./ } @content unless $showall; closedir $dh; # How many eitems are in the directory?

Without this I get a huge tree full of subtrees like .cpan, .git and so on.

EDIT: since this is such a useful little thing, I couldn't keep myself from changing it to Unicode box-drawing characters and also formatting the code according to my preferences:

#!/bin/env perl

use Modern::Perl '2014';
no  warnings 'experimental';
use feature 'signatures';

# core modules
use Getopt::Long qw(:config no_ignore_case);
use Pod::Usage;

# line-drawing characters
our %line = (
    "horizontal" => "──",
    "indent"     => "  ",
    "vertical"   => "│",
    "branch"     => "├",
    "corner"     => "└",
);

# options
our %opts;

# run program
exit MAIN();

sub MAIN {
    # grab options, display help on error
    help() unless GetOptions(
        a        => \$opts{'showall'},   # show all, including entries starting with .
        l        => \$opts{'showlinks'}, # show also a symlink's target
        f        => \$opts{'showfiles'}, # also show files
        'h|help' => \&help,
        'm|man'  => \&man,
    );

    # traverse each specified directory, or . if none was given
    foreach my $path (@ARGV ? @ARGV : '.') {
        say      $path;
        traverse($path);
    }
}

sub help { pod2usage(-verbose=>1); }
sub man  { pod2usage(-verbose=>2); }

sub traverse($path, $depth = '') {

    # Open the directory
    opendir my $dh, $path
        or die "Could not read $path: $!\n";

    # Get all directory content - leaving out files unless asked for
    my @content =
        grep { !/^\.\.?$/ }
        grep { $opts{'showall'}   or !/^\./ }
        grep { $opts{'showfiles'} or not -f "$path/$_" }
        readdir $dh;

    closedir $dh
        or die "Could not close $path: $!\n";

    # Number of elements yet to be printed
    my $remaining = scalar @content;

    # Prepare the standard indent
    my $indent = $depth . $line{'vertical'} . $line{'indent'};

    # Print all the elements
    foreach my $name (@content) {
        my $fullname = "$path/$name";

        # Prepare indent
        $indent = $depth . " " . $line{'indent'} unless --$remaining;

        print $depth, ($remaining ? $line{'branch'} : $line{'corner'}), $line{'horizontal'} , $name;

        # Is it a link?
        if (-l $fullname) {

            # Shall they be shown as such
            if($opts{'showlinks'}) {
                print " -> ", readlink $fullname;
            }

            print "\n";
            next;
        }

        print "\n";

        # Traverse if it's a subdirectory
        traverse($fullname, $indent) if -d $fullname;
    }
    return;
}


=head1 NAME

tree - A script to show a "graphical" representation of a directory structure

=head1 SYNOPSIS

tree options path...

=head1 DESCRIPTION

tree will show a "graphical" representation of a directory structure, including
all files (when B<-f> specified) and link targets (when B<-l> specified).

=head1 OPTIONS

=over 4

=item B<-f>

Show also files.

=item B<-l>

Shhow also link targets.

=item B<-h>

=item B<--help>

show a short help page.

=item B<-m>

=item B<--man>

Show the man-page.

=back

=head1 AUTHOR

Skeeve, member of perlmonks.org (perlmonks DOT org DOT Skeeve AT XoXy DOT net)
Minor changes and updates by Apple Fritter, a fellow Perl monk

=cut

In reply to Re: tree command by AppleFritter
in thread tree command by Skeeve

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.