#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    Trouble with input files for perl script


    hi, I'm having trouble running a perl script in both windows 7 and ubuntu

    in windows when I try to run it it states the input file is specified twice

    in ubuntu it claims it can't find the input file to open even though its in the same directory as the pl file I'm running.

    here is the code up to the error point which occurs around the system function.

    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    use File::Spec;
    
    
    my $inputFile = $ARGV[0];
    my $backgroudFile = $ARGV[1];
    
    #get name of the tester file and set it as the current working directory 
    my ($inputFileVolume,$inputFileDir,$name) = File::Spec->splitpath($inputFile);
    my @workingDirArr = ($inputFileVolume,$inputFileDir);
    my $workingDir = File::Spec->catdir(@workingDirArr);
    chdir $workingDir or die "$0 failed to chdir to working dir $workingDir";
    
    system ("sort -k 1,1 -k 4n -T $workingDir $name >tempLoc_$name")==0 or die "$0 failed to sort $name";
    open (NAM, "tempLoc_$name") or die "$0 failed to open file tempLoc_$name";
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    843
    Rep Power
    496
    Where do you get the error?

    Try to print the argument to see if it retrieves it correctly:

    Code:
    print "argument = /$inputFile/ \n";
    (The "/" will enable you to see if there is any additional character that you don't see, such as a carriage return or whatever.)

    Do the same (printing them) with the various variables you are using.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    Windows' sort utility has a different command line than POSIX's sort. Unless you have installed your own sort utility you need to check $^O and format the command line appropriately (if you have installed a POSIX sort on windows, double check the value of $PATH to ensure that you're running what you intend).
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0
    Originally Posted by Laurent_R
    Where do you get the error?

    Try to print the argument to see if it retrieves it correctly:

    Code:
    print "argument = /$inputFile/ \n";
    (The "/" will enable you to see if there is any additional character that you don't see, such as a carriage return or whatever.)

    Do the same (printing them) with the various variables you are using.
    The print statement gives me argument = /inputFile.txt/

    The error msg is sort: open failed : inputFile.txt: No such file or directory even though there is an inputFile.txt in the same directory the .pl file is running from.

    The error seems to be comeing from the line 20-22 where the system function is called in the code above
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    Look at $workingDir, $inputFileVolume and $inputFileDir as well as `pwd`.

    My bet is that they're empty (since you didn't supply an absolute path to your script) and the chdir dropped you into the root directory.
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0
    Originally Posted by OmegaZero
    Look at $workingDir, $inputFileVolume and $inputFileDir as well as `pwd`.

    My bet is that they're empty (since you didn't supply an absolute path to your script) and the chdir dropped you into the root directory.

    Okay so yeah they're

    argument = ///
    argument = //
    argument = //
    argument = /'pwd'/

    how do I fix it? I tried replace the two arguments with full paths to the file but it choked on line 18.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    843
    Rep Power
    496
    The printout I asked shows that the argument passed to your program is simply "input_file.txt". Your program is then trying to extract the file path from this variable in the File::Spec->splitpath method invocation), but since there is no path but just the bare name, the path is obviously bound to be empty or undefined.

    From there on, your script cannot work.

    As a starting poiny, you could try to pass to the program an argument containin the full path, something like "/home/user/me/input_file.txt" (or "C:\my_test\input_file.txt" if you are uner Windows)
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0
    Originally Posted by OmegaZero
    Windows' sort utility has a different command line than POSIX's sort. Unless you have installed your own sort utility you need to check $^O and format the command line appropriately (if you have installed a POSIX sort on windows, double check the value of $PATH to ensure that you're running what you intend).
    What would be a windows way of replicating

    Code:
    system ("sort -k 1,1 -k 4n -T $workingDir $name >tempLoc_$name")==0 or die "$0 failed to sort $name";
    since I suppose the double ks won't work in strawberry perl.

IMN logo majestic logo threadwatch logo seochat tools logo