Perl Advanced Course

notes

  • get Eclipse Epic module
  • ask for sw repository in cern

Typeglobs

  • Access to values over $main::{name}
  • Or over *main::name

Two ways to adress variables:

  • my and local
  • gets stored in a diffrent way
  • local creates a temporary limited package based scalar, array, hash, or glob.
  • the previous value is restored
  • my variable creates a limited nonpackage-based scalar

References

Make a reference to another subroutine:

  • $d = \$prog

$m=10;
$prt=\$m;
$$ptr= 20;
print $m; gives 20

change from array to scalar:
@mat; print @{$mat[0]}
this will show the topic, not the ARRAY hash

  • goto Active Perl User Guide for more info about references

Packages and Modules

  • The symbol __PACKAGE__ will return the package name
note: use bless for inheriting from other sub function

  • my doesnt store symbol vars, use global ISA

Packages

  • require: is in INC, requires a file containing perl code and compiles it
    • required module must have a true value do get compied
    • use eval statement to check if it is existing
  • use: uses functions with import, to import the real values of the scalars in modules.
    • if import function is not available use require
    • use newer than require

note: use exprot in grouplist to import it in isinlist!!!!!!!!

Global Variables

  • use vars qw
  • or our (scalars, hashes...)

Object Oriented Perl

Classes

  • The class is no more than a package
  • puts method definitions into class
  • using subroutines

Objects

  • a class is a description of the objects of the class
  • e.g today is an instance of class date

2 Types of methods:

  • class methods:
    • call the method
  • instance methods:
    • calls the functions of a method

Variables

2 Types of Variables:
  • Class variables
  • instance variables

Constructors

a class provides one constructor. it is a reference to an associative array. it returns a blessed reference.
  • bless to transform a reference into an object

Deconstructors

with DESTROY we can free used objects for new contstucting.
PERL does this automatically.

Inheritance

To use existing classes as a base of others. needs a new constructer, even when no new vars are needed.. bless (self, class) return self @ISA, UNIVERSAL, AUTOLOAD use this for inheriting

Aggregation

Using objects and instance variables

Operator Overloading

to substitute class methods or own subroutines for standard perl operators.

package Date<
use overload `==` => "equals", (name of method)
                    `<` => \&lowerthan; (reference to method)

all overloadable operators are in %overload::ops hash

Tied Variables

tie to create seemingly normal variable that is actually a full fledged Perl object.

binds variable to classname
tie VARIABLE, CLASSNAME, LIST;

$object = tied VARIABLE;

or

$object = tie VARIABLE, CLASSNAME, LIST;

Databases and Perl

see course book

Taint mode

  • to validate input data
  • prevent traps
  • ARGV[0] & ENV{} is tainted
  • only the values in a hash can be tainted, not the keys

see page 78

untaint function

use IO::Handle;
open DATA, "data.log";
DATA->untaint();   #DATA is now untainted

open files secure

open (DATA, "<", "$file"); #safest solution

Make ENV secure

delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}

Reg EX advanced

note: page 85

REGEXP::COMMON module hass a lot of pre tested reg exps to use for

-- AlexanderBernegger - 10-Dec-2009

Edit | Attach | Watch | Print version | History: r3 < r2 < r1 | Backlinks | Raw View | WYSIWYG | More topic actions
Topic revision: r3 - 2009-12-10 - unknown
 
    • Cern Search Icon Cern Search
    • TWiki Search Icon TWiki Search
    • Google Search Icon Google Search

    Main All webs login

This site is powered by the TWiki collaboration platform Powered by PerlCopyright & 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback