skip to Main Content

We recently inherited a somewhat legacy perl application and we’re working to migrate to a new server as well as setup a sandbox environment so we can start sorting the application out. The issue we’re having is the code currently uses some relative paths for the open method. And currently works in their shared hosting environment (we have little access to)

open(HANDLE,"<../relative/path/to/file.txt")

We pulled all of the code, paths, etc. over and for the most part have the application up and running until we run into one of the scripts that does the above, opens a file with a relative path. Then it fails.

If we run the code via the command line, the relative paths work. If we modify the path to be the full path it works both via command line and through Apache (navigating to the page in the browser).

This makes me think there is some module or configuration option we need to set in Apache to allow for the perl scripts to access or use the open command with relative paths?

4

Answers


  1. Chosen as BEST ANSWER

    As mentioned in the comment above. Our final solution was to use:

    File::Basename qw( dirname ); 
    chdir(dirname($0));
    

    This allowed us to get the code working while also kept additional code modifications to a minimum.


  2. Relative paths are relative to the process’s current work directory, which is not necessarily the same as a the directory containing the program. It fact, it’s often / for daemons (and thus their children).

    For paths relative to the program’s location, use

    use FindBin qw( $RealBin );
    
    my $qfn = "$RealBin/relative/path/to/file.txt";
    open(my $HANDLE, "<", $qfn)
    
    Login or Signup to reply.
  3. It is bad practice to specify file’s path in open with a fixed string unless the path is predefined and never change – as for example with /etc/fstab in linux.

    You should change the code to use variable(s) instead.

    Define variable at the top of the script — in feature if you need to change base and path you will know that you find it at the few first lines of the code.

    In such situation add temporary in the code something as

    use strict;
    use warnings;
    
    open( my $fh, '>', 'my_uniq_file.txt')
        or die 'Couldn't open my_uniq_file.txt';
    
    print $fh 'This directory is default base for path location';
    
    close $fh;
    

    Once you run your script from webserver you should look for the file my_uniq_tile.txt — it’s location will be default base of web server for the file(s) location.

    Now adjust variables with file path accordingly.

    use strict;
    use warnings;
    
    my $dir_project = './project_1/';
    my $audio_data = 'audio_data.dat';
    my $video_data = 'video_data.dat';
    my $descr_data = 'descr_data.dat';
    
    my $qfn = $dir_project . $audio_data;
    
    open(my $fh, '<', $qfn)
        or die "Couldn't open $qfn";
    
    while( <$fh> ) {
         chomp;
        [do something with data]
    }
    
    close $fh;
    
    Login or Signup to reply.
  4. I was running a script out of cgi-bin and wanted to open a template in htdocs/templates so I did this in my script…

    # open the html template
    my $filename = sprintf("%s/templates/test.tmplt", $ENV{DOCUMENT_ROOT});
    my $template = HTML::Template->new(filename => $filename);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search