Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Perl and your environment

by P0w3rK!d (Pilgrim)
on Jul 24, 2001 at 21:35 UTC ( [id://99419]=perlquestion: print w/replies, xml ) Need Help??

P0w3rK!d has asked for the wisdom of the Perl Monks concerning the following question:

I have 2 variables as follows:
<CODE> $TESTVAL = "~" & `echo ${USER}` & "/foo";
How do I get $TESTVAL = ~chris/foo ?
I've tried eval, etc. w/o luck
Thanks... :)

Replies are listed 'Best First'.
Re: Perl and your environment
by myocom (Deacon) on Jul 24, 2001 at 21:40 UTC

    & is not the concatenation operator in Perl. What you're looking for is the dot. For this example, there's no need to use it, though. Also, there's no reason to use backticks to get environment variables; they're stored in the %ENV hash.

    my $testval = "~$ENV{USER}/foo";
Re: Perl and your environment
by dsb (Chaplain) on Jul 24, 2001 at 21:46 UTC
    There are a couple problems with what you have.
    1. I'm pretty sure you mean to be using a concatentation operator, which in Perl is .. The & you are using is a bitwise AND operator.
    2. Using `` causes variable interpolation, so your script is trying to interpolate a variable called $USER. Using the {} has no bearing on variable interpolation the way you are using it. You have to escape the $ with a \
    3. The return value you will get from the `` will have a newline attached to it. You'll need to chomp that.

    After all that you should come with something like this:

    chomp($user = `echo \$USER`); $TESTVAL = "~" . $user . "/foo";

    Of course, the methods the other monks posted are probably a better idea than this. I'm trying to show you how to properly use the operators you chose.

    Hope that helps.

    Amel - f.k.a. - kel

      Sorry...
      I am trying to write ASP, Java, Perl, and SH all at the same time for 5 different projects.
      My brain is a little fried since I haven't used Perl for a while.
Re: Perl and your environment
by lshatzer (Friar) on Jul 24, 2001 at 21:37 UTC
    I would try $ENV{USER} instead of using backticks to echo.

    Updated: concatentation operator is a . not a & (As pointed out below by myocom and Amel.)
Re: Perl and your environment
by suaveant (Parson) on Jul 24, 2001 at 21:47 UTC
    What they all said is very true... what they didn't tell you... & is the bitwise and operator... so things like 1&2 yields 0 1&3 yields 1 3&7 yeilds 3 do to the bitwise operations... which means you'd get some real weird values in $TESTVAL

    Update Okay, Amel, told you while I was typing this :)

                    - Ant

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-19 08:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found