The Storage

The Categories

The Meta

BBcode is very popular on forums, if you have never heard of it you may have seen it and don’t know it’s name

BBcode looks like [b][/b] for bold, [url=url]linktitle[/url] for links, etc
I will show you how to make that with Perl and PHP
You need a basic knowledge of regular expressions. I will explain some basic stuff here.

What are Regular Expressions?

Basically, Regular Expressions (also called regexp) are templates that matches a given string.
For example
PHP:

Those codes will look for fedekiller in the given string, in perl, we use // and put the pattern there, but that was because we did not specify we were matching, if you want to use other separators, you can use for example || or {} but you have to specify you are matching. We will use // here, we dont want to complicate it so much, we also use i after the / to indicate that its CaSe InSeNsItIvE, we also specify case insensitive in PHP after the @
As you have probably realised, to match something in perl we use $string =~/pattern/;
And in PHP we use preg_match(@pattern@,$string);

QuantifiersQuantifiers are used for repetition
* Matches 0 or more times
+ Matches 1 or more times
? Matches 0 or 1 time
{2} Matches exactly 2 times
{2,} Matches 2 or more
{2,5} Matches 2, 3, 4 or 5 times

Example:
Regex:

fe+dekil{2}er

Will Match:

feeeeeedekiller

GroupingYou can also match by group, using “(” and “)”, that will also make a variable with the content between the “(” and “)”
Example:
Regex:

(fede)+

Will Match:

fedefedefede

We can now acess to the content of the first () with the variable $1, for example if we did something like
Regex:

\[b\](.+?)\[\/b\]

Will Match:

[b]Something[/b]

And the variable $1 will contain SomethingAlternativesYou can also match one thing OR other thing, using the ‘|’ bar
Example:
Regex:

(fede|killer)+

Will Match:

killerkillerfedekillerfede

Modifiers:The modifiers are:
i – Will make it CaSe InSeNsItIvE
g – Global match
m – Multiple lines match
s – Single line match
x – Allow comments and white space in the pattern
e – Evaluate placement
U – Ungreedy patternEscape CharacterSometimes we want to use something for example () or {} but we dont want to use that function, for example in BBCode we want to match [b][/b] but no use the []’s function, so, we escape them with the \ character

AnchorsIf you want something to match only at the beggining or the end of an string, we can use anchors
^ – Start of a string
$ – End of a string
\b – Word boundary
\B – Not word boundary
\< – Start of a word
\> – End of a word

What is boundary?Word boundary is if we want to match that string only in a complete word, its for making sure for example we wont find cat in category.

We use the i after the @ to choose case insensitive, we also use the grouping function to get everything between the tags and then a variable wich contains that and put it inside the corresponding HTML tags
We do exactly the same in Perl
In Perl:

in PHP, preg_replace is like this preg_replace(regex,replace_with,string)
In Perl
string =~ /regex/replace_with/;
If we dont specify the variable in Perl it will automatically use $_, but we have to specify that we are replacing with the letter s
So it will look like
s/regex/replacement/;

Today i will explain the differences between PHP and Perl with Cookies.

PHP is way better than Perl for cookies (maybe mod_perl could do a better job but PHP is still better) anyways, its not a big deal to get or set a cookie in Perl.

In Perl cookies can have maximum 6 attributes, in PHP they can have 7, most of them are optional, but they ones we are going to explain here are 3 attributes, which are in both languages, name, value and expire, the most basic attributes ;3

Before we start, i want to write the possible attributes, just for you to know them

In PHP:

Name
Value
Expire
Path
Domain
Secure
Httponly

In Perl:

Name
Value
Expire
Domain
Path
Secure Flag

Okay, now we can start explaining the 3 basic attributes, value, name and expire

In PHP we use the time function and multiply till we add one month (60 seconds * 60 minutes * 24 hours * 30 days)
In Perl, we just set +1M to make it last 1 month, if we want to delete a cookie, we have to use a negative value in expires, for example, in PHP would me time()-60*60… and in Perl it would be -1M

Now, lets read a cookie, with PHP is very easy

In PHP:

$_COOKIE[‘user’];

That will read the cookie value (in this case, fedekiller)

In Perl:

my %cookies = fetch CGI::Cookie;
$cookies{‘user’}->value

We set all cookies to the cookies hash, and then we just request the value.

I made a little function to get cookies in Perl so its a little bit easier

Remember to change the action field
In that HTML code, we make a form with a POST method, and a multipart/form-data encryption. Thats very important!, so do not change it.
Then we make a simple file input and a submit button.

Now, the code 🙂

In PHP:

<?php
if($_POST[‘Submit’]) // If the form is submitted
{
$avatar = $_FILES[‘avatar’][‘tmp_name’]; // Avatar temp name
$avatar_name = $_FILES[‘avatar’][‘name’]; // Avatar name
$ext = array(‘.gif’,’.jpg’,’.jpeg’,’.png’); // Array with allowed extensions
$my_avatar_name = ‘avatar.gif’; // This is the name that our avatar will have
$do = false; // Set the variable $do as false
foreach($ext as $e) // Loop trought the array
{
if(eregi($e, $avatar_name) // If it find the extension in the avatar name, it will stop looping
{
$do = true; // Set do to true
break; // Stop looping
}
}
if($do) // If $do is true
{
if(copy($avatar, ‘avatars/’.$my_avatar_name)) // Copy the avatar
{
echo ‘Avatar uploaded!’; // if it could copy, we show this message
}
else
{
echo ‘Error uploading avatar :(‘; // Else, if it did not copy, we will show this message
}
}
}
?>

In Perl:

#!/usr/bin/perl
use CGI’:all’; # We use all CGI methods
if(param(‘Submit’)) # If the form is submitted
{
my $avatar = upload(‘avatar’); # Avatar temp name
my $avatar_name = param(‘avatar’); # Avatar name
my @ext = qw/.gif .jpg .jpeg .png/; # Array with allowed extensions
my $my_avatar_name = ‘avatar.gif’; # This is the name that our avatar will have
my $do = 0; # Set the variable $do as false
foreach (@ext) # Loop trought the array
{
if($avatar_name =~ /\.$_$/i)) # If it find the extension in the avatar name, it will stop looping
{
$do = 1; # Set do to true
last; # Stop looping
}
}
if($do) # If $do is true
{
open(AVATAR, “>avatars/$my_avatar_name”); # We create a file to write
binmode AVATAR; # We set it to bin mode to make sure it work in UNIX systems
while(<$avatar>)
{
print AVATAR; # Print the content byte per byte
}
close AVATAR; # Close the file
print header,start_html(); # Printe the html header and start with the html, header and body tags
if(-e “avatars/$my_avatar_name”)
{
print ‘Avatar uploaded!’; # if it could copy, we show this message
}
else
{
print ‘Error uploading avatar :(‘; # Else, if it did not copy, we will show this message
}
print end_html; # Close the body and html tags
}
}

Now, in PHP, we use an array with keys and values to hold the data, but in Perl, arrays are only numeric, and we have to use hashes to have a key and value interface. We could have used fetcharray instead of fetchhash but thebn, we would have to use $r[1] instead of $r{‘content’}. That does not happen with PHP, because PHP arrays can have only values with numeric keys, or act like Perl hashes or Python dictionaries.