PerlCookBook - henk52/knowledgesharing GitHub Wiki

Table of Contents

Introduction

Purpose

References

Cookbook

perl stuff

Pattern Extraction

See: https://stackoverflow.com/questions/1661217/how-can-i-extract-the-matches-from-the-perl-match-operator-into-variables

my $nEpoch = 0;

if ( $szTimeStampLine =~ m/.*?UTC seconds: (\d+)/ ) {
   $nEpoch = $1;
}

Modules

findbin

use FindBin;
push( @INC, "$FindBin::RealBin", "$FindBin::RealBin/Linux" );    ## Path to local modules

system("$FindBin::RealBin/support_script.pl");

create temporary file, that will be deleted when the script ends

Get short cli parameters

See: http://perldoc.perl.org/Getopt/Std.html

use Getopt::Std;
my %hOptions;
getopts('oif:', \%hOptions);  # Values in %hOptions.

Get long command line options

http://perldoc.perl.org/Getopt/Long.html

Provide a call tree when dieing

http://perldoc.perl.org/Carp.html

Structure operations

Array operations

Insert element in the middle of an array

delete element from the middle of the array

Getting length of referenced array

File operation

XML

XML::Twig

#!/usr/bin/perl -w
use strict;
use XML::Twig;
my $twig = XML::Twig->new;
$twig->parsefile('/tmp/v2vlibvirt1f5bf4.xml.org');

my ($eth1) =  $twig->findnodes('/domain/devices/interface/source[@network]');
$eth1->set_att(network => "default");

my ($eth2) =  $twig->findnodes('/domain/devices/interface/source[@network = "Ethernet 2"]');
$eth2->set_att(network => "j434_cont1");

for my $number ( $twig->findnodes('/domain/devices/disk[@device = "floppy"]') ) {
  $number->delete;
}

# Hopefully this is the second cdrom entry.
for my $number ( $twig->findnodes('/domain/devices/disk/target[@dev = "hdb"]') ) {
  $number->parent->delete;
}

$twig->set_pretty_print('indented_c');
$twig->print;

Text::Template

See: http://search.cpan.org/~mjd/Text-Template-1.46/lib/Text/Template.pm

use Text::Template;

SQLite

  • # $szHeadline would be 'description' then the HEADLINE content would be the same as the DESCRIPTION content.
  • my $stmt = qq(UPDATE SERIES set HEADLINE = '$szHeadline', DESCRIPTION = '$szDescription' where ID=1;);

GUI

Tk

Status Bar

PerlCD3.0/tk/ch05_01.htm

Single line Entry Fields

Multi line Entry Fields

  • Text You get and set data through the return reference you get when creating the 'Text' field.

Pull down menu

The '-variable' If set to one of the names in the list, will ensure that, that name is highlighted/selected.

You can provide the options as either an array of scalar or an array of hashes.

  • If the option is a scalar, then both the -variable and @_ will be the option text.
  • If the option is a hash, then both the -variable and @_ will be the option value(referenced in hash).
my $palette="Magenta";
my @colors = qw/Black red4 DarkGreen NavyBlue gray75 Red Green Blue
  gray50 Yellow Cyan Magenta White Brown DarkSeaGreen DarkViolet/;

my $om = $window->Optionmenu(
      -variable => \$palette,
      -options  => [@colors],
      -command  => [sub {print "args=@_  variable=$palette\n"}],
      );
$om->pack;
#$om->addOptions(['chlorophyll' => '#8395ffff0000']);

Geometry Managers

  • Only use one geometry manager within the same area, otherwise you can cause infinite re-calculation loops.

Grid Geometry manager

  • Each call to grid will create another row in the window.
    • grid($widget1, $widget2, $widget3);
    • $widget1->grid($widget2, $widget3);
  • Special characters:
    • "-" : span, previous to this coloumn.
    • "x" : leave this cell empty.
    • "^" : Span previous row into this one.

Web

CGI with lighttpd

Architecture patterns

Testing

Test::More

Test::Exception

Troubleshooting

Invalid resource type file_line

Error: Evaluation Error: Error while evaluating a Resource Statement, Invalid resource type file_line at /etc/puppet/modules/logstash/elk.pp:34:1 on node localhost
⚠️ **GitHub.com Fallback** ⚠️