How to create a directory in Perl?

I am new to Perl and trying to write text files. I can write text files to an existing directory without any problems, but in the end I would like to be able to create my own directories.

I am going to download files from the course website, and I want to put the files in the folder named after the course. I don’t want to make a folder for each course manually in advance, and I would also like to end up sharing with the script with others, so I need a way to make directories and name them based on the course names from HTML.

So far I have managed to get this to work:

use strict;
my $content = "Hello world";
open MYFILE, ">C:/PerlFiles/test.txt";
print MYFILE $content;
close (MYFILE);

test.txtdoesn't exist, but C:/PerlFiles/does and presumably prints >allows me to create files, fine.

Below, however, does not work:

use strict;
my $content = "area = pi*r^2";
open MYFILE, ">C:/PerlFiles/math_class/circle.txt";
print MYFILE $content;
close (MYFILE);

The directory C:/PerlFiles/math_class/does not exist.

sysopen, :

use strict;
my $content = "area = pi*r^2";
sysopen (MYFILE, ">C:/PerlFiles/math_class/circle.txt", O_CREAT);
print MYFILE $content;
close (MYFILE);

Perl Cookbook 7.1. . , Bareword "O_CREAT" not allowed while "strict subs" in use. , 1998 , , , O_CREAT . - , .

, ? ?

+3
2

File:: Path . dirname, , .

, open:

open(my $fd, ">", $name) or die "Can't open $name: $!";
+7

, . mkdir.

, -d $dir (. perldoc -f -X).

+8

All Articles