Perl

PERL – Length

PERL – Length [perl] $RESULT = length(EXPRESSION); [/perl] Perl’s length() function simply returns the length of a perl string in characters [perl] $myString = ‘perl.about.com’; $myLength = length($myString); [/perl] In this example, we’ve created a string with the value perl.about.com. When we run the length function on the string, it will return 14, or the […]

Perl

Perl Push Array

Perl Push Array A slightly more interesting kind of variable is the array variable which is a list of scalars (ie numbers and strings). Array variables have the same format as scalar variables except that they are prefixed by an @ symbol. The statement @food = (“apples”, “pears”, “eels”); @music = (“whistle”, “flute”); assigns a […]

Perl

Pass Parameters to Perl Script

Pass Parameters to Perl Script Perl command line arguments stored in the special array called @ARGV. ARGV example Use $ARGV[n] to display argument. Use $#ARGV to get total number of passed argument to a perl script. For example, if your scriptname is foo.pl and you called script as follows: ./foo.pl one two three You can […]

Perl

Perl Split

Perl Split A very useful function in Perl is split, which splits up a string and places it into an array. The function uses a regular expression and as usual works on the $_ variable unless otherwise specified. The split function is used like this: [perl] $info = “Caine:Michael:Actor:14, Leafy Drive”; @personal = split(/:/, $info); […]

Perl UNIX

CPAN – Perl Command Line

CPAN – Perl Command Line There are several ways to get Perl modules from CPAN installed on your unix-based system. Keep in mind that there is always more than one way to do it with Perl, and this is no different. Before embarking upon any installation, it’s a good idea to download the module, unzip […]

Perl

Perl – Execute Command With Timeout

Perl – Execute Command With Timeout [perl] my $command_to_check = “$SSH_CON $host $SSH_CMD”; if (! execute_command($command_to_check)) { next; } sub execute_command($) { ### Executes a command with timeout ### Returns 0 if fails ### Returns 1 on success my $command=shift; my $timeout=30; eval { local $SIG{ALRM} = sub{die;}; my($oalarm) = alarm($timeout); $result=`$command 2>&1`; if ($? […]

Perl UNIX

Date/Time in a Timestamp format – Perl

Date/Time in a Timestamp format – Perl [perl] use Date::Manip; #### – Get DATE / TIME in correct format – ########## my $day= strftime “%d”, localtime; my $month= strftime “%m”, localtime; my $year = strftime “%Y”, localtime; my $now = localtime(time()); #################################################### $form_date_start = “$year/$month/$day 06:00:00”; [/perl]

Perl

Flush perl‚ Äôs print buffer – $|=1

Flush perl‚Äôs print buffer – $|=1 If you put a print statement inside of a loop that runs really really quickly, you won‚Äôt see the output of your print statement until the program terminates. The solution to this problem is to ‚Äúflush‚Äù the output buffer after each print statement. Where $| is non zero, it […]

Perl PHP

GET or POST – Secure

GET or POST – Secure GET is NOT Secure GET is not a secure method of sending data. Don’t use it for forms that send password info, credit card data or other sensitive information. Since the data is passed through as part of the URL, it’ll show up in the web server’s logfile (complete with […]