mrbbq has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I have a pl script that is normally run from a window machine and hits against an smb path. Well the box is going away so I am trying to port the script to a Linux friendly format but I am failing. Can someone please give some advice?

Here is the original script

#!/usr/bin/perl # # PlayBuild-Check.pl v1.0.0 # # Reads PlayBuild.txt and PlayBuildXMAC in the desired folder and look +s for .AHV data. # Outputs logs of builds that can be removed from text files. # my $c_streamingBuildData = "\\\\streaming.myco.net\\streaming\\builds" +; my $c_streamingPlayBuildData = "\\\\streaming.myco.net\\streaming\\Pla +yBuildData"; use strict; use warnings; my @validLangs = ( #"base", "enUS", ); #--------------------------------------------------------------------- +---------- # Parse through PlayBuild.TXT's and output results #--------------------------------------------------------------------- +---------- sub ParsePlayBuildTxt { open NEWPLAYBUILDFILE, ">", "$c_streamingPlayBuildData\\PlayBuild. +txt.good"; open PLAYBUILDLOGFILE, ">", "$c_streamingPlayBuildData\\PlayBuild. +txt.check"; open FILE, "<", "$c_streamingPlayBuildData\\PlayBuild.txt" or die +"Failed to open PlayBuild.txt"; my %buildhash; while (my $line = <FILE>) { my $success = 0; my ($build) = $line =~ s/\n//; if (!-f "$c_streamingBuildData\\${line}\\PlayBuild_IX86.AHV") { print ("${line}: PlayBuild_IX86.AHV does not exist\n"); print PLAYBUILDLOGFILE ("${line}: PlayBuild_IX86.AHV does +not exist\n"); $success = 0; } else { $success = 1; } if ($success eq 1) { # Only add unique directories to the list if (!exists $buildhash{lc("${line}")}) { $buildhash{lc(${line})} = 1; print NEWPLAYBUILDFILE ("${line}\n"); print PLAYBUILDLOGFILE ("${line}: Pass\n"); } else { print PLAYBUILDLOGFILE ("${line}: Duplicate\n"); } } } close PLAYBUILDLOGFILE; close NEWPLAYBUILDFILE; } sub ParsePlayBuildXMACTxt { open NEWPLAYBUILDFILE, ">", "$c_streamingPlayBuildData\\PlayBuildX +MAC.txt.good"; open PLAYBUILDLOGFILE, ">", "$c_streamingPlayBuildData\\PlayBuildX +MAC.txt.check"; my %buildhash; open FILE, "<", "$c_streamingPlayBuildData\\PlayBuildXMAC.txt" or +die "Failed to open PlayBuildXMAC.txt"; while (my $line = <FILE>) { my ($build) = $line =~ s/\n//; if (!-f "$c_streamingBuildData\\${line}\\PlayBuild_XMAC.AHV") { print ("${line}: PlayBuild_XMAC.AHV does not exist\n"); print PLAYBUILDLOGFILE ("${line}: PlayBuild_XMAC.AHV does +not exist\n"); } else { # Only add unique directories to the list if (!exists $buildhash{lc("${line}")}) { $buildhash{lc(${line})} = 1; print NEWPLAYBUILDFILE ("${line}\n"); print PLAYBUILDLOGFILE ("${line}: Pass\n"); } else { print PLAYBUILDLOGFILE ("${line}: Duplicate\n"); } } } close FILE; close PLAYBUILDLOGFILE; close NEWPLAYBUILDFILE; } sub renameTxtFiles { unlink "$c_streamingPlayBuildData\\PlayBuild.txt"; rename "$c_streamingPlayBuildData\\PlayBuild.txt.good", "$c_stream +ingPlayBuildData\\PlayBuild.txt"; unlink "$c_streamingPlayBuildData\\PlayBuildXMAC.txt"; rename "$c_streamingPlayBuildData\\PlayBuildXMAC.txt.good", "$c_st +reamingPlayBuildData\\PlayBuildXMAC.txt"; } #--------------------------------------------------------------------- +---------- # Main entry function #--------------------------------------------------------------------- +---------- sub Main { ParsePlayBuildTxt(); ParsePlayBuildXMACTxt(); renameTxtFiles(); } Main();

Here is what I have done to try and make the script more linux friendly. I really don't know why it's not cooperating, maybe i have just been looking at it for too long. Thanks in advance for the advice.

#!/usr/bin/perl # # PlayBuild-Check.pl v1.0.0 # # Reads PlayBuild.txt and PlayBuildXMAC in the desired folder and look +s for .AHV data. # Outputs logs of builds that can be removed from text files. # my $c_streamingBuildData = "/u01/www/streaming/builds"; my $c_streamingPlayBuildData = "/u01/www/streaming/PlayBuildData"; use strict; use warnings; my @validLangs = ( #"base", "enUS", ); #--------------------------------------------------------------------- +---------- # Parse through PlayBuild.TXT's and output results #--------------------------------------------------------------------- +---------- sub ParsePlayBuildTxt { open NEWPLAYBUILDFILE, ">", "$c_streamingPlayBuildData/PlayBuild.t +xt.good"; open PLAYBUILDLOGFILE, ">", "$c_streamingPlayBuildData/PlayBuild.t +xt.check"; open FILE, "<", "$c_streamingPlayBuildData/PlayBuild.txt" or die " +Failed to open PlayBuild.txt"; my %buildhash; while (my $line = <FILE>) { my $success = 0; my ($build) = $line =~ s/\n//; #if (!-f "$c_streamingBuildData\\${line}\\PlayBuild_IX86.AHV") if (!-f "$c_streamingBuildData/${line}/PlayBuild_IX86.AHV") { print ("${line}: PlayBuild_IX86.AHV does not exist\n"); print PLAYBUILDLOGFILE ("${line}: PlayBuild_IX86.AHV does +not exist\n"); $success = 0; } else { $success = 1; } if ($success eq 1) { # Only add unique directories to the list if (!exists $buildhash{lc("${line}")}) { $buildhash{lc(${line})} = 1; print NEWPLAYBUILDFILE ("${line}\n"); print PLAYBUILDLOGFILE ("${line}: Pass\n"); } else { print PLAYBUILDLOGFILE ("${line}: Duplicate\n"); } } } close PLAYBUILDLOGFILE; close NEWPLAYBUILDFILE; } sub ParsePlayBuildXMACTxt { open NEWPLAYBUILDFILE, ">", "$c_streamingPlayBuildData/PlayBuildXM +AC.txt.good"; open PLAYBUILDLOGFILE, ">", "$c_streamingPlayBuildData/PlayBuildXM +AC.txt.check"; my %buildhash; open FILE, "<", "$c_streamingPlayBuildData/PlayBuildXMAC.txt" or d +ie "Failed to open PlayBuildXMAC.txt"; while (my $line = <FILE>) { my ($build) = $line =~ s/\n//; #if (!-f "$c_streamingBuildData\\${line}\\PlayBuild_XMAC.AHV") if (!-f "$c_streamingBuildData/${line}/PlayBuild_XMAC.AHV") { print ("${line}: PlayBuild_XMAC.AHV does not exist\n"); print PLAYBUILDLOGFILE ("${line}: PlayBuild_XMAC.AHV does +not exist\n"); } else { # Only add unique directories to the list if (!exists $buildhash{lc("${line}")}) { $buildhash{lc(${line})} = 1; print NEWPLAYBUILDFILE ("${line}\n"); print PLAYBUILDLOGFILE ("${line}: Pass\n"); } else { print PLAYBUILDLOGFILE ("${line}: Duplicate\n"); } } } close FILE; close PLAYBUILDLOGFILE; close NEWPLAYBUILDFILE; } sub renameTxtFiles { unlink "$c_streamingPlayBuildData/PlayBuild.txt"; rename "$c_streamingPlayBuildData/PlayBuild.txt.good", "$c_streami +ngPlayBuildData/PlayBuild.txt"; unlink "$c_streamingPlayBuildData/PlayBuildXMAC.txt"; rename "$c_streamingPlayBuildData/PlayBuildXMAC.txt.good", "$c_str +eamingPlayBuildData/PlayBuildXMAC.txt"; } #--------------------------------------------------------------------- +---------- # Main entry function #--------------------------------------------------------------------- +---------- sub Main { ParsePlayBuildTxt(); ParsePlayBuildXMACTxt(); renameTxtFiles(); } Main();

Replies are listed 'Best First'.
Re: Porting Code from windows to *nix help
by ikegami (Patriarch) on Jun 29, 2011 at 20:18 UTC
    Could you isolate the part that's not working, please?

      Hi sorry for TMI, below is the part that is not working, in that it doesnt seem to get the correct information from the dir and returns : PlayBuild_XMAC.AHV does not exist

      Seems to fail in here

      open NEWPLAYBUILDFILE, ">", "$c_streamingPlayBuildData/PlayBuild.t +xt.good"; open PLAYBUILDLOGFILE, ">", "$c_streamingPlayBuildData/PlayBuild.t +xt.check"; open FILE, "<", "$c_streamingPlayBuildData/PlayBuild.txt" or die " +Failed to open PlayBuild.txt"; my %buildhash; while (my $line = <FILE>) { my $success = 0; my ($build) = $line =~ s/\n//; #if (!-f "$c_streamingBuildData\\${line}\\PlayBuild_IX86.AHV") if (!-f "$c_streamingBuildData/${line}/PlayBuild_IX86.AHV") { print ("${line}: PlayBuild_IX86.AHV does not exist\n"); print PLAYBUILDLOGFILE ("${line}: PlayBuild_IX86.AHV

        Maybe there is trailing whitespace at the end of $line (and then in $build)?

        You should output the full name of the file you are checking.

        $line surely contains at least a newline to be removed with chomp. (Upd: I didn't notice that you do remove the newline. Sorry. Note that $build contains junk. )

        Furthermore, unless you gave me only half the error message, $line appears to contain nothing other then the newline. Maybe you need something like

        while (my $line = <FILE>) { chomp; # Or more thorough: $line =~ s/\s+\z//; next if !length($line);
Re: Porting Code from windows to *nix help
by graff (Chancellor) on Jun 30, 2011 at 03:47 UTC
    Is your linux machine going to be using smb and ms-windows-style paths with back-slashes as directory-level delimiters? I'm blissfully ignorant of smb issues, and all my linux usage assumes NFS with forward slashes -- if there's a way for perl on linux to DWIM given a literal path string like '\\level1\level2\file.name', that's news to me.

    If the OP code is failing in a linux environment, I would assume that's because you're using literal back-slashes in your directory path strings, when you should be using forward slashes -- or better yet, using File::Spec so that your script can specify directory paths in an OS-agnostic manner.

Re: Porting Code from windows to *nix help
by Anonymous Monk on Jun 30, 2011 at 01:03 UTC
    Hi,

    I had a problem like this where the case of the file names was slightly off between the 2 operating systems.

    Worth a check?

    J.C.
Re: Porting Code from windows to *nix help
by locked_user sundialsvc4 (Abbot) on Jun 30, 2011 at 12:22 UTC

    The stuff that usually bites you in the asterisk – and when it does bite, it bites hard – is stupid-but-important stuff, namely, “forward vs. backward slashes.”   But things like unicode and even blank spaces can be a real problem, too.   If I know that code needs to be portable between platforms, I push the necessary manipulation duties off to various CPAN modules, generally in the File:: namespace, which are designed to be operating system aware.   Some of these take the approach of letting you “always use one kind of slash, and if need be I’ll automagically substitute the other.”   Others are even more abstracted, letting you provide directory-paths as an arrayref and so on.   But in any case, somebody else is supposed to have already pulled their hair out over the issue, and to have built self-tests to prove that you won’t have to pull-out yours.   (A rapidly-increasingly urgent consideration, at least for me:   I can’t spare the stuff anymore.)

    The catch-22 is that you usually don’t start out this way.   The need to port your code usually comes up after you’ve written it.   This obliges you, then, to a more or less annoying (as the case may be) amount of rework.