Category Archives: Perl

Find intresting solutions for some Perl problems

Perl and Google Authenticator

In this article I’ll explain (with code) how to create and validate a Google Authenticator two-step authentication token, using Perl. Both on the command-line and on a simple website. Some background Google Authenticator is a two-step authentication process created by Google. It’s software based, unlike those hardware tokens your bank may use, and when paired… Read More »

Send email from Gmail using Perl

You can send email from Perl using the following code: use Email::Send; my $mailer = Email::Send->new( { mailer => ‘SMTP::TLS’, mailer_args => [ Host => ‘smtp.gmail.com’, Port => 587, User => ‘username@gmail.com’, Password => ‘password’, Hello => ‘fayland.org’, ] } ); use Email::Simple::Creator; # or other Email:: my $email = Email::Simple->create( header => [ From… Read More »

Perl, character encoding and the WWW

For most applications, UTF-8 is a good choice, since you can code arbitrary Unicode codepoints with it. On the other hand English text (and of most other European languages) is encoded very efficiently. HTTP offers the Accept-Charset-Header in which the client can tell the server which character encodings it can handle. But if you stick… Read More »

Installing Perl modules using CPAN

Installing Perl modules required by various open source software is a routine tasks for sysadmins. Installing Perl modules manually by resolving all the dependencies is  tedious and annoying process. Installing Perl modules using CPAN is a better solution, as it resolves all the dependencies automatically. In this article, let us review how to install Perl… Read More »

Returning Eval error code

eval { require “myfile.pl”; $txt.=&mysub(); }; &error_to_file(“My error: $@”) if $@; eval { require “myfile.pl”; $txt.=&mysub(); }; &error_to_file(“My error: $@”) if $@;

Handle files in Perl

Read a file and put its content into one variable $line=””; open (FILE, “<“, “$public_html_dir/upload/promotii.html”); @lines=<FILE>; foreach $l (@lines){ $line.=$l; } close(FILE); $line=””; open (FILE, “<“, “file.html”); @lines=<FILE>; foreach $l (@lines){ $line.=$l; } close(FILE); Write to a file if($xEditor ne “”){ open (FILE, “>”, “file.html”); print FILE “$xEditor”; close (FILE); }

Using FCKEditor in Perl

If you want to use FCK Editor in Perl you must insert the following lines: require “$serverfck/fckeditor.pl”; &FCKeditor(‘xEditor’); $BasePath=$fck_s_path; $Value=”$line”; $Width = $FckWidth; $Height = $FckHeight; &Create(); require “$serverfck/fckeditor.pl”; &FCKeditor(‘xEditor’); $BasePath=$fck_s_path; $Value=”$line”; $Width = $FckWidth; $Height = $FckHeight; &Create();

Trim whitespaces with Perl

If you want to remove white spaces with Perl, you can use the following Reggular Expressions Trim all whitespaces: $value=~s/s*//g; # remove all the whitespace Or you can use function to trim the whitespaces from beggining, ending or both: # Perl trim function to remove whitespace from the start and end of the string sub… Read More »

Change a csv file

If you want to use a huge csv file, an easy way to do this is to parse it and write another file. Bellow is an example, which takes the values from old_file.csv, do some changes and write another file new_file.csv #! /usr/bin/perl ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime(); $year=1900+$yearOffset;… Read More »

Modify a file from browser

#! /usr/bin/perl use CGI qw/:standard/; print “Content-type: text/htmlnn”; print “Modify the file:$file<br/><br/>”; $action = param(‘action’); if($action eq “”){ $file_content=””; open (MYFILE, “<“, $file); @lines=<MYFILE>; foreach $l(@lines){ $file_content.=$l; } close (MYFILE); print<<frm; <form action=”modify.cgi” method=”POST”> <textarea name=”file” cols=”150″ rows=”30″> $file_content </textarea> <input type=”hidden” id=”action” name=”action” value=”modify”> <br /><br /> <input type=”submit” value=”Modify!”> </form> frm } if($action… Read More »