Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7940929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:30:48+00:00 2026-06-03T23:30:48+00:00

The Path::Class module is a top-rated module and has very good reviews at cpan.org.

  • 0

The Path::Class module is a top-rated module and has very good reviews at cpan.org. I wonder whether the Path::Class:Dir module can create directory tree like this*:

/home/scottie/perl/lib/Path-Class-0.25
  -> lib/
     -> Path/
        -> Class/
           -> Dir.pm
           -> Entity.pl
           -> File.pl
        -> Class.pl
  -> t/
     -> 01-basic.t
     -> 02-foreign.t
     -> 03-filesystem.t
     -> 04-subclass.t
     -> 05-traverse.t
     -> author-critic.t
  -> Build.PL
  -> Changes
  -> dist.ini
  -> INSTALL
  -> LICENSE
  -> Makefile.PL
  -> MANIFEST
  -> META.yml
  -> README
  -> SIGNATURE

*) Source: Path-Class-0.25.tar.gz file and “/” at the end of string indicated directory (like ls -p in *nix systems)

And not like this (full path from root):

  /home/scottie/perl/lib/Path-Class-0.25
  -> /home/scottie/perl/lib/Path-Class-0.25/lib/
     -> /home/scottie/perl/lib/Path-Class-0.25/lib/Path/
        -> /home/scottie/perl/lib/Path-Class-0.25/lib/Path/Class/
           -> /home/scottie/perl/lib/Path-Class-0.25/lib/Path/Class/Dir.pm
           -> /home/scottie/perl/lib/Path-Class-0.25/lib/Path/Class/Entity.pl
           -> /home/scottie/perl/lib/Path-Class-0.25/lib/Path/Class/File.pl
        -> /home/scottie/perl/lib/Path-Class-0.25/lib/Path/Class.pl
  -> /home/scottie/perl/lib/Path-Class-0.25/t/
     -> /home/scottie/perl/lib/Path-Class-0.25/t/01-basic.t
     -> /home/scottie/perl/lib/Path-Class-0.25/t/02-foreign.t
     -> /home/scottie/perl/lib/Path-Class-0.25/t/03-filesystem.t
     -> /home/scottie/perl/lib/Path-Class-0.25/t/04-subclass.t
     -> /home/scottie/perl/lib/Path-Class-0.25/t/05-traverse.t
     -> /home/scottie/perl/lib/Path-Class-0.25/t/author-critic.t
  -> /home/scottie/perl/lib/Path-Class-0.25/Build.PL
  -> /home/scottie/perl/lib/Path-Class-0.25/Changes
  -> /home/scottie/perl/lib/Path-Class-0.25/dist.ini
  -> /home/scottie/perl/lib/Path-Class-0.25/INSTALL
  -> /home/scottie/perl/lib/Path-Class-0.25/LICENSE
  -> /home/scottie/perl/lib/Path-Class-0.25/Makefile.PL
  -> /home/scottie/perl/lib/Path-Class-0.25/MANIFEST
  -> /home/scottie/perl/lib/Path-Class-0.25/META.yml
  -> /home/scottie/perl/lib/Path-Class-0.25/README
  -> /home/scottie/perl/lib/Path-Class-0.25/SIGNATURE

I tried to do it but there is something wrong with my code:

#------------------8<------------------
my $dir = Path::Class::Dir->new('/home/scottie/perl/lib/Path-Class-0.25');

my $nfiles = $dir->traverse(sub {
    my ($child, $cont) = @_;
    return if -l $child; # don't follow symlinks

    #print Dumper($child);
    #print "$child\n";

    print $child->{'dir'}{'dirs'}[-1];
    print " -> ";
    print $child->{'file'};
    print "\n";
    return $cont->();
});
#------------------8<------------------

Could you please have a gander at my code and tell me how to make a directory tree without full paths in $child?

Many thanks!

Best regards,
Scottie

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:30:50+00:00Added an answer on June 3, 2026 at 11:30 pm

    You need to use the basename method of Class::Path::File (which is also available as a method of Class::Path::Dir) to extract just the last element of the path.

    You must also use a parameter to the callback to keep track of the current level of indentation.

    This program appears to fulfil your requirement.

    use strict;
    use warnings;
    
    use Path::Class;
    
    my $dir = dir('/home/scottie/perl/lib/Path-Class-0.25');
    
    $dir->traverse(sub {
    
      my ($child, $cont, $indent) = @_;
      $indent //= 0;
    
      if ($indent == 0) {
        print $child, "\n";
      }
      else {
        print '   ' x ($indent - 1), '-> ', $child->basename;
        print '/' if $child->is_dir;
        print "\n";
      }
    
      $cont->($indent + 1);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know I can expose a class inside a web-module as a restful interface
To avoid the long class path issue in windows, i modified my build.xml like[1]
I had a question about Java Class Path variables. If I have multiple jars
That is when you have any jar file attached to you your class path.
I have a class called Path for which there are defined about 10 methods,
What class(FrontController , Bootstrap, Dispacher....) sets up the default structure path in ZF?
I have a class which loads an xml file using the following: Path.Combine( AppDomain.CurrentDomain.BaseDirectory,
I'm studying some Rails 3 code from Spree: module Spree module Generators class SiteGenerator
I have a simple class that defines some constants, e.g.: module Foo class Bar
I'm currently writing a serialization module in Python that can serialize user defined classes.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.