Reputation: 41
I have a Perl script written on Server 2008. It works fine here.
I have copied it to my laptop, which is running Windows 10 Home Edition, Version 1993, OS Build 18362.959.
I also download Active Perl, for the very first time.
The script basically takes an input file and applies regular expressions to the content and then outputs the results to a file.
On Windows 10 it is not writing to the file, the file is not even created.
I have done a search on this issue but have not found a solution.
I tried the following code, found on one on the reply to the same issue. But it does not create or writes to the file. It works fine on server 2008. Am I missing something? As mentioned this is the first time I downloaded ActivePerl version
Perl Version Details are as follows:
This is perl 5, version 28, subversion 1 (v5.28.1) built for MSWin32-x64-multi-thread
(with 1 registered patch, see perl -V for more detail)
Copyright 1987-2018, Larry Wall
Binary build 0000 [58a1981e] provided by ActiveState http://www.ActiveState.com
Built Apr 10 2020 17:28:14
Perl code is as follows:
use strict;
use IO::File;
my $FileHandle = new IO::File;
$FileName = "C:\\Users\\moons\\Documents\\Personal Planning\\Shopping\\ShoppingList.txt";
open ($FileHandle, "<$FileName") || print "Cannot open $FileName\n";
local $/;
my $FileContents = <$FileHandle>;
close($FileHandle);
$FileContents =~ s/(Add|Bad|Limit|Each).*\n|Add$|\nWeight\n\d{1,}\nea|\$\d{1,}\.\d\d\/100g\n//g;
Do more Regular expressions.
$FileContents =~ s/(.*)\n(.*)\n(\$\d{1,}\.\d\d)/$1,$3,$2/g;
printf $FileContents;
Above code works. Code below does not create or write to file.
$OutFile = "C:\\Users\\moons\\Documents\\Personal Planning\\Shopping\\test.txt";
$FileHandle = new IO::File;
open ($FileHandle, ">$OutFile") || print "Cannot open $OutFile\n";
printf $FileHandle $FileContents;
close($FileHandle);
Upvotes: 0
Views: 379
Reputation: 386561
Always use use strict; use warnings;
.
my $OutFile = "C:\Users\moons\Documents\Personal Planning\Shopping\test.txt";
results in
Unrecognized escape \m passed through at a.pl line 3.
Unrecognized escape \D passed through at a.pl line 3.
Unrecognized escape \P passed through at a.pl line 3.
Unrecognized escape \S passed through at a.pl line 3.
You could use
my $OutFile = "C:\\Users\\moons\\Documents\\Personal Planning\\Shopping\\test.txt";
Whole thing:
use strict;
use warnings;
my $in_qfn = "C:\\Users\\moons\\Documents\\Personal Planning\\Shoppin\\ShoppingList.txt";
my $out_qfn = "C:\\Users\\moons\\Documents\\Personal Planning\\Shopping\\test.txt";
open(my $in_fh, '<', $in_qfn)
or die("Can't open \"$in_qfn\": $!\n");
open(my $out_fh, '>', $out_qfn)
or die("Can't create \"$out_qfn\": $!\n");
my $file;
{
local $/;
$file = <$in_fh>;
}
for ($file) {
s/(Add|Bad|Limit|Each).*\n|Add$|\nWeight\n\d{1,}\nea|\$\d{1,}\.\d\d\/100g\n//g;
s/(.*)\n(.*)\n(\$\d{1,}\.\d\d)/$1,$3,$2/g;
}
print $file;
Upvotes: 1