How To: Perl for the Aspiring Hacker (Control Statements)

Perl for the Aspiring Hacker (Control Statements)

In this tutorial we will be go over how we can make use of conditionals.

In Perl the main conditional is the if statement. The if statement in
Perl is used like this:

If (example){
} else {
#execute a different type of code than above.

It is literally that simple! I know, right?

Let's make something cool with that.

use Net::Ping;
$host = "";
$example = Net::Ping->new("icmp", 1, 64);
if ($example->ping($host)) {
print "$host is very much alive.\n";
} else {
print "$host is dead. Hopefully he resurrects.\n";

So, let's review the source code. First we start with the Shebang. After the shebang we use a Perl Module, "NET::PING;". This might be the first time you see "Net::Ping;" if you're new to Perl. This module specifically exists to do ping functions. After that, we gave an IP address to the $host variable so it can than feed the IP to the module. We than say that we want to send our ping by ICMP (OTW covered what ICMP is I believe.). We than setup an 'if' statement that will tell us if the host returned with any errors. If it did, than the script will return with a " is dead. Hopefully he resurrects." If the host returns with 0 errors, it'll come back with '"Host is very much alive."

That is all for today! Thank you.

Be the First to Comment

Share Your Thoughts

  • Hot
  • Latest