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

Newbie to perl scripting. I am trying to read a file and append some test to it. my code works fine on windows but behaves very differently on Linux.

use strict; sub main{ my $configFile="/home/testuser/perl/config.properties"; #my $configFile="C:/Projects/perl/win_config.properties"; my $rootLoc ="NA"; if(-e $configFile){ # Open file to read if(open(DATAP, "<$configFile")){ # iterate through index file. while(<DATAP>){ my @splitLine = split('=', $_); if($splitLine[0] eq "rootLoc"){ chomp $splitLine[1]; $rootLoc = $splitLine[1]; } } #close data file. close( DATAP ); } } else{ print "***Config file does not exist***\n"; } print "\$rootLoc -> $rootLoc \n"; if(!($rootLoc eq "NA")){ my $srcDirectory = $rootLoc . "src/*"; my $srcWorkDir = $rootLoc . "work/"; my $procDir = $rootLoc."processed/"; my $errDir = $rootLoc."error/"; print "$srcDirectory\n"; print "$srcWorkDir\n"; print "$procDir\n"; print "$errDir\n"; } } main();

Output on windows:

$rootLoc -> C:/Projects/perl/output/ C:/Projects/perl/output/src/* C:/Projects/perl/output/work/ C:/Projects/perl/output/processed/ C:/Projects/perl/output/error/
but out put on Linux:
rootLoc -> /home/testuser/perl/ src/*/testuser/perl/ work//testuser/perl/ processed/user/perl/ error/testuser/perl/
What am i doing wrong...
expected output on Linux:
/home/testuser/perl/src/* /home/testuser/perl/work/ /home/testuser/perl/processed/ /home/testuser/perl/error/

Replies are listed 'Best First'.
Re: Script behaving differently on Linux vs windows
by Corion (Patriarch) on May 16, 2016 at 17:50 UTC
    my $configFile="/home/testuser/perl/config.properties"; ... chomp $splitLine[1];

    The file contains Windows newlines (\r\n), but you only remove the trailing newline \n, leaving the \r at the end.

    Instead of chomp, remove all whitespace from the end of each line:

    $splitLine[1] =~ s!\s*$!!;
      thank you...that fixed the issue..
Re: Script behaving differently on Linux vs windows
by choroba (Cardinal) on May 16, 2016 at 17:51 UTC
    Your input file has DOS line endings, i.e. even after chomping the line, it still ends in a "\r". This special character moves the cursor to the beginning of line without moving to the next line, and the concatenated string than overwrites the beginning of the line.

    Run your input file through dos2unix or fromdos on Linux.

    ($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: Script behaving differently on Linux vs windows
by stevieb (Canon) on May 16, 2016 at 18:19 UTC

    Are you needing to write back to the file as well afterwards? Your opening sentence makes it unclear.