Setting a Perl variable as an environment variable - perl

I'm trying to set a variable I previously set in a Perl script as an environment variable, but it seems to not realize the parameter I'm passing in is a variable and not the actual path I want.
For example, when I run this:
$ENV{'ENV_VARIABLE'}='\'$file_path\'';
print($ENV{'ENV_VARIABLE'});
I only get:
'$file_path'
Is there any way I can tell it that what I'm passing in is actually a variable and not a literal string?

In Perl, single quoted strings do not interpolate variables. You want to use a double quote:
$ENV{'ENV_VARIABLE'}= "'$file_path'";
In that line, the rvalue is interpreted as q{'} . $file_path . q{'} where q{'} is a fancy way of writing '\'', which is a bit harder to read.

Related

PowerShell - Merge two variables into one

I'm learning PowerShell so please forgive (what I'm sure is) a simple question.
I'm used to coding BATCH scripts and if I wanted to merge %USERDOMAIN% and %USERNAME% I would:
set zFullUsername=%USERDOMAIN%\%USERNAME%
echo %zFullUsername%
How can I do the same in PowerShell?
Thank you for your time.
On a supported Operating System, I wouldn't even bother with environment variables for this:
$zFullUsername = whoami
Then just access it as required:
$zFullUsername
In PowerShell, you can access environment variables in a few different ways. The way I recommend is to use the $env:VAR variable to access them.
$user = $env:USERNAME
$domain = $env:USERDOMAIN
echo "$domain\$user"
Note: \ is not an escape character in the PowerShell parser, ` is.
Similarly to rendering the echo command (echo is an alias of Write-Output btw) you can create a username variable like so:
$fullUserName = "$domain\$user"
Or you can skip right to creating $fullUserName straight from the environment variables:
$fullUserName = "${env:USERDOMAIN}\${env:USERNAME}"
Note: When variables have non-alphanumeric characters in them, the ${} sequence tells PowerShell everything between the ${} is part of the variable name to expand.
It seems the : in $env:VAR is actually an exception to this rule, as"Username: $env:USERNAME" does render correctly. So the ${} sequence above is optional.
To avoid confusion when trying to apply this answer in other areas, if you needed to insert the value of an object property or some other expression within a string itself, you would use a sub-expression within the string instead, which is the $() sequence:
$someVar = "Name: $($someObject.Name)"
When using either ${} or $(), whitespace is not allowed to pad the outer {} or ().

Do curly braces mean anything in PowerShell variable declaration?

I have come across variables (or parameters) being declared like this:
${var_name} = "Hello world!"
As far as I can tell, this is no different to the following:
$var_name = "Hello world!"
I am wondering if the {} braces in the first example do or mean anything. Do they change the behaviour of the variable?
Curly braces in PowerShell variable names allow for arbitrary characters in the name of the variable. If there are no "pathological" characters in the variable name, then the braces are not needed and have no effect.
You'll find that "generated" code will sometimes use curly braces because they guarantee that the variable name is valid.
the {} braces use for declare variables with spaces in the middle or inside of the variable, like this:
${var_name hello } = "Hello world!2"
$var_name = "Hello world!"
it's not the same, 'cause you can't save data in a variable with spaces, powershell understand the variable until a space, except it's inside the braces.
Have a good day. (:
What might not be obvious on a first glance is that you can use any provider within ${}, for example:
${c:\tmp\foo.txt}="Hello World" # C: uses file system provider
The effect depends on the provider. For the file system provider the example changes the content of the specified file.

Ignore space in string stored in argument perl

I'm trying to execute a .VB script along with some other arguments.
I'm calling system("$cmd"), where
my $cmd = "Report.exe $app_env $rpt_Dir $eff_date";
and
my $rpt_Dir = "\\\\server\\folder\\Target Report\\test";
The problem I am having is that (I think) the space between "Target Report" is making the script treating it as 2 arguments.
And the reason why I didn't just surround the path with "" and pass it as an argument instead of saving it to a variable is that the path changes based on the date.
And it's not a first choice that I change the code in the .VB script.
On Windows, the system call uses cmd.exe to process commands, so you need double quotes around any parameters that contain spaces
Set your $cmd up like this
my $cmd = qq{Report.exe $app_env "$rpt_Dir" $eff_date};
The qq{...} construction is identical to ordinary double quotes, but it allows you to choose your own delimiters so that you don't have to escape any embedded double quotes
Then you can write
system($cmd);
It is wrong to put quotes around a solitary scalar variable
You can pass an array of args to system. If you do this, you won't be tripped over by quote interpolation.
system ( "Report.exe", $app_env, $rpt_Dir, $eff_date );
You can simply enclose the $rpt_Dir variable in single quotes:
my $cmd = "Report.exe $app_env '$rpt_Dir' $eff_date";
and it will be treated as a single arguement

Passing a variable to a command in a script

I've been searching all over the place and since I'm taking my first steps in PERL this might be one of he dumbest questions but here it goes.
So I'm creating a script to manage my windows and later bind it to keyboard shortcuts, so I I'm trying to run a command and passing some variables:
my $command = `wmctrl -r :ACTIVE: -e 0,0,0,$monitors->{1}->{'width'}/2,$monitors->{1}->{'height'}`;
But I get an error saying I'm not passing the right parameters to the command, but if I do this, everything works great:
my $test = $monitors->{1}->{'width'}/2;
my $command = `wmctrl -r :ACTIVE: -e 0,0,0,$test,$monitors->{1}->{'height'}`;
So do I really have to do this? assign it first to a variable and then pass it, or there's a more elegant way of doing it?
The backticks operator (or the qx{}) accepts A string which is (possibly) interpolated. So accepts string and not expression like $var/2.
Thats mean than the $variables ($var->{1}->{some} too) are expanded but not the arithmetic expressions.
Therefore your 2 step variant works, but not the first.
If you want evaluate an expression inside the string you can use the next:
my $ans=42;
print "The #{[ $ans/2 ]} is only the half of answer\n";
prints
The 21 is only the half of answer
but it is not very readable, so better and elegant is what you're already doing - calculate the command argument in andvace, and to the qx{} or backticks only pass the calculated $variables.

Tcl set command

Quick question... I'm trying the following:
set changedir "cd $JSDIR/"
eval $changedir
..Where $JSDIR is defined already. Running this script gives the error: "set: Variable name must begin with a letter."
What's the fix?
Thanks.
That's not an error message generated by Tcl, because Tcl's variables most definitely do not need to begin with a letter. OK, it tends to be convenient to keep them alphanumeric because the $var shorthand syntax is more restrictive than the general space of variable names, but the set var form can handle virtually anything.
I'm guessing that script is being evaluated by something that isn't Tcl, perhaps bash?
Try to set changedir cd ${JSDIR/} instead
This message can appear when in fact the variable name is entirely correct, and the real problem is that the variable value needs to be quoted. Try instead of: set changedir "cd $JSDIR//"