cmdref.net - Cheat Sheet and Example

cmdref.net is command references/cheat sheets/examples for system engineers.

User Tools

Site Tools


Sidebar








Cloud



Etc


Reference














.

programming:perl:index.html



Programming Languages

Perl

References


Framework

Framework Memo
Mojolicious http://mojolicio.us/
Catalyst




Grammar

use strict;
use warnings;

print

print "Hello world";


Variables

my $num;
my @students
my %month_num;

my $num = 1;
my $num = 1.234;
my $num = 100_000_000;

$div = int(3/2);

$mod = 3 % 2;

$i++
$i--

my $str1 = 'abc';
my $str2 = "def";
my $str3 = "a\tbc\n";

my $str4 = "$str1 def"; # abc def

my $join1 = 'aaa' . 'bbb';

my $join2 = join(',', 'aaa', 'bbb', 'ccc');

my @record = split(/,/, 'aaa,bbb,ccc');


my $length = length 'abcdef';

my $substr = substr('abcd', 0, 2); # ab

my $result = index('abcd', 'cd');

Array

my @array;

@array = (1, 2, 3);

$array[0];
$array[1];

$array[0] = 1;
$array[1] = 2;

my $array_num = @array;

my $first = shift @array;

unshift @array, 5;

my $last = pop @array;

push @array, 9;

Conditionals

if

if (condition) {

}

if else

if (condition) {
    
}
else {
    
}

if elsif

if (condition1) {
    
}
elsif (condition2) {
    
}

while

my $i = 0;
while ($i < 5) {
    #
    $i++;
}

for

for (my $i = 0; $i < 5; $i++) {
  #
}

foreach

foreach my $field (@fields) {
  #
}

comparative operation

numerical value

$p == $q
$p != $q
$p < $q
$p > $q
$p <= $q
$p >= $q

character string

$s eq $t
$s ne $t
$s lt $t
$s gt $t
$s le $t
$s ge $t

subroutine

sub sum {
  my ($num1, $num2) = @_;
  
  my $total = $num1 + $num2;
  
  return $total;
}

File

open my $fh, '<', $file
  or die "Cannot open '$file': $!";

while (my $line = <$fh>) {
  #
}

close $fh;

Tips

Config

my $conf_file = "app.conf";
my $conf = do $conf_file
  or die qq/Can't load config file "$conf_file": $!$@/;

{
    name => 'Foo',
    number => 9
}




programming/perl/index.html.txt ยท Last modified: 2014/12/07 by admin

Page Tools