Perl from the ground up: variables and data types
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Perl from the ground up: variables and data types

on

  • 883 views

a basic introduction to variable types in Perl

a basic introduction to variable types in Perl

Statistics

Views

Total Views
883
Views on SlideShare
883
Embed Views
0

Actions

Likes
0
Downloads
3
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Perl from the ground up: variables and data types Presentation Transcript

  • 1. Perl From Ground level and Up Lecture 1 October 22 2007 By Shmuel Fomberg
  • 2. Data Types
    • Scalar
    • Array
    • Hash
    • IO
    • CODE
    • GLOB
    October 22 2007 Shmuel Fomberg
  • 3. 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
    October 22 2007 Shmuel Fomberg
  • 4. 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.
    October 22 2007 Shmuel Fomberg
  • 5. 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.
    October 22 2007 Shmuel Fomberg
  • 6. 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
    October 22 2007 Shmuel Fomberg
  • 7. 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?
    October 22 2007 Shmuel Fomberg
  • 8. 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
    October 22 2007 Shmuel Fomberg
  • 9. 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;
    October 22 2007 Shmuel Fomberg
  • 10. Packages October 22 2007 Shmuel Fomberg main MyPkg Private MyPkg Private id GLOB SCALAR 5
  • 11. 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 { … .}
    October 22 2007 Shmuel Fomberg
  • 12. 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
    October 22 2007 Shmuel Fomberg
  • 13. Closure
    • Every piece of code remembers where it was defined
    October 22 2007 Shmuel Fomberg my $offset = 5; sub get_acc { my $acc = 1; return sub { return $offset + $acc++; }; }
  • 14. Local Globals
    • Temporarily change the value of global (our) variable
    • Effective until the end of the block. After that, the original value is restored
    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;;