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

Hello , can you tell me how to do this in perl
> $env = 'whoami'
I tried somthing like
my $env = system( qq ( whoami )) ;
but don't seem to work.

Replies are listed 'Best First'.
Re: shell to perl
by Aristotle (Chancellor) on Jan 23, 2003 at 22:56 UTC
    What is it that you're trying to do? Assign the string whoami to $env? That's my $env = 'whoami'; - or are those supposed to be backticks? In that case system is not what you want as it just returns the process' return code. If you want to capture the output, you have to use backticks, just like in shell: my $env = `whoami`; You can also use the qx notation: my $env = qx[whoami]; Both do the same, the latter just lets you pick different delimiters when that's convenient.

    Makeshifts last the longest.

      thanks
Re: shell to perl
by Rich36 (Chaplain) on Jan 23, 2003 at 23:04 UTC

    The code below will give you the same output as "whoami" without having to make a system call.

    my $id = (getpwuid($<))[0]; print qq($id\n);

    «Rich36»
Re: shell to perl
by rbc (Curate) on Jan 23, 2003 at 22:41 UTC
    Did you do ...
    $ perl -e 'my $env = system( qq ( whoami )) ;'
    ... that worked for me?
      ... that worked for me?

      Uhm... No, it didn't. In your case, $env was set to the exit status of whoami(1), probably 0.

      -sauoq
      "My two cents aren't worth a dime.";
      
        Uhm... No, it didn't. In your case, $env was set to the exit status of whoami(1), probably 0.
        Yeah so ... does that mean it "didn't work"?
        *Geez*
        $ perl -e 'my $env = system( qq ( whoami )) ;' rbc $ perl -e 'my $env = system( qq ( whoru )) ;' wrong