Home: Perl Programming Help: Intermediate:
How to check, if the script on current directory



digioleg54
User

Mar 29, 2017, 12:03 PM


Views: 3854
How to check, if the script on current directory

Perl script is working on different directories and at the end I want to clean working directory. I have a variable $path_to_cur_dir = /path/to/dir
What command to use to check if the script is on current directory, and if not, then change directory to current.

In my opinion it is supposed to be something like
if ( ! -z $path_to_cur_dir)
{
chdir $path_to_cur_dir
}


(This post was edited by digioleg54 on Mar 29, 2017, 12:10 PM)


FishMonger
Veteran / Moderator

Mar 29, 2017, 1:31 PM


Views: 3846
Re: [digioleg54] How to check, if the script on current directory

http://search.cpan.org/~rjbs/PathTools-3.62/Cwd.pm


Laurent_R
Veteran / Moderator

Mar 30, 2017, 5:37 AM


Views: 3839
Re: [digioleg54] How to check, if the script on current directory

If you really want to know whether you're in the right directory, then use the module suggested by FishMonger, but do you really need to check? Why don't you simply issue the chdir command?


digioleg54
User

Mar 30, 2017, 6:00 AM


Views: 3838
Re: [Laurent_R] How to check, if the script on current directory

I already found correct solutuion

if ( defined $dir_to_check and length $dir_to_check)
{
chdir $$dir_to_check
}