CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Writing to file not working

 



aha
New User

Nov 29, 2013, 6:54 AM

Post #1 of 4 (928 views)
Writing to file not working Can't Post

Hi guys,

Apologies if this is a really easy issue - I've only just started using PERL! The basic problem I have is that I have modified a script (which moves some files around) to try and add a logging of the files involved and, while it starts writing to the file, it doesn't add any of the other logging actions. Script is below :-

#!/usr/bin/perl
# for the ~/scripts directory
# script to move/rename files older than 5 minutes from the infobus directory
# to the FS inbound directory, prefixing their name with "SEC_INFOBUS_"
#

use strict;
use File::Basename;
use File::Copy;

# First, switch to home directory
chdir $ENV{'HOME'};

my $pickup_dir = $ENV{'HOME'} . '/data/SEC_infobus/from_infobus'; #// DIRECTORY TO SCAN FOR edi files
my $out_dir = $ENV{'HOME'} . '/data/inbound'; #// DIRECTORY TO MOVE FILES TO
my $http_doc_poster = $ENV{'HOME'} . '/programs/gisdocposter/run_unix.sh'; #// GIS DOC POSTER PROGRAM

`ln scripts/LEAVE_ME_ALONE scripts/GISDOCPOSTER_LOCKFILE.TMP`;
if ($? > 0) {
die 'ABORTING: LOCK EXISTS scripts/GISDOCPOSTER_LOCKFILE.TMP';
}

my $LOGFile = $ENV{'HOME'} . '/INFOBUS_INBOUND.LOG';
if ( ! open LOGFile, ">>$LOGFile") {die "Can't open LOG file: $!"};
my $CurrDate = gmtime;
printf LOGFile ("LOG Process Date/Time - " . $CurrDate . "\n");

# Look for EDI files (810,856)
foreach(glob($pickup_dir . "/*.{edi,EDI}")) {
my $age = -M $_;
my $pfound = $_;
my $found = basename($pfound, "");
printf LOGFILE ("Found " . $found . "==>");
# the below is about 10 seconds, in days..
if ($age > 0.00012) {
my $type = "";
# check what it is , rename to 856 if necessary
open(my $in, "<", $pfound) or die "Can't open input file: $!";
while (<$in>) {
if ($_ =~ /ST\*856\*0/) {
$type = "856";
}
}
my $outfile = $out_dir . "/" . "SEC" . $type . "_INFOBUS_" . $found;
move($pickup_dir . "/" . $found, $outfile);
printf LOGFILE (" moved file " . $pickup_dir . "/" . $found . " to " . $outfile . "\n");
} else {
printf LOGFILE (" file is too young to move\n");
}
}

# Look for XML files (GETS,GMCV,OBS,etc)

foreach(glob($pickup_dir . "/*.{xml,XML,Xml}")) {
my $age = -M $_;
my $pfound = $_;
my $found = basename($pfound, "");
printf LOGFILE ("Found " . $found . "==>");
# the below is about 10 seconds, in days..
if ($age > 0.00012) {
my $to_send = $pickup_dir . "/" . $found;
printf LOGFILE ("running http gis doc poster\n");
my $sout = `sh $http_doc_poster $to_send`;
print $sout;
} else {
printf LOGFILE (" file is too young to move\n");
}
}

close LOGFile;
`unlink scripts/GISDOCPOSTER_LOCKFILE.TMP`;

===

Basically, I added the coding to create the LOG file and then amended the script to write to LOGFILE. So, the first write works (adding the date and time) but none of the other show up. All the other actions of the script work as expected.

Any help would be greatly appreciated!!


aha
New User

Nov 29, 2013, 7:05 AM

Post #2 of 4 (925 views)
Re: [aha] Writing to file not working [In reply to] Can't Post

Should have mentioned the Perl version

$ perl -v

This is perl, v5.8.8 built for aix-thread-multi

Copyright 1987-2006, Larry Wall


In Reply To
Hi guys,

Apologies if this is a really easy issue - I've only just started using PERL! The basic problem I have is that I have modified a script (which moves some files around) to try and add a logging of the files involved and, while it starts writing to the file, it doesn't add any of the other logging actions. Script is below :-

#!/usr/bin/perl
# for the ~/scripts directory
# script to move/rename files older than 5 minutes from the infobus directory
# to the FS inbound directory, prefixing their name with "SEC_INFOBUS_"
#

use strict;
use File::Basename;
use File::Copy;

# First, switch to home directory
chdir $ENV{'HOME'};

my $pickup_dir = $ENV{'HOME'} . '/data/SEC_infobus/from_infobus'; #// DIRECTORY TO SCAN FOR edi files
my $out_dir = $ENV{'HOME'} . '/data/inbound'; #// DIRECTORY TO MOVE FILES TO
my $http_doc_poster = $ENV{'HOME'} . '/programs/gisdocposter/run_unix.sh'; #// GIS DOC POSTER PROGRAM

`ln scripts/LEAVE_ME_ALONE scripts/GISDOCPOSTER_LOCKFILE.TMP`;
if ($? > 0) {
die 'ABORTING: LOCK EXISTS scripts/GISDOCPOSTER_LOCKFILE.TMP';
}

my $LOGFile = $ENV{'HOME'} . '/INFOBUS_INBOUND.LOG';
if ( ! open LOGFile, ">>$LOGFile") {die "Can't open LOG file: $!"};
my $CurrDate = gmtime;
printf LOGFile ("LOG Process Date/Time - " . $CurrDate . "\n");

# Look for EDI files (810,856)
foreach(glob($pickup_dir . "/*.{edi,EDI}")) {
my $age = -M $_;
my $pfound = $_;
my $found = basename($pfound, "");
printf LOGFILE ("Found " . $found . "==>");
# the below is about 10 seconds, in days..
if ($age > 0.00012) {
my $type = "";
# check what it is , rename to 856 if necessary
open(my $in, "<", $pfound) or die "Can't open input file: $!";
while (<$in>) {
if ($_ =~ /ST\*856\*0/) {
$type = "856";
}
}
my $outfile = $out_dir . "/" . "SEC" . $type . "_INFOBUS_" . $found;
move($pickup_dir . "/" . $found, $outfile);
printf LOGFILE (" moved file " . $pickup_dir . "/" . $found . " to " . $outfile . "\n");
} else {
printf LOGFILE (" file is too young to move\n");
}
}

# Look for XML files (GETS,GMCV,OBS,etc)

foreach(glob($pickup_dir . "/*.{xml,XML,Xml}")) {
my $age = -M $_;
my $pfound = $_;
my $found = basename($pfound, "");
printf LOGFILE ("Found " . $found . "==>");
# the below is about 10 seconds, in days..
if ($age > 0.00012) {
my $to_send = $pickup_dir . "/" . $found;
printf LOGFILE ("running http gis doc poster\n");
my $sout = `sh $http_doc_poster $to_send`;
print $sout;
} else {
printf LOGFILE (" file is too young to move\n");
}
}

close LOGFile;
`unlink scripts/GISDOCPOSTER_LOCKFILE.TMP`;

===

Basically, I added the coding to create the LOG file and then amended the script to write to LOGFILE. So, the first write works (adding the date and time) but none of the other show up. All the other actions of the script work as expected.

Any help would be greatly appreciated!!



BillKSmith
Veteran

Nov 29, 2013, 7:50 AM

Post #3 of 4 (922 views)
Re: [aha] Writing to file not working [In reply to] Can't Post

Perl symbols are case sensitive. You open the filehandle LOGFile. You print the date to this handle. All other prints are to the handle LOGFILE. If you had used the pragma "use warnings;", you would have received a warning "Attempt to print to an unopen filehandle.....". With this much help, you probably could have solved the problem yourself.

I am sure that other gurus will have comments on your style. I am trying to answer your main problem as quickly as possible.
Good Luck,
Bill


aha
New User

Nov 29, 2013, 8:42 AM

Post #4 of 4 (917 views)
Re: [BillKSmith] Writing to file not working [In reply to] Can't Post

Well there's a "Duh!!" moment! Blush

I've been looking at this code for too long I think! I do know about the case sensitivity - but still not fixed in me yet as I'm coming from Windows!

Thanks for the quick response - I'll turn on the warnings now as well.

Don't mind people critiquing the code - wasn't mine to begin with! Smile

Many thanks Bill!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives