Perl :- Temporary Files

Background

In the middle of troubleshooting a fading Perl Script.

Error

Thankfully, I have an error message it reads.

Error Text

error.removingbinfile.01.20190807.1145AM.png

Error Image


Error removing C:/Users/dadeniji/AppData/Local/Temp/tmp_myAppZay.bin

Trouble Shooting

Code Review

I reviewed the code and here is what it looks like.

Snippet


my $file;
$file = tempfile
         (
            'tmp_therapy_XXXX'
            , SUFFIX => '.bin'
            , DIR => $TMP_DIR
            , UNLINK => 1
         );

Explanation

  1. Read up more and determined that the UNLINK says
    • Upon app completion , do clean up
    • Remove all temporary files

Sample Code

Code Snippet


#!/usr/bin/perl
use strict;
use warnings;
use File::Temp qw(tempfile);
use File::Basename;

my $username;
my $format;
my $learning;
my $fileTemp;
my $ts;

my $moduleName;
my @suffixlist;
my $moduleMajor;
my $fileNameTempFilePrefix;

# dadeniji remove tempfile or not
my $tempFileUnlink;

$tempFileUnlink=1;
$tempFileUnlink=0;

#get module name
$moduleName = basename($0);
$moduleMajor = basename($moduleName, @suffixlist);

$username =  $ENV{'USERNAME'}; 

$learning = "Who is learning Perl Today ? $username\n"; 

print $learning; 

$fileNameTempFilePrefix = "$moduleMajor" . "XXXXX";

# remove the file when the reference goes away with the UNLINK option
$fileTemp = new File::Temp
	(
		  $fileNameTempFilePrefix
		, SUFFIX => '.tmp'
		, UNLINK => $tempFileUnlink
	);

# print filename
print "temp filename: $fileTemp\n";

# write into $file
print $fileTemp "Module name is $moduleName\n";

# write into $file
print $fileTemp $learning;

Output

fs.sample.01.20190807.1212PM.PNG

Explanation

The little application I placed together works.

 

Trouble Shooting More

SysInternals

Process Monitor

Launched SysInternals’ process Monitor.

Filtered on my designated temp folder.

Tracked the error down to a sharing violation.

Source Code Control

GitHub

DanielAdeniji\perlSample
Link

 

Dedicated

Dedicating to Larry Wall.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s