alternative to "cwd()"

S

Sunil

All,
I have code like
use Cwd;
my $currDir = cwd();
and I get an error
Can't locate Cwd.pm in @INC (@INC contains:
................................... .......... .........)

I cannot change @INC by setting the value of PERL5LIB. or do a "use
lib ..."

Is there any other very basic way of getting the current working
directory, which will work always.



Thanks,
Sunil.
 
A

Anno Siegel

Sunil said:
All,
I have code like
use Cwd;
my $currDir = cwd();
and I get an error
Can't locate Cwd.pm in @INC (@INC contains:
.................................. .......... .........)

I cannot change @INC by setting the value of PERL5LIB. or do a "use
lib ..."

What's stopping you?
Is there any other very basic way of getting the current working
directory, which will work always.

"use lib ..." works always, as far as anything works "always". Your
question is like saying, "I want to turn the light on, but I can't use
the switch. Is there a method that works always?"

You can push things on @INC directly, but that's harder to do (in time)
than "use lib".

Anno
 
B

Brian McGonigle

Sunil said:
All,
I have code like
use Cwd;
my $currDir = cwd();
and I get an error
Can't locate Cwd.pm in @INC (@INC contains:
.................................. .......... .........)

I cannot change @INC by setting the value of PERL5LIB. or do a "use
lib ..."

Is there any other very basic way of getting the current working
directory, which will work always.



Thanks,
Sunil.

"$currDir = $ENV{PWD}" works on unix/linux and maybe MacOS X, I heard it
was unix-based.

"$currDir = `chdir`" will print the cwd on Win32.
 
J

Jakob Lell

All,
I have code like
use Cwd;
my $currDir = cwd();
and I get an error
Can't locate Cwd.pm in @INC (@INC contains:
.................................. .......... .........)

I cannot change @INC by setting the value of PERL5LIB. or do a "use
lib ..."

Is there any other very basic way of getting the current working
directory, which will work always.

Hello,
the POSIX-module provides the function getcwd:

use POSIX qw(getcwd);
my $currdir=getcwd();

Regards
Jakob
 
J

Joe Smith

Brian said:
"$currDir = $ENV{PWD}" works on unix/linux and maybe MacOS X, I heard it
was unix-based.

The value in $ENV{PWD} may or may not be accurate, depending on
what shell was being used at the time the perl program was started.
It is very often wrong when invoked via cron or as a CGI.
It is definitely wrong if a chdir() is earlier in the script.
-Joe
 
J

Joe Smith

Sunil said:
I cannot change @INC by setting the value of PERL5LIB. or do a "use
lib ..."

Horsepucky. You can always change @INC.

Perhaps you meant "Changing $ENV{PERL5LIB} appears to have no
affect on @INC", which is true if you forget to use a BEGIN block.

Have you tried this:
BEGIN {
$ENV{PERL5LIB} = "/primary/perl/lib";
push @INC,"/primary/perl/lib/subdir";
}
use Cwd;

-Joe
 
W

Walter Roberson

: BEGIN {
: $ENV{PERL5LIB} = "/primary/perl/lib";
: push @INC,"/primary/perl/lib/subdir";
: }
: use Cwd;

Any particular reason you used double-quotes instead of single-quotes?
 
J

Joe Smith

Walter said:
: BEGIN {
: $ENV{PERL5LIB} = "/primary/perl/lib";
: push @INC,"/primary/perl/lib/subdir";
: }
: use Cwd;

Any particular reason you used double-quotes instead of single-quotes?

1) Commodore Amiga. From the CLI, file names with embedded blanks needed
double quotes.
2) I don't buy into the religious argument of single quotes being better
than double quotes for constants. I understand the counter arguments,
I just don't find them convincing.
3) Old habit I don't think is worth the effort to break.
-Joe
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top