in reply to Re^2: make_path for creating directory tree
in thread make_path for creating directory tree

Although I don't understand some bits, so if you could be so kind and explain them you'd be helping me fill one more page in my Perl notebook!
Sure. Feel free to ask any questions.
my $length = $total_length - length($prefix); my $format = "${prefix}%0${length}d";
$format just makes a format string for sprintf. It seems you're not familiar with that function. It is modeled after the famous printf function in the C programming language. I recommend you to just search the internet for something like "printf tutorial". There must be lots of those, and it's a very useful function in general.

The goal of the line my $format = "${prefix}%0${length}d"; is to produce a string like "AB%04d", which is a format string for sprintf.

Why "${prefix}" and not "{$prefix}"?
${length} is actually a more useful example here. You know that you can interpolate variables in strings like so:"$variable". Suppose that I used $length instead of ${length}: "${prefix}%0$lengthd". Then Perl would've tried to find variable $lengthd (notice the d at the end), and there's no such variable. You can use curly braces to disambiguate: "${length}d". Now it's clear (to Perl) that $ goes with length, while d is just a character and is not a part of the variable's name.

And why ${prefix} instead of just $prefix? Just for symmetry with ${length}.

Another way to write it:
my $format = $prefix . '%0' . $length . 'd';
Actually in general what confused me is that I thought Perl reads the script line by line from top to bottom. Now in this example we first use make_path and then set the subroutine. Shouldn't the subroutine be on top of everything else?
Actually, no. Variables work like that, but not subroutines. I always put all subroutines at the bottom. That's different from some other programming languages and UNIX shells - in those, functions must also be put on top before you can actually use them - but not in Perl.
return map sprintf( $format, $_ ), $from .. $to;
This is a pretty dense line. map is another famous function, from functional programming languages. You should read it right-to-left (like in Arabic):
$from .. $to
That creates an anonymous list of numbers, starting with $from and ending with $to. For example, if $from is 1 and $to is 5, it will create (1,  2, 3, 4, 5). It's anonymous because it doesn't have a name and exists only temporarily in Perl's virtual machine.
map sprintf( $format, $_ ), ANONYMOUS_LIST
map acts like a loop. It loops over anonymous list and invokes sprintf for each element of that list. map puts the result into another anonymous list. Like so:
for (ANONYMOUS_LIST) { STRING = sprint( $format, $_ ); put STRING into ANOTHER_ANONYMOUS_LIST; }
and then return returns ANOTHER_ANONYMOUS_LIST from the make_seq function.

Replies are listed 'Best First'.
Re^4: make_path for creating directory tree
by fasoli (Beadle) on Dec 24, 2015 at 12:25 UTC

    Ok I just finished studying this :) I understand everything now and I will read on subs, map and printf. Thank you so much! :)

    edit: Actually I was thinking I'd like to make it a bit more "organised" especially in case I add more subfolders in the future, so I made this slight change (nothing really, just specified the subdirectories in the beginning). It looks like it's correct and the result is what I want - sorry, just getting paranoid that I might have messed the subroutine up, that's the only reason why I'm posting this tiny change.

    my @dirs = ( dir_tree( 'AB', 6, 1, 2 ), dir_tree( 'BC', 6, 1, 2 ), ); my @subdirs = ( dir_tree( 'F', 6, 1, 5 ), ); for my $dir (@dirs) { for my $subdir (@subdirs) { print "/results/$dir/$subdir \n"; } }