alternative to cwd()

Discussion in 'Perl Misc' started by Sunil, Jun 22, 2004.

  1. Sunil

    Sunil Guest

    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.
     
    Sunil, Jun 22, 2004
    #1
    1. Advertising

  2. Sunil wrote:

    > 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

    --
    -Gk
     
    George Kinley, Jun 22, 2004
    #2
    1. Advertising

  3. Sunil

    Tintin Guest

    "Sunil" <> wrote in message
    news:CaPBc.55$...
    > 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?
     
    Tintin, Jun 22, 2004
    #3
  4. Tintin wrote:
    > "Sunil" <> wrote in message
    > news:CaPBc.55$...
    >
    >>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?


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

    The wording of the OP suggests this.

    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Moellers, Jun 22, 2004
    #4
  5. Sunil

    Tore Aursand Guest

    On Tue, 22 Jun 2004 15:17:36 +0200, Josef Moellers wrote:
    >> 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.


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


    --
    Tore Aursand <>
    "The science of today is the technology of tomorrow." (Edward Teller)
     
    Tore Aursand, Jun 22, 2004
    #5
  6. Tore Aursand wrote:
    > On Tue, 22 Jun 2004 15:17:36 +0200, Josef Moellers wrote:
    >
    >>>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.

    >
    >
    > 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.

    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Moellers, Jun 22, 2004
    #6
  7. Sunil

    Ben Morrow Guest

    Quoth :
    > "Sunil" <> wrote:
    > > 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
    > > (``).

    >
    > Ask Cwd how it does it.
    >
    > vi `perldoc -l Cwd`


    or simply perldoc -m Cwd.

    Ben


    --
    Although few may originate a policy, we are all able to judge it.
    - Pericles of Athens, c.430 B.C.
     
    Ben Morrow, Jun 23, 2004
    #7
  8. Sunil wrote:

    > 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";
    -----------------------------------------------------
    time ./test2.pl
    /home/nekret/public_html
    0.04user 0.00system 0:00.05elapsed 70%CPU (0avgtext+0avgdata 0maxresident)k
    0inputs+0outputs (535major+222minor)pagefaults 0swaps
     
    Nicholas Erkert, Jun 23, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Burganovsky
    Replies:
    1
    Views:
    455
    Mark Rae
    Dec 15, 2005
  2. Burganovsy
    Replies:
    0
    Views:
    451
    Burganovsy
    Dec 11, 2005
  3. Tim Black
    Replies:
    2
    Views:
    447
    Steve Christensen
    Sep 9, 2004
  4. Kamilche

    More elegant way to cwd?

    Kamilche, Dec 24, 2004, in forum: Python
    Replies:
    11
    Views:
    503
    Peter Hansen
    Dec 29, 2004
  5. Sunil

    alternative to "cwd()"

    Sunil, Apr 16, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    156
    Joe Smith
    Apr 21, 2004
Loading...

Share This Page