chdir ($variable)

M

maheshpop1

Hi Guys,

the following piece of code doesnt seem to work

my $folder= $hash{working_directory};
chdir("$folder") or die "can't change dir $folder" ;

I printed out the output and checked if the the variable $folder was
being loaded and it was being loaded correctly.

while this works if I put in the absolute path.

chdir("/test/rep") or die "can't change dir /test/rep" ;

hmmmm any thoughts.

cheers,
pop.
 
D

David Squire

Hi Guys,

the following piece of code doesnt seem to work

my $folder= $hash{working_directory};
chdir("$folder")

Why are you quoting that variable?
or die "can't change dir $folder" ;

Perl is your friend. Let it help by telling what went wrong:

or die "can't change dir $folder: $!";
I printed out the output and checked if the the variable $folder was
being loaded and it was being loaded correctly.

while this works if I put in the absolute path.

chdir("/test/rep") or die "can't change dir /test/rep" ;

hmmmm any thoughts.

hmmmm. Are you sure that the working directory of your script is what
you think it is? Quick and dirty check: print `pwd`;

DS
 
M

maheshpop1

oh yeah forgot to add it throws an error
"cant change dir /test/rep
No such file or directory at custom.pl at like 156"

any ideas,

cheers,
pop
 
M

maheshpop1

Hi David,

Still learning, I put in the $! and it gave me this error

"can't change dir /test/rep/
No such file or directory at custom.pl line 156."

The working dir of the script is /users/XXX/perlscripts/ and I am
trying to access a folder at the home level /test/rep to run a command
in that folder.

pop
 
D

David Squire

oh yeah forgot to add it throws an error
"cant change dir /test/rep

Didn't you say in the first post that it *works* if you use the absolute
path? Which is true?
No such file or directory at custom.pl at like 156"

"like"? Please cut and paste real error messages.
any ideas,

Yes. As suggested in my previous post, check what directory your script
is actually running in.

Also, please read and follow the posting guidelines if you hope that
people will continue helping you. Start by quoting context when you reply.

DS
 
S

Sumo Wrestler (or just ate too much)

Hi Guys,

the following piece of code doesnt seem to work

my $folder= $hash{working_directory};
chdir("$folder") or die "can't change dir $folder" ;

I printed out the output and checked if the the variable $folder was
being loaded and it was being loaded correctly.
[...]

Perhaps $folder has a trailing \n. Chomp it to be sure.

BTW, you don't have to put variables in double quotes like that. Just
use "chdir($folder)."
 
T

Tad McClellan

Sumo Wrestler (or just ate too much) said:
(e-mail address removed) wrote:
Perhaps $folder has a trailing \n. Chomp it to be sure.


If you adopt a helpful style of diag message, then such cases
become obvious when they happen:

chdir $folder or die "can't change dir '$folder' $!";
 
P

Paul Lalli

Ferry said:
pop:


Quotes not required in chdir.

That is a misstatement of the truth. Quotes are dependent on what
they're quoting, not where they're being used. Double quotes are not
required around a single scalar variable. Quotes of some kind are
definately required around an otherwise bare word, if strict is
enabled. Quotes of some kind are always required around a string that
consists of non-word characters.

chdir($folder); #good
chdir("$folder"); #okay, but pointless (see: perldoc -q quoting)
chidr('$folder'); #probably wrong, unless you have a folder actually
named '$folder'

chdir(tmp); #bad, breaks if strict is enabled.
chdir("tmp"); #good
chdir('tmp'); #better

chdir(/tmp/foo); #wrong, breaks regardless of strict
chdir("/tmp/foo"); #good
chdir('/tmp/foo'); #better

Paul Lalli
 
G

Glenn Jackman

At 2006-05-17 06:48AM said:
Hi David,

Still learning, I put in the $! and it gave me this error

"can't change dir /test/rep/
No such file or directory at custom.pl line 156."

The working dir of the script is /users/XXX/perlscripts/ and I am
trying to access a folder at the home level /test/rep to run a command
in that folder.

pop

Are you trying to chdir to /users/XXX/perlscripts/test/rep or /test/rep ?
If the former, drop the leading slash.
 
M

maheshpop1

Tad said:
s/consists of/contains/;
Hi Guys

Found the problem. t indeed is a new line character at the end of the
value which is causing it. Somehow chomp didnt work either here.

I was reading from a text file a line and splitting it into different
key, value pairs in a hash map. Take this line in the text file
1:query_action,checkin:working_directory,/temp/rep

If you observe there is a :)) seperating the one key,value from
another. The last one is the working_directory and there is no :))
after its value. So when I read it in
my $folder= $hash{working_directory};
.. It loaded itself as
/temp/rep with an additional \n (newline) character. So the chdir did
not work. I changed the text file to show as

1:query_action,checkin:working_directory,/temp/rep: <-------- (added
another :)) at the end of the string).

Now it loads okay. somehow chomp didnt work either.

thanks for the assist guys (Still learning programming :)))) so kindly
bear with any newbie questions from my side.
pop.
 
B

Ben Morrow

Quoth "Ferry Bolhar said:
Paul Lalli:

Almost always. Not in regular expressions.

ObPedant: The // (or whatever) around a regex *are* quote characters :).

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top