The issue
I found out how to use windows environment variables (e.g., %AppData%, %HomePath%, %SystemRoot%, etc.) in this SO post:
Getting the path of %AppData% in perl script
Here is the code snippet that was chosen as a working, correct answer:
#!/usr/bin/perl
use warnings;
use strict;
my $localConfPath = $ENV{localappdata};
my $appdata = $ENV{appdata};
print $localConfPath; #will print the app path - C:\users\xxx\AppData\local
print $appdata; #prints - C:\users\xxx\AppData\Roaming
However, this is not working on my machine in my code for some reason. My scripts work without the shebang (#!) line so I tried the script both with and without it, to no avail.
My set-up
I'm using the Perl that comes with GitBash, if that makes a difference.
What I've tried
I tried a simple Perl command line execution:
perl -e 'print %ENV{AppData}';
This didn't work. I also tried the following alternatives:
perl -e 'print %ENV{APPDATA}';
perl -e 'print %ENV{appdata}';
That also didn't work. Here's the error I get (the same for all 3 versions):
syntax error at -e line 1, near "%ENV{AppData"
Execution of -e aborted due to compilation errors.
I even tried to use the code from the SO post I mentioned in it's own file. That code doesn't work either. With the code from the post I get this error:
$ perl /c/Users/User1/Desktop/ehCode_testingWindowsEnvironmentVariables_01.pl
Use of uninitialized value in print at /c/Users/User1/Desktop/ehCode_testingWindowsEnvironmentVariables_01.pl line 7.
Use of uninitialized value in print at /c/Users/User1/Desktop/ehCode_testingWindowsEnvironmentVariables_01.pl line 8.
The lines in question then are these:
print $localConfPath; #will print the app path - C:\users\xxx\AppData\local
print $appdata; #prints - C:\users\xxx\AppData\Roaming
I don't see why they shouldn't work.
I've checked Perl Monks, Perl Maven, Stack Overflow, and other popular Perl resources, to no avail. Even Active State did not have the answer.
When you access individual items of a hash, you need to use the scalar sigil, $ as opposed to the hash sigil, %:
perl -e 'print $ENV{APPDATA}'
Related
I am using this answer to compare the min version number that is required. But before i go to comparison, I am actually stuck on how to extract the version number.
My current script looks like this
#!/usr/bin/env bash
x=`pgsync -v`
echo "---"
echo $x
and its output is
> ./version-test.sh
0.6.7
---
I have also tried with x="$(pgsync -v)" and i am still getting an empty string. What am i doing wrong here.
If you're trying to capture a command's output in a variable and it's instead getting printed to the terminal, that's a sign the command isn't writing to its standard output, but to another stream - usually standard error. So just redirect it:
x=$(pgsync -v 2>&1)
As an aside, writing out an explicitly requested version number to standard error instead of standard output is counter intuitive and arguably a bug.
Also, prefer $() command substitution to backticks; see Bash FAQ 082 for details.
I am encountering a problem with the following perl script that someone else wrote to generate a uuid and print it to stdout. It likely has to do with a misconfigured perl setup on my Windows 7 machine with ActiveState's Perl from http://downloads.activestate.com/ActivePerl/releases/5.20.2.2001/ActivePerl-5.20.2.2001-MSWin32-x86-64int-298913.msi. I also have the Data::UUID perl module versiion 1.220 installed.
#!/usr/bin/perl
use UUID;
UUID::generate($uuid);
UUID::unparse($uuid, $string);
print $string . "\n"
When the script in a DOS shell is run with no arguments I get the following error:
Undefined subroutine &UUID::generate called at xxx
Any advice on how to debug this?
I am using Doxygen to generate HTML documentation and then run a Perl script to get function names.
To run Doxygen configuration, I need to run doxygen file_name in cmd.
But I want to run everything from Perl.
I tried this code
my $cmd = "perl -w otherscript.pl";
my $result = system("start $cmd");
But it just opens a cmd window. I need to execute cmd code directly through Perl (not a Perl command line, but through a Perl IDE). Is there a way to achieve this?
Your usage of system and start is OK.
From your description in the comment, I think it's because you're not using the correct escaping method when giving configure files to Doxygen that it throws such an error:
Error: configuration file C:SERSGHOSHBCD not found!
Try with
my $result = `doxygen C:\\Users\\aghosh\\abcd`;
In the two back-slashes, the former one is to escape the latter one, so that it's recognized by Windows as the directory separator.
I'm using ActivePerl on a Win 7 box and I want to use the Proc::Reliable CPAN module. It downloaded and installed properly but when I tried to run the following code, it failed at run
my $newProc = Proc::Reliable->new()
$newProc->run("perl.exe -e print 'hello world'");
I tried a couple things, such as testing the status and trying to retrieve output, but with no luck. As best as I can tell, the program dies silently on run.
For reference perl.exe is in my PATH variable and I'm calling this from commandline as: perl.exe test.pl
It probably isn't failing. -e print 'hello world' tells perl to execute the code print with #ARGV set to hello world (or perhaps ("'hello","world'"), I forgot how windows cmd quoting handles ''). This prints the contents of $_ (that is, undef) to STDOUT.
Always use warnings. Even on one-liners. Perhaps especially on one-liners. Compare:
$ perl -e print 'hello world'
$
and
$ perl -we print 'hello world'
Use of uninitialized value $_ in print at -e line 1.
$
Quoting is a little different in the Windows "shell". To get your mini-program to be interpreted as a single argument, try something like
perl.exe -e "print qq/hello world/"
I have contacted the author of the Proc::Reliable module and he confirmed that the module does not work on Windows.
I am having this strange issue with Perl. I am trying to execute an external program from inside my Perl script and this external program takes string + wildcard as parameters. My Perl program looks like this
my $cmd_to_run = 'find-something-in-somedb myname* |'
open(procHandle, $cmd_to_run); # I am using open because I want to
# parse the output using pipes
For some weird reason, running this Perl script (under Windows) call to open function ends up with error:
'sqlselect' is not recognized as an internal or external command
I guessed that its something to do with * present in my command string and hence I removed it and now my command string looks like this
my $cmd_to_run = 'find-something-in-somedb myname|'
Now when I run my Perl script it works perfectly fine. Problem comes only when wildcard character is present.
Some points to note :
I ran the same command with wildcard char, in the same cmd prompt (where i am executing this perl script) and it works perfectly fine..
Same command works when I program it in C using _open function in Windows.
Problem seems to be only when wildcard * is present , at least that's what I am guessing
No, I haven't tried this in Unix..
Any clues???
EDIT : I found that this is something to do with ENV . The program that i am trying to run uses "sqlselect" only when "*" wild card is present in search string...
Both find-something-in-somedb and sqlselect are present in same location. In which case how perl is able to find "find-in-db" and not "sqlselect"
Sorry i realize that original problem is turning out to be something else now.. Something to do with "ENV" and not with Wildcard *
It is recommended to use the 3-argument form of open
open(procHandle, '-|', 'find-something-in-somedb', 'myname*');
as that bypasses the shell (which will perform * expansion).
However, on Windows, applications often perform their own quote-parsing and * expansion, so you may need
open(procHandle, '-|', 'find-something-in-somedb', '"myname*"');
or even
open(procHandle, '-|', 'find-something-in-somedb "myname*"');
as I'm not sure exactly how and when Perl hands things off to cmd.
It's very likely that Perl is expanding the wildcard itself, which you don't want to do. The answer provided by ephemient is very good, but in order to debug this, try calling this really simple program:
print join ' ', #ARGV;
Put that into its own file, then call it from your original program (I named mine argv.pl):
my $cmd_to_run = './argv.pl myname* |'
open(procHandle, $cmd_to_run);
That will definitively tell you on your platform how Perl is parsing things. On Unix, the * is expanded to match the files in the current working directory. Not sure about Windows though.
What happens if you use three-argument open?
open my $procHandle, '-|', 'find-something-in-somedb myname*'
or die "Cannot open pipe: $!";