Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Use Mkdir to create dir from list

by GregorySRobinson (Initiate)
on Jun 19, 2012 at 04:27 UTC ( #976955=perlquestion: print w/replies, xml ) Need Help??
GregorySRobinson has asked for the wisdom of the Perl Monks concerning the following question:

Below is proof of concept code: My question is what is correct syntax to replace print with mkdir (useFile::Path Module)? End goal is to create directories from an ASCII file where each line is the directory to make.

  • Example of line by line entries in file being read:
  • 101-Makeup_Air_Handlers-Cleanroom
  • 102-Recirc_Air_Handlers-Cleanroom
  • 103-Makeup_Air_Handlers-General
  • 104-General_Air_Handling_System
  • 105-Air_Condensing_Cooling_Heating_Coils
#!/usr/bin/perl -w <-- Not required on Windows, find -w arg??? # Perl v5.14.2 Last Edit GSR 2012-06-22 12:00 # # Create subroutine to make directories from ASCII file containing # a line of text without spaces representing the directories use strict; use warnings; use diagnostics; # use File::Path qw(make_path); <-- recommended by PerlMonks and works + use File::Path qw(make_path remove_tree); # Scaler string my $GSR_dir = "F:/sandbox/"; # Scaler string my $GSR_file = "UPN_List12.06.02.txt"; # append the scalers / strings my $GSR_file2read = join ('/',$GSR_dir, $GSR_file,); # Ok, lets go open(my $GSR_fh, "<", $GSR_file2read) or die "cannot open <, String: $!"; # this reads the content of the file handle (file) into the array vari +able # but a pointer to the file handle is at the end of the file so any # subsequent use of <$GSR_fh> yields null my @thefiles = <$GSR_fh>; # see if we have list in var # print @thefiles; <-- Works so we use that array # See if we have string in vars # print $GSR_dir; <-- Works # print $GSR_file; <-- Works # Using line below proved the module loaded and executed.... # make_path 'F:/sandbox/123_water'; # Line below creates directories in resident directory of program exec +ution make_path ( map {chomp; $_} @thefiles ); # Can join append string to the file I want made? close $GSR_fh;

Replies are listed 'Best First'.
Re: Use Mkdir to create dir from list
by frozenwithjoy (Priest) on Jun 19, 2012 at 04:45 UTC

      Yes, and preformed a number of google searches Have also printed out most of the Perl manuals and spend time searching for the logic behind why my code as is does not preform as expected.

        Then consider looking at the debug facilities that File::Path offers for make_path. Especially the error facility.

        Also, from your code, it is not clear to me which statements are supposed to execute together and which are to demonstrate your problem. Especially the following two lines:

        my @thefiles = <$GSR_fh>; ... make_path(map {chomp} <$GSR_fh>);

        Once you've read a filehandle (like $GSR_fh) to its end, you need to seek to the start again before you can read the file again. But why re-read the file if you can use the data in @thefiles?

Re: Use Mkdir to create dir from list
by rovf (Priest) on Jun 19, 2012 at 08:31 UTC

    CODE UPDATED (bugfix)


    use File::Path qw(make_path); make_path(map {chomp; $_} <$GSR_fh>);
    If you need error handling, have a look at File::Path.

    Ronald Fischer <>

      Huh? chomp does NOT return $_! You will need map { chomp; $_ }

      $ perl -MDP -E'@_=map{$_."\n"}1..5;DDumper[@_]' [ '1 ', '2 ', '3 ', '4 ', '5 ' ] $ perl -MDP -E'@_=map{$_."\n"}1..5;DDumper[map{chomp}@_]' [ 1, 1, 1, 1, 1 ] $ perl -MDP -E'@_=map{$_."\n"}1..5;DDumper[map{chomp;$_}@_]' [ 1, 2, 3, 4, 5 ]

      Enjoy, Have FUN! H.Merijn
        Of course you are right! Silly mistake, thanks for pointing it out. I will update my node.

        Ronald Fischer <>

      I spent time looking at and testing your solution. As presented and as I interpreted the solution I remains stumped as to how to preform objective. I posted the revised code.Oh, and thank you.

Re: Use Mkdir to create dir from list
by Your Mother (Chancellor) on Jun 20, 2012 at 13:21 UTC

    I think you have a couple problems.

    # make_path(map {chomp} <$GSR_fh>); # make_path(map {chomp} <$GSR_fh>);

    chomp returns its success/fail status, not the $_. So you're probably going to just feed a list of 1s to make_path. You could change it to map { chomp; $_ }.

    And the map is consuming/reading the whole filehandle. So if you're really doing the second make_path, you will end up with no input at all. Update: Corion already caught this and more.

      Currently Studying the threads and attempting to understand each replies ramifications. I believe I may have an issue with my code having access to File::Path so working to verify the module is active now. At this moment no errors with current version. Yet no directories as well.

      As of 2012.06.22 recommended solutions do not provide directories from list in ASCII file. Still reading through manuals and looking for error codes to understand problem.

Re: Use Mkdir to create dir from list
by aaron_baugher (Curate) on Jun 22, 2012 at 16:13 UTC

    Look at the first two lines below, taken from your code. The first reads all the lines from the file pointed to by $GSR_fh into the array @thefiles. The second reads all the lines from the file pointed to by $GSR_fh, chomps them, and passes the chomped values to make_path. But there aren't any lines in the file to read at this point, because they were all read by the first line. Either remove the first line or use the array in your second line, as in the third line below.

    my @thefiles = <$GSR_fh>; make_path ( map {chomp; $_} <$GSR_fh>); make_path ( map {chomp; $_} @thefiles );

    Aaron B.
    Available for small or large Perl jobs; see my home node.

      Thanks for the explanation - Makes sense. Lots of joy hitting execute and seeing those directories appear!!! Now on to next step. Append directory string (scaler in Perl) to the directory being created.)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://976955]
Approved by Old_Gray_Bear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2017-08-22 11:07 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (333 votes). Check out past polls.