CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
accessing files with script

 



stevieger
Novice

Nov 26, 2012, 1:48 PM

Post #1 of 14 (2979 views)
accessing files with script Can't Post

I'm writing a simple script to make two copies of a file and put into a specific directory. As you can see in the code I'm copying from a file in the /ABC/ directory and putting them in /ABC/old/ directory. However my perl script is located in the directory /ABC/perlscript/ and when I run the script it tells me file doesn't exist at the location so my question is how do i access the contents of the folder higher up in the folder hierarchy than my script itself?



Code
#!/usr/bin/perl 
use strict;
use warnings;
use File::Copy;

my $abc = "ABC/";
my $old = "ABC/old/";

if (-e "data.txt")
{
open (my $DIRECTIONS, '<', 'data.txt') or die "open failed: $!";
chomp( my @directions = <$DIRECTIONS> );
close ($DIRECTIONS);
my ($file) = @directions;
my $to = $abc . $file;
my $copy = $old . "por$file";
copy ($to, $copy) or die "copy failed: $!";
move ($to, $old) or die "move failed: $!";
unlink('data.txt');
}



Laurent_R
Veteran / Moderator

Nov 26, 2012, 2:27 PM

Post #2 of 14 (2978 views)
Re: [stevieger] accessing files with script [In reply to] Can't Post

In ABC is your root directory (as it looks from your explanations since you mention twice /ABC/...) then simply declare your files as follows:


Code
my $abc = "/ABC/";  
my $old = "/ABC/old/";


If it is not the root directory, you could still do this to get one level up:

Code
my $abc = "../ABC/";  
my $old = "../ABC/old/";


Or you can give the full path.

Or, better, use a Perl module such as File::Path to deal more efficiently with directories.


rovf
Veteran

Nov 27, 2012, 12:37 AM

Post #3 of 14 (2971 views)
Re: [stevieger] accessing files with script [In reply to] Can't Post


Quote
However my perl script is located in the directory


For the effect of this program, it doesn't matter where you program is located. Since you are using relative pathnames, it matters however where your working directory is when you run the program.


Laurent_R
Veteran / Moderator

Nov 27, 2012, 4:10 AM

Post #4 of 14 (2958 views)
Re: [rovf] accessing files with script [In reply to] Can't Post

Yes, good point, what matters is not where the program is located, but the directory from which you launch it.


stevieger
Novice

Nov 27, 2012, 6:12 AM

Post #5 of 14 (2954 views)
Re: [Laurent_R] accessing files with script [In reply to] Can't Post

I am launching it from the directory it is in, not sure how I would do otherwise..

this is all being done on a unix solaris box.


(This post was edited by stevieger on Nov 27, 2012, 6:13 AM)


rovf
Veteran

Nov 27, 2012, 6:19 AM

Post #6 of 14 (2951 views)
Re: [stevieger] accessing files with script [In reply to] Can't Post

How about using the absolute path for the script?

Or putting the script directory in your PATH?

Although possible, it is very uncommon to require that the user does a cd into the script directory, just to execute the script. Usually the cd is done (if it is necessary at all) to the directory which contains the data to be processed.


stevieger
Novice

Nov 27, 2012, 7:31 AM

Post #7 of 14 (2949 views)
Re: [rovf] accessing files with script [In reply to] Can't Post

I don't want to have the script in the PATH, I need it in its own directory.

I thought about using cd within the script to access the data but am unsure how to do that.

Other newbie question from what I've seen and practiced; it seems logical to think everything in a PERL script will be in reference to the directory it is contained in, is this correct?


stevieger
Novice

Nov 27, 2012, 7:37 AM

Post #8 of 14 (2946 views)
Re: [Laurent_R] accessing files with script [In reply to] Can't Post

I'm actually trying to go one level down as that is where the data is that I need to access however I'm unknowing of how to do that.


rovf
Veteran

Nov 27, 2012, 8:04 AM

Post #9 of 14 (2942 views)
Re: [stevieger] accessing files with script [In reply to] Can't Post


Quote
I don't want to have the script in the PATH, I need it in its own directory.


I didn't suggest to move your script to a different location. I proposed to extend your PATH - or do use a full path to access your script.


Quote
I thought about using cd within the script to access the data but am unsure how to do that.


You could do this (see perldoc Cwd), but it's not a good idea: It is - at least in your example - pointless to use *relative* pathes in your scripts AND then chdir to the appropriate location, only to make the relpathes work. In this case, you could have accessed your data by abspath in the first place.

Why don't you like the idea to write out the path when accessing your script?


stevieger
Novice

Nov 27, 2012, 8:43 AM

Post #10 of 14 (2940 views)
Re: [rovf] accessing files with script [In reply to] Can't Post

I've resolved the issue (via everyones help on here) by just referencing the absolute path/directory.

I misunderstood what you meant, I thought you were saying to move the script to a different directory rather than calling it from a different directory by writing out the location of it. That would work fine I assume however I've resolved my issue and my script is now functioning properly.

Thanks everyone for the help.


Laurent_R
Veteran / Moderator

Nov 27, 2012, 10:15 AM

Post #11 of 14 (2938 views)
Re: [stevieger] accessing files with script [In reply to] Can't Post


In Reply To
I am launching it from the directory it is in, not sure how I would do otherwise..


This is very simple Unix practice.

If your data is in /ABC/ and /ABC/old and your script in /ABC/perlscript, just cd into /ABC and launch the script as:


Code
./perscript/my_script.pl



rovf
Veteran

Nov 27, 2012, 1:43 PM

Post #12 of 14 (2933 views)
Re: [Laurent_R] accessing files with script [In reply to] Can't Post

... and of course you can drop the leading './' .....


Laurent_R
Veteran / Moderator

Nov 27, 2012, 2:57 PM

Post #13 of 14 (2931 views)
Re: [rovf] accessing files with script [In reply to] Can't Post

Yes, but as a matter of reliability and security, even when a script is in my current directory, I prefer to type: "./my_script.pl", rather than simply "my_script.pl", just to make sure that I know for sure which version is being used (rather than relying on the PATH, which may sometimes create problems). This is not paranoia, but the voice of experience: I have been bitten too often by the error of not executing the program version I thought I was using.


rovf
Veteran

Nov 28, 2012, 12:51 AM

Post #14 of 14 (2912 views)
Re: [Laurent_R] accessing files with script [In reply to] Can't Post

In the case ./my_script.pl you are right, of course (though I, being lazy, and lucky enough not being bitten yet, still prefer less typing), but in the case of the example you gave (./some_directory/my_script.pl), PATH searching isn't done anyway. But I agree: If you always prepend your relpathes by ./, you don't have to think about this difference, and you are always on the safe side.

With respect to safety, I'm a bad guy anyway, because I have "." in my PATH.... ;-)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives