Issue installing and referencing CPAN module in Strawberry Perl on Windows 7 - perl-module

I have downloaded a perl module cfrom CPAN using Strawberry Perl's CPAN client tool. It got successfully installed. I wrote a small perl script to use the module. But I am getting the following error:
Undefined subroutine &main::chisquare called at C:\MyPadreFiles\UseChiSquare.pl
line 9.
Press any key to continue . . .
My perl code is given below:
use strict;
use warnings;
use Statistics::Chisquare;
my(#subwaystops) = (14,18,23,28,34,42,50,59,66,72,79,86,96,103);
print chisquare(#subwaystops);
I even tried Statistics::Chisquare::chisquare(#subwaystops);
But still the same undefined subroutine error. Please let me know how to resolve it.
Thanks

There was a typo in my code. The corrected one is given below:
use Statistics::Chisquare; wrong one
use Statistics::ChiSquare; correct one
Thanks

Related

Internal Server Error with perl on Big Sur

I have a series of perl scripts I am unsing since many years. Now I want to modify one of them and copy the code into a new file with another name (modified with _n) It is in the same location and with the same permissions. Running it I get
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
In the server log I read
End of script output before headers
I tried the same thing with other files that all work ok on my system, with the same result: Internal server error.
I also used a very simple script as
#!/usr/bin/perl
print "Content-type: text/html\n\n";
use strict;
use warnings;
print "Hello, world!<br />\n";
with the same result.
What is the difference that leads to this error?
MacOS Big Sur 11.2.3
Perl 5.34, reinstalled
BBEdit 14.0.1
Fetch 6.8.2
adding -w to the shebang solved the problem. Silly...

Access Perl environment (shell) variables in Windows - $ENV not working

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}'

Undefined subroutine &UUID::generate called at

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?

Error when using lua filename.lua

I'm very new to lua, and I've been trying to create a lua file using TextWrangler, then execute the file in my terminal (using a Mac). I create the following in a textwrangler file:
for i=1,10 do
print ("Hello")
end
print ("That's all!")
and I save it as test.lua. I then move this file into the lua directory, ~/lua-5.2.3. I then start lua, and use the following command:
lua test.lua
and I get the following error:
stdin: 1: syntax error near 'test'
What am I doing wrong here? I've looked everywhere online for a solution to, what I assume, is a very simple oversight on my part, but I have found nothing. My first thought was that I was putting the file in the wrong place, but I have moved it everywhere with the same result.
You can execute script typing lua test.lua in terminal.
If you enter interpreter mode, you can use dofile "test.lua". There is no lua command(function ) there, unless you declare it somewhere.
There is a PIL section about stand-alone interpreter usage and more up-to date reference section.

How to check availability of Perl, its version and presence of a required module?

I have written a Perl script, I just want to give it to every one,
for that I planned to write a bash script which is used to test the environment of a user and find whether that environment is capable of running the Perl script.
I want to test the things like:
Whether Perl has installed in that system
Perl should have the version 5 or more
Whether the module JSON::Any is available
Any suggestion would greatly appreciated :-)
No, do not write a shell script. Perl already has a perfectly fine way of doing this. The correct way to do this is to build a CPAN-ready distribution using the normal toolchain. Some of this is explained in perlnewmod, perlmodstyle and perlmodinstall.
For a minimal working example, create a directory layout thus:
.
├── Build.PL
├── README
└── script
└── abuscript.pl
In the Build.PL file, put:
use 5.000;
use Module::Build qw();
Module::Build->new(
module_name => 'abuscript',
dist_version => '1.000',
dist_author => 'abubacker <abubacker#example.com>',
dist_abstract => 'describe what the script does in one sentence',
configure_requires => {
'perl' => '5.000',
},
requires => {
'JSON::Any' => 0,
},
)->create_build_script;
Change the details to suite your purposes.
In the README file, put some installation instructions, for instance:
To install this module, run the following commands:
perl Build.PL
./Build install
Once you're done with all that, you run:
perl Build.PL
./Build manifest
./Build dist
This will result in a .tar.gz archive which you will distribute. Tell your users to install it like any other CPAN module, or if they don't know what that means, they should read the README.
If you have time, I recommend converting your script to a module. The program pl2pm (comes with Perl) and the CPAN module Module-Starter-PBP help you.
If license permits, it is possible to upload your code to CPAN to make it even more convenient for your users. Ask for help in any of the following places first: mailing list module-authors#perl.org, web forum PerlMonks, IRC channel #toolchain on MagNET (irc://irc.perl.org/toolchain)
Regarding checking Perl availability the easiest way to do it is to check the return code (exit code) of the command perl -v,if this is not 0, you do not have Perl.
Now regarding Perl requirements, you should deal with them from inside your Perl script:
#!/usr/bin/env perl
use 5.006_001;
use ModuleName 2.0;
The above Perl code will run only with perl 5.6.1 or newer and with modele "ModuleName" version 2.0 or newer. There is no need to manually check the Perl version from bash, it is better and easier to do it directly from the Perl script.
References:
Requiring a minimal Perl version
Checking if a Perl module is installed
if perl -MJSON::Any -e 'print "$JSON::Any::VERSION\n"' >/dev/null 2>&1
then : OK
else echo "Cannot find a perl with JSON::Any installed" 1>&2
exit 1
fi
I often use '${PERL:-perl}' and similar constructs to identify the command (for awk vs nawk or gawk; troff vs groff; etc).
If you want to test the version of JSON::Any, capture the output from the command instead.
If you want to test the version of Perl, add 'use 5.008009;' or whatever number you think is sensible. (It wasn't so long ago that they finally removed Perl 4 from one of the NFS-mounted file systems at work - but that was not the only Perl on the machine - at least, not in the last decade or more!)

Resources