Perl From Ground level and Up Lecture 1 October 22 2007 By Shmuel Fomberg
Data Types <ul><li>Scalar </li></ul><ul><li>Array </li></ul><ul><li>Hash </li></ul><ul><li>IO </li></ul><ul><li>CODE </li>...
Data Types <ul><li>Scalar </li></ul><ul><ul><li>Number (integer, floating) </li></ul></ul><ul><ul><li>String </li></ul></u...
Data Types <ul><li>Array </li></ul><ul><ul><li>Contain only scalars </li></ul></ul><ul><ul><li>Can be undef (obsolete) </l...
Data Types <ul><li>Hash </li></ul><ul><ul><li>Keys  –  only strings </li></ul></ul><ul><ul><li>Values  –  scalars </li></u...
Data Types <ul><li>Code </li></ul><ul><ul><li>All the functions in the script are actually Code variables </li></ul></ul><...
Data Types <ul><li>IO </li></ul><ul><ul><li>files handlers, sockets </li></ul></ul><ul><ul><li>open FH,  “ < “ ,  “ filena...
Data Types <ul><li>GLOB </li></ul><ul><ul><li>A  “ hash ” </li></ul></ul><ul><ul><li>Contains only one scalar, array, hash...
Packages <ul><li>Main package is called  “ main ” . </li></ul><ul><ul><li>or  “ :: ” </li></ul></ul><ul><li>Declaring the ...
Packages October 22 2007 Shmuel Fomberg main MyPkg Private MyPkg Private id GLOB SCALAR 5
Packages <ul><li>More on Package-Globals </li></ul><ul><ul><li>All the Functions </li></ul></ul><ul><ul><li>Not searched b...
Lexicals <ul><li>When declaring variable with  “ my ” </li></ul><ul><ul><li>my $x = 5; </li></ul></ul><ul><li>Valid only i...
Closure <ul><li>Every piece of code remembers where it was defined </li></ul>October 22 2007 Shmuel Fomberg my $offset = 5...
Local Globals <ul><li>Temporarily change the value of global (our) variable </li></ul><ul><li>Effective until the end of t...
Upcoming SlideShare
Loading in...5
×

Perl from the ground up: variables and data types

721

Published on

a basic introduction to variable types in Perl

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
721
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Perl from the ground up: variables and data types

  1. 1. Perl From Ground level and Up Lecture 1 October 22 2007 By Shmuel Fomberg
  2. 2. Data Types <ul><li>Scalar </li></ul><ul><li>Array </li></ul><ul><li>Hash </li></ul><ul><li>IO </li></ul><ul><li>CODE </li></ul><ul><li>GLOB </li></ul>October 22 2007 Shmuel Fomberg
  3. 3. Data Types <ul><li>Scalar </li></ul><ul><ul><li>Number (integer, floating) </li></ul></ul><ul><ul><li>String </li></ul></ul><ul><ul><li>Number and String </li></ul></ul><ul><ul><li>Reference to anything. (except IO) </li></ul></ul><ul><ul><li>undef (actually, a null reference) </li></ul></ul><ul><ul><li>False – undef, empty string, zero or string that evaluate to zero </li></ul></ul>October 22 2007 Shmuel Fomberg
  4. 4. Data Types <ul><li>Array </li></ul><ul><ul><li>Contain only scalars </li></ul></ul><ul><ul><li>Can be undef (obsolete) </li></ul></ul><ul><ul><li>Has length </li></ul></ul><ul><ul><ul><li>scalar(@array) </li></ul></ul></ul><ul><ul><ul><li>$#array </li></ul></ul></ul><ul><ul><li>Can ask if exists $array[2] </li></ul></ul><ul><ul><li>Can get slices @a2 = @array[3,5] </li></ul></ul><ul><ul><li>False – only empty/undefined array. </li></ul></ul>October 22 2007 Shmuel Fomberg
  5. 5. Data Types <ul><li>Hash </li></ul><ul><ul><li>Keys – only strings </li></ul></ul><ul><ul><li>Values – scalars </li></ul></ul><ul><ul><li>Can ask if exists $hash{key} </li></ul></ul><ul><ul><li>Can delete $hash{key} </li></ul></ul><ul><ul><li>Ops: keys, values, each </li></ul></ul><ul><ul><li>Can be undef (obsolete) </li></ul></ul><ul><ul><li>Can get slices @a2 = @hash{ “ key1 ” , “ key2 ” } </li></ul></ul><ul><ul><li>False – only empty/undefined hash. </li></ul></ul>October 22 2007 Shmuel Fomberg
  6. 6. Data Types <ul><li>Code </li></ul><ul><ul><li>All the functions in the script are actually Code variables </li></ul></ul><ul><ul><li>Can have anonymous functions </li></ul></ul><ul><ul><ul><li>my $code = sub { … .} </li></ul></ul></ul><ul><ul><li>Can ask if exists/defined &function </li></ul></ul><ul><ul><li>Can run the function </li></ul></ul>October 22 2007 Shmuel Fomberg
  7. 7. Data Types <ul><li>IO </li></ul><ul><ul><li>files handlers, sockets </li></ul></ul><ul><ul><li>open FH, “ < “ , “ filename ” ; </li></ul></ul><ul><ul><li>Global </li></ul></ul><ul><ul><li>Can not pass to functions </li></ul></ul><ul><ul><li>Can not use in recursion </li></ul></ul><ul><ul><li>open $fh, “ < “ , “ filename ” ; </li></ul></ul><ul><ul><ul><li>What ’ s that? </li></ul></ul></ul>October 22 2007 Shmuel Fomberg
  8. 8. Data Types <ul><li>GLOB </li></ul><ul><ul><li>A “ hash ” </li></ul></ul><ul><ul><li>Contains only one scalar, array, hash, code, IO </li></ul></ul><ul><ul><li>Globals are stored using GLOBs </li></ul></ul><ul><ul><li>open $fh, “ < “ , “ filename ” ; </li></ul></ul><ul><ul><ul><li>Now $fh contain a reference to anonymous glob </li></ul></ul></ul>October 22 2007 Shmuel Fomberg
  9. 9. Packages <ul><li>Main package is called “ main ” . </li></ul><ul><ul><li>or “ :: ” </li></ul></ul><ul><li>Declaring the name of the current package: package MyPkg; package MyPkg::Private; </li></ul><ul><li>Declaring a package-global with “ our ” our $id; </li></ul><ul><li>Accessing the global from elsewhere: $MyPkg::id = 5; </li></ul>October 22 2007 Shmuel Fomberg
  10. 10. Packages October 22 2007 Shmuel Fomberg main MyPkg Private MyPkg Private id GLOB SCALAR 5
  11. 11. Packages <ul><li>More on Package-Globals </li></ul><ul><ul><li>All the Functions </li></ul></ul><ul><ul><li>Not searched by default (for variables) </li></ul></ul><ul><li>More on GLOBs </li></ul><ul><ul><li>A way to declare constant </li></ul></ul><ul><ul><ul><li>*C = 5 </li></ul></ul></ul><ul><ul><li>A way to push a function </li></ul></ul><ul><ul><ul><li>*func = sub { … .} </li></ul></ul></ul>October 22 2007 Shmuel Fomberg
  12. 12. Lexicals <ul><li>When declaring variable with “ my ” </li></ul><ul><ul><li>my $x = 5; </li></ul></ul><ul><li>Valid only in the current block </li></ul><ul><ul><li>When outside of any block – to the current file </li></ul></ul><ul><li>Can not be accessed from “ outside ” </li></ul><ul><ul><li>Possible, but very difficult </li></ul></ul><ul><li>Does not use globs </li></ul><ul><ul><li>Way faster </li></ul></ul>October 22 2007 Shmuel Fomberg
  13. 13. Closure <ul><li>Every piece of code remembers where it was defined </li></ul>October 22 2007 Shmuel Fomberg my $offset = 5; sub get_acc { my $acc = 1; return sub { return $offset + $acc++; }; }
  14. 14. Local Globals <ul><li>Temporarily change the value of global (our) variable </li></ul><ul><li>Effective until the end of the block. After that, the original value is restored </li></ul>October 22 2007 Shmuel Fomberg open my $fh, &quot;foo&quot; or die $!; local $/; # enable localized slurp mode my $content = <$fh>; close $fh; { local *blaY= sub {return 1;}; print defined(&blaY) ? &quot;&quot; : &quot;Not &quot;, &quot;Exists &quot;; } print defined(&blaY) ? &quot;&quot; : &quot;Not &quot;, &quot;Exists &quot;;
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×