astronogun has asked for the wisdom of the Perl Monks concerning the following question:
Hi,
I'm a newbie in Perl and recently had a project in school, I would like to know how to display the process in cmd when I run my perl script..
For example I have this project.pl and I run this via Strawberry (CMD). I want to know how to display the process that it's actually running... or at least will display that the execution is successful or not. Because when I run my script it just displayed the whole directory of C:\ and by that I know that it run successfully. But while on execution it just display blinking _ so I don't know if it's hang or something... Hope for your help because I'm so new to this (just 3 weeks learning phase) ehehe! thanks
Re: How to display the .pl process in command line
by bms (Monk) on Mar 27, 2012 at 07:47 UTC
|
Well, I have a couple recommendations. For one, please organize your code. Remember DRY(Don't Repeat Yourself), there is a lot of code duplication in that snippet. And also think about logically grouping your code. Secondly(and most importantly), I think you want IO::Tee.
use IO::Tee;
my $out_handles = new IO::Tee(\*STDOUT, new IO::File('>random_file.txt
+'));
print $out_handles "*Hey Buddy*\n";
print $out_handles "*I'm on your screen and your file!*\n";
So, essentially, IO::Tee allows you to write to multiple file handles simultaneously.
You could use a second print statement with just a string to accomplish the same result. Or select. Plus a bunch I can't think of right now. Always a lot of choices. | [reply] [Watch: Dir/Any] [d/l] |
|
it failed..
I download the IO::File at cpan then when i run the command it said that Can't locate object method "new" via package "IP::File (perhaps you forgot to load "IP::File"?)
the i add use IP::File then it said that can' locate IP/File.pm
then I checked cpan for download there's no IP::File..
did do it incorrectly? Thanks
| [reply] [Watch: Dir/Any] |
|
IO::File and IO::Tee. Not sure what IP::File is, looks like a typo. You may have to 'use IO::File'. It is a core module.
| [reply] [Watch: Dir/Any] |
|
|
|
Re: How to display the .pl process in command line
by bms (Monk) on Mar 27, 2012 at 04:33 UTC
|
Okay... Can you post your code? I have several ideas for this, but I'd like to gear it to your particular context.
| [reply] [Watch: Dir/Any] |
|
Here it goes much proper way to view: :)
open(OUTFILE, ">", "check_result.txt") or die ("unable to write output
+: $!");
my @url = ('www.yahoo.com', 'www.google.com' ); #for check Ping & c
+heck TCP
#check Ping
print OUTFILE "*Ping Result*\n";
use Net::Ping;
$p = Net::Ping->new("syn");
if ($p->ping($url2[0]))
{
print OUTFILE "$url2[0] is alive. \n";
}
else
{
print OUTFILE "$url2[0] is not alive. \n";
}
if ($p->ping($url2[1]))
{
print OUTFILE "$url2[1] is alive. \n\n";
}
else
{
print OUTFILE "$url2[1] is not alive. \n\n";
}
#check TCP Result
use strict;
use Socket;
use Time::HiRes qw( gettimeofday );
use Net::hostent;
print OUTFILE "*TCP Result*\n";
my $timeout = 3;
my $hostname = ($url2[0]);
my $hostname1 = ($url2[1]);
my $portnumber = '80';
my $portnumber1 = '80';
my $host = shift || $hostname;
my $host1 = shift || $hostname1;
my $port = shift || $portnumber;
my $port1 = shift || $portnumber1;
my $proto = getprotobyname('tcp');
my $iaddr = inet_aton($host);
my $iaddr1 = inet_aton($host1);
my $paddr = sockaddr_in($port, $iaddr);
my $paddr1 = sockaddr_in($port1, $iaddr1);
socket(SOCKET, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
+#for casino.sbobet.com
eval {
local $SIG{ALRM} = sub { die "timeout" };
alarm($timeout);
connect(SOCKET, $paddr) || error();
alarm(0);
};
if ($^P) {
close SOCKET || die "close: $!";
print OUTFILE "$hostname is NOT listening on tcp port $portnumber.\n
+";
}
else {
close SOCKET || die "close: $!";
print OUTFILE "$hostname is listening on tcp port $portnumber.";
my ($t02, $t12, $startTime2);
$startTime2 = gettimeofday();
connect(SOCK, $paddr);
$t02 = gettimeofday() - $startTime2;
close (SOCK);
$t02 = gettimeofday() - $startTime2,
printf OUTFILE (" avg:%.8f", $t02);
print OUTFILE ("ms\n");
}
socket(SOCKET, PF_INET, SOCK_STREAM, $proto) || die "socket: $!"; #f
+or casino1.sbobet.com
eval {
local $SIG{ALRM} = sub { die "timeout" };
alarm($timeout);
connect(SOCKET, $paddr1) || error();
alarm(0);
};
if ($ARGV) {
close SOCKET || die "close: $!";
print OUTFILE "$hostname1 is NOT listening on tcp port $portnumber1.
+\n";
}
else {
close SOCKET || die "close: $!";
print OUTFILE "$hostname1 is listening on tcp port $portnumber1.";
my ($t02, $t12, $startTime2);
$startTime2 = gettimeofday();
connect(SOCK, $paddr1);
$t02 = gettimeofday() - $startTime2;
close (SOCK);
$t02 = gettimeofday() - $startTime2,
printf OUTFILE (" avg:%.8f", $t02);
print OUTFILE ("ms");
}
| [reply] [Watch: Dir/Any] [d/l] |
|
okay this code is just a check ping and check tcp here it is:
note: that I didn't make the script just edit and modify as part of my learning :)
open(OUTFILE, ">", "check_result.txt") or die ("unable to write output: $!");
my @url = ('www.yahoo.com', 'www.google.com' ); #for check Ping & check TCP
#check Ping
print OUTFILE "*Ping Result*\n";
use Net::Ping;
$p = Net::Ping->new("syn");
if ($p->ping($url2[0]))
{
print OUTFILE "$url2[0] is alive. \n";
}
else
{
print OUTFILE "$url2[0] is not alive. \n";
}
if ($p->ping($url21))
{
print OUTFILE "$url21 is alive. \n\n";
}
else
{
print OUTFILE "$url21 is not alive. \n\n";
}
#check TCP Result
use strict;
use Socket;
use Time::HiRes qw( gettimeofday );
use Net::hostent;
print OUTFILE "*TCP Result*\n";
my $timeout = 3;
my $hostname = ($url2[0]);
my $hostname1 = ($url21);
my $portnumber = '80';
my $portnumber1 = '80';
my $host = shift || $hostname;
my $host1 = shift || $hostname1;
my $port = shift || $portnumber;
my $port1 = shift || $portnumber1;
my $proto = getprotobyname('tcp');
my $iaddr = inet_aton($host);
my $iaddr1 = inet_aton($host1);
my $paddr = sockaddr_in($port, $iaddr);
my $paddr1 = sockaddr_in($port1, $iaddr1);
socket(SOCKET, PF_INET, SOCK_STREAM, $proto) || die "socket: $!"; #for casino.sbobet.com
eval {
local $SIG{ALRM} = sub { die "timeout" };
alarm($timeout);
connect(SOCKET, $paddr) || error();
alarm(0);
};
if ($^P) {
close SOCKET || die "close: $!";
print OUTFILE "$hostname is NOT listening on tcp port $portnumber.\n";
}
else {
close SOCKET || die "close: $!";
print OUTFILE "$hostname is listening on tcp port $portnumber.";
my ($t02, $t12, $startTime2);
$startTime2 = gettimeofday();
connect(SOCK, $paddr);
$t02 = gettimeofday() - $startTime2;
close (SOCK);
$t02 = gettimeofday() - $startTime2,
printf OUTFILE (" avg:%.8f", $t02);
print OUTFILE ("ms\n");
}
socket(SOCKET, PF_INET, SOCK_STREAM, $proto) || die "socket: $!"; #for casino1.sbobet.com
eval {
local $SIG{ALRM} = sub { die "timeout" };
alarm($timeout);
connect(SOCKET, $paddr1) || error();
alarm(0);
};
if ($ARGV) {
close SOCKET || die "close: $!";
print OUTFILE "$hostname1 is NOT listening on tcp port $portnumber1.\n";
}
else {
close SOCKET || die "close: $!";
print OUTFILE "$hostname1 is listening on tcp port $portnumber1.";
my ($t02, $t12, $startTime2);
$startTime2 = gettimeofday();
connect(SOCK, $paddr1);
$t02 = gettimeofday() - $startTime2;
close (SOCK);
$t02 = gettimeofday() - $startTime2,
printf OUTFILE (" avg:%.8f", $t02);
print OUTFILE ("ms");
}
| [reply] [Watch: Dir/Any] |
Re: How to display the .pl process in command line
by bms (Monk) on Mar 27, 2012 at 03:48 UTC
|
What exactly do yo want to do? Stop the command prompt window from immediately closing? Display the script that's running? I don't get what you want to know...
| [reply] [Watch: Dir/Any] |
|
What I want to do is... When I run my .pl file via Strawberry (CMD) or windows cmd it will display that the script is actually running.. Right now when I run the script it will display the C:\Users\Vin\Documents and a blinking underscore.. So if success it will display again the C:\Users\Vin\Documents
note: the output of my script is via .txt file
what i want to see in the cmd is somewhat like this:
C:\Users\Vin\Documents\<file>.pl
Processing....
Successful check the <file>.txt for the result
or
Execution failed.. (if the script have problems or failed to execute)
something like that. hope that you got me thanks :)
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
|
|