alternative to cwd()

S

Sunil

Hi All,
I need to find the current working directory, Is there any alternative
to
use Cwd;
$pwd = cwd();

I am looking for a solution other than using system() or backtics
(``).

Thanks,
Sunil.
 
G

George Kinley

Sunil said:
Hi All,
I need to find the current working directory, Is there any
alternative to
use Cwd;
$pwd = cwd();

I am looking for a solution other than using system() or
backtics (``).

Thanks,
Sunil.


On Windows
$pwd=`cd`

BTW what wrong with cwd
 
T

Tintin

Sunil said:
Hi All,
I need to find the current working directory, Is there any alternative
to
use Cwd;
$pwd = cwd();

I am looking for a solution other than using system() or backtics

For solution see above.

Seriously, why not cwd?
 
J

Josef Moellers

Tintin said:
For solution see above.

Seriously, why not cwd?

Probably
"Assignment 3: "Find the current working directory. Do NOT
use Cwd; $pwd = cwd();
"

The wording of the OP suggests this.
 
T

Tore Aursand

Probably
"Assignment 3: "Find the current working directory. Do NOT
use Cwd; $pwd = cwd();
"

The wording of the OP suggests this.

Still: Why not use cwd? It comes with the standard Perl distribution,
and it's portable.
 
J

Josef Moellers

Tore said:
Still: Why not use cwd? It comes with the standard Perl distribution,
and it's portable.

Because his homework assignment strictly forbids to do that.

Besides: It is sometimes extremely instructive NOT to use some canned
function. But for the sake of portability, I'd use Cwd, too.
 
N

Nicholas Erkert

Sunil said:
Hi All,
I need to find the current working directory, Is there any alternative
to
use Cwd;
$pwd = cwd();

I am looking for a solution other than using system() or backtics
(``).

Thanks,
Sunil.
One way you could do it would be using the syscall function.
Unfortunately doing so seems to take about twice the time to run than
just using cwd.
-----------------Using syscall-----------------------
#!/usr/bin/perl
require 'syscall.ph';

#prepare the gunk string for up to 2048 chars
$gunk = pack('A2048', ' ');

#call getcwd, not sure if it would be off by one if I used 2048
#here so just to be safe I used 2047.
syscall(&SYS_getcwd, $gunk, 2047);

#trim off trailing whitespace
$gunk =~ s/\s+$//;

print "$gunk\n";
-----------------------------------------------------
time ./test.pl
/home/nekret/public_html
0.07user 0.00system 0:00.08elapsed 84%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (385major+206minor)pagefaults 0swaps


---------------Using cwd-----------------------------
#!/usr/bin/perl
use Cwd;

print cwd() . "\n";
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top