Top Banner
October 22 2007 Perl From Ground level and Up Lecture 1 By Shmuel Fomberg
14

Perl from the ground up: variables and data types

Jun 22, 2015

Download

Education

Shmuel Fomberg

a basic introduction to variable types in Perl
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Perl from the ground up: variables and data types

October 22 2007

PerlFrom Ground level and Up

Lecture 1

By Shmuel Fomberg

Page 2: Perl from the ground up: variables and data types

2

Perl – From The Ground and UP

October 22 2007Shmuel Fomberg

Data Types

Scalar Array Hash IO CODE GLOB

Page 3: Perl from the ground up: variables and data types

3

Perl – From The Ground and UP

October 22 2007Shmuel Fomberg

Data Types

Scalar Number (integer, floating) String Number and String Reference to anything. (except IO) undef (actually, a null reference) False – undef, empty string, zero or string that

evaluate to zero

Page 4: Perl from the ground up: variables and data types

4

Perl – From The Ground and UP

October 22 2007Shmuel Fomberg

Data Types

Array Contain only scalars Can be undef (obsolete) Has length

scalar(@array) $#array

Can ask if exists $array[2] Can get slices @a2 = @array[3,5] False – only empty/undefined array.

Page 5: Perl from the ground up: variables and data types

5

Perl – From The Ground and UP

October 22 2007Shmuel Fomberg

Data Types

Hash Keys – only strings Values – scalars Can ask if exists $hash{key} Can delete $hash{key} Ops: keys, values, each Can be undef (obsolete) Can get slices @a2 = @hash{“key1”, “key2”} False – only empty/undefined hash.

Page 6: Perl from the ground up: variables and data types

6

Perl – From The Ground and UP

October 22 2007Shmuel Fomberg

Data Types

Code All the functions in the script are actually Code

variables Can have anonymous functions

my $code = sub {….}

Can ask if exists/defined &function Can run the function

Page 7: Perl from the ground up: variables and data types

7

Perl – From The Ground and UP

October 22 2007Shmuel Fomberg

Data Types

IO files handlers, sockets open FH, “<“, “filename”; Global Can not pass to functions Can not use in recursion open $fh, “<“, “filename”;

What’s that?

Page 8: Perl from the ground up: variables and data types

8

Perl – From The Ground and UP

October 22 2007Shmuel Fomberg

Data Types

GLOB A “hash” Contains only one scalar, array, hash, code, IO Globals are stored using GLOBs open $fh, “<“, “filename”;

Now $fh contain a reference to anonymous glob

Page 9: Perl from the ground up: variables and data types

9

Perl – From The Ground and UP

October 22 2007Shmuel Fomberg

Packages

Main package is called “main”. or “::”

Declaring the name of the current package:package MyPkg;package MyPkg::Private;

Declaring a package-global with “our”our $id;

Accessing the global from elsewhere:$MyPkg::id = 5;

Page 10: Perl from the ground up: variables and data types

10

Perl – From The Ground and UP

October 22 2007Shmuel Fomberg

Packages

main

MyPkg

Private

MyPkg

Private

id

GLOB

SCALAR 5

Page 11: Perl from the ground up: variables and data types

11

Perl – From The Ground and UP

October 22 2007Shmuel Fomberg

Packages

More on Package-Globals All the Functions Not searched by default (for variables)

More on GLOBs A way to declare constant

*C = \5

A way to push a function *func = sub {….}

Page 12: Perl from the ground up: variables and data types

12

Perl – From The Ground and UP

October 22 2007Shmuel Fomberg

Lexicals

When declaring variable with “my” my $x = 5;

Valid only in the current block When outside of any block – to the current file

Can not be accessed from “outside” Possible, but very difficult

Does not use globs Way faster

Page 13: Perl from the ground up: variables and data types

13

Perl – From The Ground and UP

October 22 2007Shmuel Fomberg

Closure

Every piece of code remembers where it was defined

my $offset = 5;

sub get_acc {

my $acc = 1;

return sub { return $offset + $acc++; };

}

Page 14: Perl from the ground up: variables and data types

14

Perl – From The Ground and UP

October 22 2007Shmuel Fomberg

Local Globals

Temporarily change the value of global (our) variable

Effective until the end of the block. After that, the original value is restoredopen my $fh, "foo" or die $!;local $/; # enable localized slurp modemy $content = <$fh>;close $fh;

{ local *blaY= sub {return 1;}; print defined(&blaY) ? "" : "Not ", "Exists\n";}print defined(&blaY) ? "" : "Not ", "Exists\n";