Jump to content

Recommended Posts

Posted

Lesson 3

 

What are If statements and loops?

 

Every Perl script is made of instructions which tell theinterpreter what to do. Depending on the purpose of the script you'llwrite different instructions. Sometimes, you might want someinstructions to be executed only on certain conditions. To do this,you'll use If statements. A typical If statement is aninstruction which defines three elements:

 

    *

 

      A condition

    *

 

      A set of instruction to execute if the condition is met

    *

 

      A set of instruction to execute if the condition is not met

 

For instance,consider the following code:

 

if ($time > 12){

print "Good Afternoon";

}

else {

print"Good Morning";

}

 

 

In this Ifstatement, the condition is ($time > 12). If the condition is metthen the script prints "Good afternoon", otherwise itprints "Good morning".

 

Sometimes, you might wantsome instructions to be executed more than once. For this you can useloops. A typical loop is composed of a condition and a set ofinstructions to execute. There are different kinds of loops and wewill study them in the chapters below.

 

If statements in Perl

 

Perl is a very flexible language and this is particularly truewhen it comes to If statements.

 

Simple If-statements

 

The most common form of If statement in programming is composed ofa condition (between parenthesis) followed by a set of instructionswhich are placed between brackets. For instance:

 

if ($time > 12) {

print "time is $time\n";

print"Good Afternoon\n";

}

 

 

Although it is considered bad practice, you don't have towrite the brackets if there is only one instruction in the Ifstatement:

 

if ($time > 12)

print "Good Afternoon\n";

 

To avoid any possible confusion, it is recommended to always writethe brackets, or, if there is only one simple instruction to placethe condition and the instruction on the same line:

 

if($time > 12) print "Good Afternoon\n";

 

In Perl, you can also write the condition after the instructionitself. For instance:

 

print "Good Afternoon\n" if($time > 12);

 

Else and Elsif Statements

 

Similarly, you can define an “else” block of instructions,which will be executed if the condition is not met:

 

print"time is $time\n";

if ($time > 12) {

print "GoodAfternoon\n";

}

else {

print "Good Morning\n";

}

 

Sometime you may want to dosomething depending on more than one condition. Of course you canalways define If statement within others, but you can also use elsif,as a contraction of “else, if (something else..)”:

 

if ($time < 12) {

print "GoodMorning\n";

}

elsif ($time < 20) {

print "Good Afternoon\n";

}

elsif ($time < 23) {

print "Good Evening\n";

}

else {

print "Good night\n";

}

 

Unless-statements

 

In Perl, you can use the Unlessstatement. It behaves exactly like the If statement but executes thecode if the condition is not met. For instance the following codeprints “Good night” if it's more than 11pm:

 

print "Good night\n" unless($time < 11pm);

 

Loops in Perl

 

There are many different kinds of loop in Perl. Here are the mostcommon types.

 

While loop

 

This loop executes a block of code while a condition remains true.For instance, the following code prints “Hello” three times:

 

$i = 0;

while ($i < 3) {

print "Hello\n";

$i = $i + 1;

}

 

Until loop

 

This loop executes a block of code until a condition becomes true.For instance, the following code prints “Hello” three times:

 

$i = 1;

until ($i > 3) {

print "Hello\n";

$i = $i + 1;

}

 

For loop

 

The for loop is composed of four elements:

 

    *

 

      A starting assignment

    *

 

      A condition

    *

 

      An incrementation

    *

 

      A set of instructions

 

The notation of the For-loop is as follows:

 

for (starting assignment; condition; incrementation) {

instructions

}

 

The for loop starts by executing the starting assignment. Then,while the condition is met, it keeps executing the instructions andthe incrementation. For instance the following For-loop prints“Hello” three times:

 

for ($i = 0; $i < 3; $i = $i + 1) {

print "Hello\n";

}

 

Note that the ++ operator is usually used to increment a variable,so we could have written $i++ instead of $i = $i + 1;

 

In Perl, you can also use the For-loop with a “range operator”.The notation for the range operator is “..”. For instance, thefollowing code prints “Hello” three times:

 

for (1..3) {

print "Hello\n";

}

Foreach loop

 

The foreach loop is used to iterate through the elements of anarray. For instance, the following code prints the days of the week:

 

@days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");

foreach $dayOfTheWeek (@days) {

    print "$dayOfTheWeek\n";

}

 

You can also use the foreach loop to iterate through the keys of anhashtable. For instance, the following code prints the days of theweek:

 

%days = ();

$days{"Monday"} = 1;

$days{"Tuesday"} = 2;

$days{"Wednesday"} = 3;

$days{"Thursday"} = 4;

$days{"Friday"} = 5;

$days{"Saturday"} = 6;

$days{"Sunday"} = 7;

 

foreach $day (keys %days) {

print "$day is the day number $days{$day} of the week\n";

}

 

File Handling

 

The Perl language was designed to make file handling easy andefficient, so you'll probably won't have any problem opening filesand reading them.

 

Opening and closing files

 

In order to open a file, you'll use the “open” instructionwhich takes two arguments: the name of a filehandle and the name ofthe file itself. The filehandle is like a variable which representsthe handling of the file within the script. For instance, in thefollowing code we're opening the file “clients.txt” with afilehandle called CLIENTS:

 

open (CLIENTS, "clients.txt");

 

By default, the file is open in read-mode, which means you canonly read from it. You can decide to open a file in write-mode, inorder to be able to write into it. If the file already existed or hadsome data written into it, the data will be lost. To open a file inwrite-mode simply add a “>” in front of the file name:

 

open (CLIENTS, ">clients.txt");

 

If you'd rather keep the data written in the file, you can openthe file in append-mode. This way, the data will be kept, and whatyou'll write in the file will be appended to it. In order to do thisadd a “>>” in front of the file name:

 

open (CLIENTS, ">>clients.txt");

 

The “open” instruction returns true if it managed to open thefile, false otherwise. You can use this value in order to test thesuccess of the operation. For instance, the following code opens thefile in write-mode and prints “Insufficient privileges” if thescript doesn't manage to do so.

 

open (CLIENTS, ">clients.txt") or print "Insufficientprivileges\n";

 

Remember to always close the files once you're finished with them.If you don't your changes might be lost. In order to close a file,simply use the “close” instruction on the filehandle:

 

close (CLIENTS);

 

Writing into files

 

Once the file is open in write mode you can write into it simplyby writing into its filehandle. The “print” instruction writesthings on the screen by default, but you can specify a filehandle towrite into. For instance, the following code adds a line “Mr JohnDoe” in the end of the “clients.txt” file:

 

open (CLIENTS, ">>clients.txt") or die "Insufficientprivileges\n";

print CLIENTS "Mr John Doe\n";

close (CLIENTS);

 

Reading data from files

 

There are many ways to read the content of a file. Here are thetwo most common ways in Perl.

 

Copying the content of the file into an array

 

You can copy the whole content of the file into an array. Eachline will then correspond to an element of the array. Here is anexample:

 

open(CLIENTS, "clients.txt");

@lines = ;

close(CLIENTS);

print @lines; 

 

Looping through the filehandle

 

Alternatively you can loop through the filehandle in a while loopby writing while($line = ) (think of it as “whilethere are lines in the clients file, I'm assigning the current lineto $line):

 

open (CLIENTS, "clients.txt");

while ($line = ) {

print $line;

}

close (CLIENTS);

 

As you can see, Perl makes it very easy to manipulate files. Inthe next lesson we'll look at how to search for a particular elementwithin a file, and how to handle and manipulate strings.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Posts

    • WoW Midnight — AFK Fishing Bot v2.0     Fully rewritten. No more pixel detection. V2 is audio-only and paired with a companion addon that handles everything inside the game — one keybind does it all.     Showcase:                  What's new in v2.0 Audio-only detection. Listens for the splash. No screen scanning, no calibration, no zone tuning. It just works anywhere. One key for everything. Same keybind casts the line and loots the catch. Bound via the included BobberAssist addon. Auto-buff system. Drop your lures, teas, potions (Haranir Phial, Sanguithorn Tea, Ominous Octopus Lure, etc.) into the addon. Set a timer. The bot refreshes them automatically before each cast. Sit-and-fish mode. Optional — bot types /sit when you start. Fewer interrupts from Root Crabs and roaming mobs. Smart anti-AFK. Fires only between catches, never mid-cast. AutoDestroy addon included. Junk items deleted automatically after every catch. VB-CABLE support. Play music, Discord, YouTube with zero interference. Randomized timing — natural 50–120 ms reactions.                       Features One-key start (F6) — press and walk away Audio isolation via VB-CABLE — play music, YouTube, Discord with zero interference Configurable consumable auto-use (lures / baits / potions / teas) with per-item timers Sit-and-fish mode to avoid ambient mob aggro Anti-AFK built in — jumps every 5 minutes in stand mode, jump + re-sit in sit mode (between catches only, never mid-cast) AutoDestroy addon included — automatically deletes junk items to keep bags clean after every catch Randomized splash→loot and loot→recast delays (natural timing, 50–120 ms reaction) Works in any zone, any fishing spot No pixel detection means no zone-specific tuning — it just works        Why v2 > v1 V1 scanned your screen for the bobber. It broke when the UI changed, the camera moved, or a new zone had different visuals. V2 only listens for the splash — nothing visual to break. Plus the new addon lets a single keybind cast, loot, and use your buffs. Way less setup, way more reliable.            Safety Runs completely external. No injection, no memory reads, no file edits. Watches nothing, listens to audio, presses one key. Use at your own risk. Automation is against Blizzard's ToS.          V1 owners Free upgrade. DM me for the new build.   One-time payment, lifetime access. DM on Discord: Nythrand
    • Hi as the title says, I need a working script for anti captcha. First window appears and have to click continue, then comes up another to type the code. Tell me how much u want for help $$.    
    • Did your boss send you to post this? hahaha
    • TG Support: https://t.me/buyingproxysup | Channel: https://t.me/buyingproxycom Discord support: #buyingproxy | Server: Join the BuyingProxy Discord Server!  Create your free account here
  • Topics

×
×
  • Create New...

Important Information

This community uses essential cookies to function properly. Non-essential cookies and third-party services are used only with your consent. Read our Privacy Policy and We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue..