Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

system pwd

by vbrtrmn (Pilgrim)
on May 21, 2003 at 23:11 UTC ( [id://259930]=perlquestion: print w/replies, xml ) Need Help??

vbrtrmn has asked for the wisdom of the Perl Monks concerning the following question:

I'm executing the following code, I'm not sure why it returns the $VAR1, can someone clear it up for me?

Simplified Code
use Data::Dumper; my $dir = system('pwd'); print Dumper($dir);
Output:
/web1/users/u2369/cgi-bin $VAR1 = 0;

--
paul

Replies are listed 'Best First'.
Re: system pwd
by jkahn (Friar) on May 22, 2003 at 00:08 UTC
    The other comments (regarding the difference between system and backticks) are entirely correct.

    I have a separate comment, however -- if you want to identify the current working directory, you should use Perl's core module Cwd instead of invoking system tools. (try perldoc Cwd).

    use Cwd; print getcwd();

    The biggest advantage is that using Cwd does not have portability issues -- your tool will continue to work correctly on whatever OS you need to use Perl on.

    If you already know this, great. Just my $0.02 US.

Re: system pwd
by Limbic~Region (Chancellor) on May 21, 2003 at 23:21 UTC
    vbrtrmn,
    I am not sure why it is showing $var1 in the dump as I am not a Data::Dumper expert, but you are setting the variable equal to the return code of the system command - and hence it is showing 0.

    The reason you are getting the pwd is because pwd prints to standard out.

    If you want to capture the output of the pwd in the variable use my $dir = `pwd`;

    Cheers - L~R

      And you can use the Dump method so the dump shows the actual variable name instead of $VAR1:

      #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Cwd; my $dir = getcwd(); print Data::Dumper->Dump([$dir],[qw(*dir)]),"\n";

      Update: as well as what jkahn said, of course =]


      cp
      ----
      "Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."
Re: system pwd
by arthas (Hermit) on May 21, 2003 at 23:24 UTC
    Don't use system() if you need the output (it grabs the return code!) to be captured in a variable. Use the backticks instead:
    my $dir = `pwd`;
    Also, but you probably know, you don't need Dumper to print a scalar. ;-)

    Michele.
Re: system pwd
by graff (Chancellor) on May 22, 2003 at 05:22 UTC
    I was about to point out that you are likely to have an environment variable on hand for this -- e.g. with a bash shell, I can use "$ENV{PWD}" -- but there are some problems with this:
    • The name of the %ENV element may vary depending on what shell you use.
    • Perl's "chdir" function does NOT update %ENV.

    It turns out that jkahn's advice to "use Cwd;" is absolutely right -- and for my own sake, I'm glad to have learned how right it is:

    cd /home/graff perl -e 'use Cwd; $,=" "; print $ENV{PWD},getcwd,$/; chdir "/tmp"; print $ENV{PWD},getcwd,$/;'
    As the output shown below makes clear, Cwd::getcwd always returns the true current directory:
    /home/graff /home/graff /home/graff /tmp

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://259930]
Approved by Enlil
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-24 02:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found