in reply to Simple/Quick question about opening creating a file using perl
Start by making sure the subfolder is there (use mkdir).
my $subdir = 'subfolder'; if ( ! mkdir( $subdir ) && ! -d $subdir ) { die "Can't create '$subdir': $!"; }
Then just open the file you want to write into.
my $outfile = "$subdir/file.txt"; open my $out_fh, '>', $outfile or die "Can't write '$outfile': $!"; print $out_fh $source; close $out_fh or die "close() failed: $!";
Note that I'm using a lexical filehandle instead of a global one, and I'm using the three-argument form of open. Also, the directory/file that we make is relative to current directory when the program is executed, not to where the program actually lives. For the latter, have a look at $0.
|
|---|