Hi Perlmonks,
My interest is to create a text file z.txt inside a subfolder y of the main folder x. I want to write in the text file using a file handle like $fh. I searched for perl examples online but did not find one which can solve my problem. I have written a script x4.pl which creates folders x and y and not the file. The result in cmd shows "Cannot open file 'x/y/z.txt'". I welcome suggestions from the perlmonks to sort out this problem.
Here goes my script x4.pl:
#!/usr/bin/perl use strict; use warnings; my $x='x'; my $y='y'; my $dirname = "$x/$y"; my @folders = split /\/|\\/, $dirname; map {mkdir $_; chdir $_;} @folders; # Print output to a Text File: 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"; exit;
Here goes the result of cmd:
Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users>cd d* C:\Users\Desktop>x4.pl Cannot open file 'x/y/z.txt'. C:\Users\Desktop>
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |