top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

Perl: Locating files in sub-directories

+1 vote
116 views

I'm in the process of moving certain files of the SurfShop script into a sub-directory to clean up the main directory and would like to know the best way for the script to find these files.

I was using this method:

use FindBin qw($Bin);
use lib "$Bin/../ss_files";

which worked for me, but I read somewhere that FindBin can break in certain situations. So I tried the following:

use lib "ss_files";

This also works, but iirc, I read that it won't work across all platforms, is that correct? Is one of these methods better than the other, or is there another way that I haven't found yet?

posted Dec 16, 2013 by Garima Jain

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

This method also works, but to be equivalent with the first method it should have been:

use lib "../ss_files";

This second method has a big limitation though. If The current directory when you run the script is the directory in which is place the script, then it will work. If you change the current directory to be another directory, like /home/user for example, it won't work, because it will search for modules in /home/ss_files.

Using FindBin will always get the path to the directory in which is placed the script you run, and then the line "use lib" will set the path of modules starting from that path and not from the current directory.

Is one of these methods better than the other, or is there another way that I haven't found yet?

The first method is better because it will work even if you change the current directory. And you can run the scripts from a cron job for example.

answer Dec 17, 2013 by Amit Parthsarthi
Similar Questions
0 votes

what I'm trying to do is create a list of sha-2 sums of files the names of which are stored in a file. The purpose is to verify from time to time whether any of the listed files have been modified or not in the
meantime.

So I can read the list of file names; what I don't understand is what the most efficient way would be to create a sha-2 sum for a file.

It doesn't even need to be sha-2 since there are no security issues involved. The only purpose is to figure out reliably if any of the files on the list have been modified or not. I don't want to go by file modification dates because that isn't entirely reliable.

I've been googling for examples of how to create a sha-2 sum of a file in perl without success. What I'm looking for is something like:

 $hash = create_sha2_sum( $filename);

Do you know of any examples I could look at? Or is there a better way to figure out if a file has been modified?

+1 vote
$ perl -le 'system "df -h"'
$ perl -le 'system "df","-h"'

Both two styles work fine. What's the difference between them and which is better usage?

+1 vote

I've a program that needs to print some fields formatted in different ways according to some conditions. The solution I come up is working, but I'm looking for a suggestion for something more elegant.

What I do is something like the following:

print sprintf $formats[ $condition ], @fields;

where $condition is the condition used to select a sprintf format string out of an array (@formats) that contains something like:

my @formats = (
 qw( %09d %-1s %03d ... )
, qw(%-4s %1s %09d %1s %-150s %-4s %011d)
, ...
);

Now, while this approach is working really fine, it is a little hard to decode, especially considering that I've got some formats with 50+ fields. I don't believe that using Perl formats is a solution, it will provide a quite longer configuration (consider I've got even fields specified as "-100%s"!).

Any suggestion to get a more readable code?

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...