in reply to Re: script running on linux but not windows -- File::Spec
in thread script running on linux but not windows

Hello again and thank you for useful feedback. I am making very preliminary progress. I absolutely had to change some file paths up front for:

#set the log files

my $creationTime = strftime "%F-%R:%S", localtime;

$sqlLog = "C:/Users/thclotworthy/squirrel/logs/squirrel-sql-" . $creationTime . ".log";

$scriptLog = "C:/Users/thclotworthy/squirrel/logs/squirrel-" . $creationTime . ".log";

However, I am immediately getting error on the following line:

#Setup filehandlers for the mysql output log and the squirrel log

open($sqlFh, ">", $sqlLog) or exitOnError("Could not open $sqlLog for writing. Exiting......\n");

open($scriptFh, ">", $scriptLog) or exitOnError("Could not open $scriptLog for writing. Exiting......\n");

, where the exit on error is generated. Here is the exit on error subroutine:

sub exitOnError

{

    my $errorString = $_[0];

    print $sqlFh "$errorString\n";

    print $scriptFh "$errorString\n";

    print "$errorString\n";

    exit(9);

}

, and here is the actual failure message on the terminal:

print() on closed filehandle $sqlFh at squirrelBAK.pl line 855. Can't use an undefined value as a symbol reference at squirrelBAK.pl line 856.

, So I am guessing that sqlFh perhaps needs to be initialized and I am not doing that?. The only instance of it before the code above is at the beginning of the script with other parameters:

my $dbHost; my $dbPort; my $serviceName; my @connectionNames; my %usernames; my %passwords; my $chosenRealm; my $release; my $releasePath; my $sqlLog; my $sqlFh; my $scriptLog; my $scriptFh;

, So perhaps I supposed to be passing it as a command line argument or something? Grateful for any ideas. Thank you!

Replies are listed 'Best First'.
Re^3: script running on linux but not windows -- File::Spec
by choroba (Cardinal) on Dec 20, 2017 at 17:00 UTC
    You might be interested in the original error string ($! and $^E) which might tell you more about the problem. Don't use the filehandle to report errors related to the filehandle itself.
    open $sqlFh, '>', $sqlLog or die "Could not open $sqlLog for writing.\ +n$!\n$^E\nExiting...\n";

    BTW, do you know that double quotes interpolate variables?

    $scriptLog = "C:/Users/thclotworthy/squirrel/logs/squirrel-$creationTi +me.log";

    Also, declaring all the variables in one place usually makes their scope too wide. It's better to declare them only for the scope where they're really needed.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re^3: script running on linux but not windows -- File::Spec
by poj (Abbot) on Dec 20, 2017 at 17:55 UTC

    Check that folder C:/Users/thclotworthy/squirrel/logs exists and is writeable to.

    poj
Re^3: script running on linux but not windows -- File::Spec
by Marshall (Canon) on Dec 20, 2017 at 22:25 UTC
    As debugging 101, I ran the first part of your code (below). Making a simple example for us is good, but in the future please make it a "runnable as is" example.

    An obvious problem is that colon ":" is not valid in a Windows file name. Also regardless of that issue, the complete path does look a bit suspicious to me - that path does not exist on the Windows machine I'm writing this post upon using to write this post.

    #!/usr/bin/perl use strict; use warnings; use POSIX qw(strftime); my $creationTime = strftime "%F-%R:%S", localtime; my $sqlLog = "C:/Users/thclotworthy/squirrel/logs/squirrel-sql-" . $cr +eationTime . ".log"; my $scriptLog = "C:/Users/thclotworthy/squirrel/logs/squirrel-" . $cre +ationTime . ".log"; print "$sqlLog\n$scriptLog\n"; __END__ prints: C:/Users/thclotworthy/squirrel/logs/squirrel-sql--:23.log C:/Users/thclotworthy/squirrel/logs/squirrel--:23.log
    As an extra updated note: I use Windows as my development platform and often move programs to a Unix environment. I try to only use file names that are compatible with both Windows and Unix. For example Windows allows a "space" in the name but Unix does not.(see post by choroba - my recollection of this is was evidently wrong). So I use underscore "_" instead of that (where possible). Of course there are situations on Windows where a space is need, e.g. "My Documents". Always use "/" instead of "\" for path names. That even works from the Windows command line (which BTW is NOT DOS). There are some weird situations where the backslash is needed, but I encounter this so rarely that I can't think of such an example right now. Perl is amazingly good at being multi-platform.
      > For example Windows allows a "space" in the name but Unix does not

      That's not precisely true.

      perl -wle 'open my $fh, ">", "a b" or die $!; opendir my $dh, "." or d +ie $!; print for grep / /, readdir $dh' a b

      As you can see, the outer quotes are single, so I'm not on MSWin.

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,