The title is pretty clickbaiting. But read me out: Yes, you can learn a new language in just 4 hours. Other thing is mastering it...
So, as this is the first language of my #12in24, I wanted to start small. I will use this video to learn the basics: Perl Programming Course for Beginners.
The basics
File structure
#!/usr/bin/env perl
# Good ol' shebang
use strict; # This does not allow execution if errors
use warnings; # This prints the errors in console
print "DEV community is cool!"; # The actual program
1; # Not always present, indicates that compilation succeeded
# Don't forget ; at the end of the lines
Data types: Scalars
my $number = 1;
my $decimal = 3.14;
print $number + $decimal; # 4.14
print $number - $decimal; # -2.14
# ...
my $string = "Follow me ";
my $string2 = "on dev.to!";
print $string . $string2; # Follow me on dev.to!
# Variables are case sensitive
my $variable = 1;
# is different to
my $Variable = 1;
# To declare a global variable
our $global_variable = 5;
Data types: Arrays
my @array = (
1,
"two",
3.14
);
print @array;
# $VAR1 = 1;
# $VAR2 = "two";
# $VAR3 = "3.14";
print $array[0]; # This will print 1
my $size = @array;
print $size; # This will print $VAR1 = 3;
my @array2 = ( 3 .. 7 );
# $VAR1 = 3;
# $VAR2 = 4;
# $VAR3 = 5;
# $VAR4 = 6;
# $VAR5 = 7;
# If you use ( "a" .. "d" ) it will print each letter from a to d
# You can use it for array indexes as well
push @array2, 8; # Adds number 8 to the array
pop @array; # Removes last element
shift @array; # Removes first element
unshft @array, 0; # Adds element at the beginning
Subroutines
sub my_function {
$hi = "Hello";
}
print my_function(); # This will print Hello
sub my_name( $name ) {
print $name;
}
my_name("Alex"); # This will print Alex
Conditionals
$test = 3;
if ( $test > 1 ) {
print "Hi!";
}
elsif ( $test < 4 ) {
print "How are you?";
}
else {
print "Goodbye!";
}
Loops
$test = 0;
while ( $test < 5 ) {
print "Count: $test";
$test++;
}
# Count: 0
# Count: 1
# Count: 2
# Count: 3
# Count: 4
for ( my $count = 0; $count < 3; $count++; ) {
print "Hello"
}
# Hello
# Hello
# Hello
The video covers way more than this, I know, but these are the basics. With a little help of the documentation, I will be fine for the next sections.
Practice
Now, I will put my knowledge to test using Exercism.
Starting here!
Hello World
I must say that Exercism complicates way too much the exercises. But anyway, it's not bad to practice.
Project
I wanted to do something related to Cybersecurity but not too complex as I am new to Perl. After some deliberation, I came up with this:
A very simple SQLi Vulnerability Finder. It is simple as it is just a PoC but can be enhanced to be usable just by adding payloads and places to check.
#!/usr/bin/env perl
use strict;
use warnings;
use LWP::UserAgent;
sub check_sql_injection {
my ($url) = @_;
my $payload = "' OR '1'='1";
my $test_url = $url . "?id=" . $payload;
my $ua = LWP::UserAgent->new;
my $response = $ua->get($test_url);
if ($response->content =~ /error in your SQL syntax/i) {
return "Vulnerable to SQL Injection: $url";
} else {
return "Not Vulnerable: $url";
}
}
# Prompt the user for the target URL
print "Enter the target URL: ";
my $target_url = <STDIN>;
chomp $target_url;
# Example usage:
my $result = check_sql_injection($target_url);
print $result, "\n";
Hope you enjoyed it! See you next month learning COBOL.
Top comments (0)