SCALAR output after creating executable from perl script - perl

I have a working perl script that I want to create an executable from. I used PAR::Packer and
pp -o try.exe run_scenarios_1.1.pl
to create the .exe file. Now, when I run the .exe in cygwin with
User#PC ~/my_perls
$ ./try.exe
I get the following output and the program cancels:
User#PC ~/my_perls
$ ./try.exe
SCALAR(0x60103eac8)
Here is my minimal code:
#!/usr/bin/perl
use strict;
use warnings;
use File::Slurp;
my $export_complete_path = "C:/Prog/Cygwin/home/User/my_perls/test/";
my #test_file = read_file($export_complete_path."Do_Test.txt");
my #combined_data = read_file($export_complete_path."Do_Test_Copy.txt");
### Take the original file and add the current test data
push (#combined_data, #test_file);
### add data to file
open my $newfile, '>', $export_complete_path."combined.txt" or die "\nError: Unable to create in $export_complete_path\n";
write_file($newfile, #combined_data);
It's my first bigger perl program and the first time I use the PAR::Packer.
Do I need to force the packer to do something special?
Thanks in advance :)

Related

Unable to execute perl script in cmd without invoking perl.exe

I am trying to execute a perl script to delete file1.txt in a directory.
When I execute the perl script using command prompt, I faced an error : Use of uninitialized value in chdir at C:/Debug/test.pl line 7.
Example:
C:\Debug>test.pl C:\Debug
However if I invoke perl in front of test.pl, the perl script is executed successfully and file1.txt was deleted.
Example:
C:\Debug>perl test.pl C:\Debug
Please find test.pl code as follow:
use strict;
use warnings;
use File::Copy;
my ($working_dir) = #ARGV;
chdir $working_dir or die "Can't change directory$!";
unlink "file1.txt";
I have mks_toolkit v8.7.5 with perl.exe installed. The .pl extension is associated with "C:\Program Files (x86)\MKS Toolkit\mksnt\perl.exe" "%1" %*.
I have no problem execute hello.pl (Hello World) without invoking perl in front of it.
Example:
C:\Debug>hello.pl
Anyway, users who face this problem can try to check the properties of your file, make sure the file is not read only and has full admin access.

How to make output file of one script, as an input file for the another script?

I am currently working on a project where in I am using Perl language to create command line application of one online tool.
There are total nine modules (for each module there is separate Perl script).
This Command Line Application should work in the following way-
Out of these nine modules user would be able to select any number of modules. (in short pipeline should be built).
after running first selected module, output files are generated.
output file of first module should be taken as an input file by the next module selected by the user.
My doubt is how we can make output file of first module as an input file for the next selected module.
It will be a great help if you solve my doubt as I am new to Perl programming.
Thanking you!
Tamar is right. You can use pipe command: "|". You can do this no matter if you're using windows or a unix based operating system.
Here's a simple example of what you're doing:
Code to output data
out.pl
#!/usr/bin/perl
use strict;
use warnings;
my $file = "output.txt";
my $data = "gasp";
unless(-e $file){
open(my $fh, '>', $file);
print $fh $data;
close $fh;
}
Code that takes input file
in.pl
#!/usr/bin/perl
use strict;
use warnings;
my $gaspage = <STDIN>;
chomp $gaspage;
print $gaspage."\n";
Then you just run it with the commands below that can be run within your perl application or just in the terminal:
perl out.pl
cat output.pl | in.pl

How to run set of .exe files in a folder through .bat file using perl script

I am beginner to Perl and I have to create a .pl file and I have folder containing near about 30 exe files(inside Folder1 in G:\Folder1). All of them must be executed by click to the .pl file.
My try is :
use strict; use warnings;
use autodie; # automatic error handling
while (defined(my $file = glob 'C:\shekhar_Axestrack_Intern*.exe'))
{
open my $fh, "<", $file; # lexical file handles, automatic error handling
while (defined( my $line = <$fh> )) {
do system $fh ;
}
close $fh;
}
Please let me know if my logic correct ? Could some one please correct me if i am wrong ?
Use system to execute an exe:
while (my $file = glob 'C:\shekhar_Axestrack_Intern\*.exe') {
system $file;
}
In addition, I have the feeling that you meant to write 'C:\shekhar_Axestrack_Intern*.exe'
instead of 'C:\shekhar_Axestrack_Intern*.exe'.
I think pl2bat may help you. It allows you to wrap Perl code into a batch file.
BTW why are you using echo in your Perl script? You should use print.
Edit: You have edited your question and now you want to know how to run all exe files from a folder using Perl?
Use the system command to run the exe files providing the full path.
See: How to run an executable file using Perl on Windows XP?
Edit 2: do system $fh ; This is not how you do it, please get a book (I'd suggest Beginning Perl by Ovid) and start learning Perl.

execute shell commands from perl script

I want to rename *.DIF files to *.SUC files
But the following script is giving "sh: bad substitution" error
I cannot use "rename" becuase my OS is not Linux.
$com="for i in *.DIF; do mv \$i \${i/DIF/SUC}; done;";
print $com."\n";
print `$com`;
Output :
for i in *.DIF; do mv $i ${i/DIF/SUC}; done;
sh: bad substitution
If you are having problems with Perl's rename, use File::Copy, a platform-independent module for moving and copying files. It is a core module, so it should be already part of your Perl installation.
If the system command works when you enter it in the shell, but not in Perl, the most likely reason is that Perl isn't using the shell you expect. We would need more information about your system to be sure, though.
There's no need to shell out for file operations that you can easily do within Perl.
The following renames all of your .dif extension files as .suc.
use strict;
use warnings;
use File::Copy qw(move);
move($_, s/dif$/suc/r) for glob('*.dif');
be default perl was using sh, instead of bash, which allows {//}
this question helped.
Finally I used :
open my $bash_handle, '| bash' or die "Cannot open bash: $!";
print $bash_handle 'for i in *.SUC; do mv $i ${i/SUC/DIF}; done;';

using export and perl -c in perl scripting

Since export cannot be used with a Perl script I've used the environment variable.
This code doesn't return any error but the command perl -c to check the syntax of the .pm file does not print the output.
myscript.pl
$ENV{'PATH'}='C:/Users/abc/Desktop/mno/wwwww/scripts/lib/perl/';
system("perl -c ContentModifySeasonPassOverlayRecord.pm");
Let me make another guess at what you want to do:
You want to batch syntax-check all your Perl modules, maybe in a cronjob. The script you are using to do that is located somewhere outside your working directory (where your framework sits). The scripts you want to check also sit somewhere else.
What you need to do is run the perl -c command from where the lib (framework) is, so that the working directory for the script while running has the lib files. You need to change the working directory before doing your perl -c call, and you need to include the full path to your scripts in the call.
#!/usr/bin/perl
use strict; use warnings;
# Change current working directory to where the framework is
chdir('/home/user/Desktop/QWARTS-0.6/autoinfra/lib/perl/');
# Run the perl -c command for each of your scripts you want to check
foreach my $script (qw(ContentModifySeasonPassOverlayRecord.pm otherfiles.pm)) {
system("perl -c /path/to/your/scripts/$script");
}
#!/usr/bin/perl
use warnings;
use strict;
system("perl -c /root/.cpan/build/DateTime-TimeZone-1.31-oqQt_7/lib/DateTime/TimeZone/America/Noronha.pm");
I don't see how it doesn't work?
# ./errr.pl
/root/.cpan/build/DateTime-TimeZone-1.31-oqQt_7/lib/DateTime/TimeZone/America/Noronha.pm syntax OK
I think you are doing a wrong way to execute a perl script with in perl script
here is the right way of executing a perl script with in perl script
use strict;
use warnings;
use IPC::System::Simple qw(system capture);
# Run a command, wait until it finishes, and make sure it works.
# Output from this program goes directly to STDOUT, and it can take input
# from your STDIN if required.
system($^X, "yourscript.pl", #ARGS);
# Run a command, wait until it finishes, and make sure it works.
# The output of this command is captured into $results.
my $results = capture($^X, "yourscript.pl", #ARGS);
And to check the errors in a module , You can just 'use' the module in your perl script and run the script in an usual way , if it has errors it will throw to stdout
If you want to test large number of perl modules you can build a shell script for that purpose .
#!/bin/sh
// List all modules
MODULES="Data::Dumper Foobar::Test"
for i in $MODULES ; do
if $(perl -M$i -e '1;' >/dev/null 2>&1 ); do
echo "Ok."
else
echo "No."
fi
done