Gossamer Forum
Home : General : Perl Programming :

Changing Directories in a Subroutine

Quote Reply
Changing Directories in a Subroutine
Quick question...

If I'm in directory C:\stuff\bob and I change to a subdirectory in a subroutine

sub ralph {
change to C:\stuff\bob\ralph
do perl code
}

Do I need to change "back" to the correct directory (C:\stuff\bob) ?

TIA - Mike.
Quote Reply
Re: [Watts] Changing Directories in a Subroutine In reply to
I have Xitami (C:\Xitami\cgi-bin\audit.pl) installed on my local machine and run a script called audit.pl. I run it from a web browser on any networked machine by using "http://myMachineName/cgi-bin/audit.pl"

I open and read a file on the network by using the following:

chdir('R:\SharedDocs\Loan') or die "cannot connect to R:\SharedDocs\Loan\n";

this sets the directory locatation to network share and I can then open a particular file in the "Loan" directory read what I need out of it.

Example:
open(AUDIT, "<$auditFileIs");

&subroutine;

close(AUDIT);

My question is - during the subroutine can I change directories? I know I can open and close other files (while the first file is still open) because I've done it.

What I need to do now is:

chdir('C:\Xitami\cgi-bin\pud')

but it keeps "dying" on me.

I've tried chdir('myMachineName://Xitami/cgi-bin/pud') and other variations.

Any ideas?
Quote Reply
Re: [Watts] Changing Directories in a Subroutine In reply to
Quote:
What I need to do now is:

chdir('C:\Xitami\cgi-bin\pud')

but it keeps "dying" on me.

Figured it out
(There was an error in the file name - single vs. double quotes) in the open(AUDIT, "<$auditFileIs");

I had single quotes by mistake.
Quote Reply
Re: [Watts] Changing Directories in a Subroutine In reply to
Yeah, windows is a real PITA with that kinda stuff :(

If you just want to use single forward slashes, and then convert them automatically with a regex, you could try;

Code:
my $file = 'D:/something/somefile.html';
$file =~ s|/|\\\|sig;

I use this quite a bit, especially when grabbing file locations from different routines, as you can do it easily..i.e;

Code:
my $file = get_file_locations($ID);
$file =~ s|/|\\\|sig;

Hope that helps.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Watts] Changing Directories in a Subroutine In reply to
Hi,

canīt you just use

Code:

cd name


and

Code:

cd ..


I did not use commands on a windows server yet but sure you use system(''); or whatever you have to on windows.

Regards

n || i || k || o
Quote Reply
Re: [el noe] Changing Directories in a Subroutine In reply to
Hi,

chdir() and `cd` are quite different =)

chdir() saves the changes in the perl script, allowing you to run other system() commands; whilst the `` just executes the command, but doesn't "more" the user (thus it wouldn't be much help, unless running from SSH/Telnet/DOS prompt) Angelic

Good ol' M$ again :P

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Changing Directories in a Subroutine In reply to
Oh, started a new day and learned something new.

Thanks
Quote Reply
Re: [el noe] Changing Directories in a Subroutine In reply to
In Reply To:
Oh, started a new day and learned something new.

<G> I only know about that, as I've had a lot of trouble with M$ Windows servers (load of rubbish IMO), but for some of the stuff we need to do, its something I have to work with Frown

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!