Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Alternative for a directory builder

by atcroft (Monsignor)
on Feb 22, 2011 at 18:40 UTC ( #889649=note: print w/ replies, xml ) Need Help??


in reply to Alternative for a directory builder

I used the same idea, but File::Spec to parse the filename/directory portions when I was testing the following, which may help if you need portability later:

use strict; use warnings; use File::Spec; my $p = q{/var/www/vhosts/testing.com/httpdocs/test1.html}; my ( $v, $d, $f ) = File::Spec->splitpath( $p ); my @dp = File::Spec->splitdir( $d ); foreach my $i ( 0 .. $#dp ) { next unless ( $i and length $dp[$i] ); print File::Spec->catdir( @dp[0 .. $i ] ), qq{\n}; }

For my sample data, the code gave me the following output:

/var /var/www /var/www/vhosts /var/www/vhosts/testing.com /var/www/vhosts/testing.com/httpdocs

Hope that helps.

Update 2011-02-22

I was able to come up with a map solution that seemed to work, although I believe others may have better solutions:

use strict; use warnings; use File::Spec; my $p = q{/var/www/vhosts/testing.com/httpdocs/test1.html}; my ( $v, $d, $f ) = File::Spec->splitpath( $p ); my @dp = File::Spec->splitdir( $d ); print join( qq{\n}, map{ if (! defined $dq ) { $dq = q{/}; } $dq .= $_ . q{/}; } grep{ m/.+/; } @dp ), qq{\n};

or, the following:

use strict; use warnings; use File::Spec; my $p = q{/var/www/vhosts/testing.com/httpdocs/test1.html}; my ( $v, $d, $f ) = File::Spec->splitpath( $p ); my @dp = File::Spec->splitdir( $d ); print join( qq{\n}, map{ File::Spec->catdir( @dp[0 .. $_] ) } grep{ length $dp[$_]; } 0 .. $#dp ), qq{\n};


Comment on Re: Alternative for a directory builder
Select or Download Code
Re^2: Alternative for a directory builder
by ikegami (Pope) on Feb 22, 2011 at 19:33 UTC

    which may help if you need portability later:

    You forgot to add the volume back in.

    Path::Class is easier to get right if you want portability.

Re^2: Alternative for a directory builder
by cyber-guard (Acolyte) on Feb 22, 2011 at 19:41 UTC
    Cheers for the suggestion, however I feel reluctant using whole extra module as the script will be actually ran on web server and won't go below www dir, thus compatibility issues should be out of question

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://889649]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2014-07-26 10:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls