#!/usr/bin/perl use strict; use warnings; use File::Path qw(make_path); my $x='x'; my $y='y'; my $dirname = "$x/$y"; make_path( $dirname , { chmod => 0777, }); my $output="$dirname/z.txt"; open ( my $fh, ">", $output ) or die "Cannot open file '$output': $!\n"; print $fh "\n It's ok.\n"; close $output; print "\n Program is over.\n"; #### D:\code>perl path.pl Program is over. D:\code>cd x/y D:\code\x\y>dir Volume in drive D is Data Volume Serial Number is 3241-B96B Directory of D:\code\x\y 22/04/2017 10:39 . 22/04/2017 10:39 .. 22/04/2017 10:39 13 z.txt 1 File(s) 13 bytes 2 Dir(s) 107,416,027,136 bytes free D:\code\x\y>cat z.txt It's ok. D:\code\x\y>