Perl Refernces. Kinds of references: hard: a scalar variable that points to data symbolic: a variable that names another reference typeglob: a kind of.

Slides:



Advertisements
Similar presentations
1/12 Steven Leung Very Basic Perl Tricks A Few Ground Rules File I/O and Formatting Operators, Flow Control Statements Regular Expression Subroutines Hash.
Advertisements

C Language.
Advanced Perl WUC Perl Poetry Variable Scope.
A Guide to Unix Using Linux Fourth Edition
Perl for biologists A. Emerson, Perl for Biologists PERL More references, complex data types and objects.
Kernighan/Ritchie: Kelley/Pohl:
References and Data Structures. References Just as in C, you can create a variable that is a reference (or pointer) to another variable. That is, it contains.
General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
8.1 References and complex data structures. 8.2 An associative array (or simply – a hash) is an unordered set of key=>value pairs. Each key is associated.
Pointers and Arrays C and Data Structures Baojian Hua
Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
10.1 Variable types in PERL ScalarArrayHash $number $string %hash => $array[0] $hash{key}
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
8ex.1 References and complex data structures. 8ex.2 An associative array (or simply – a hash) is an unordered set of key=>value pairs. Each key is associated.
I/O while ($line= ){ #remove new line char \n chomp($line); if($line eq “quit”){ exit(1); } while ( ){ #remove new line char \n chomp($_); if($_ eq “quit”){
Guide To UNIX Using Linux Third Edition
Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
Subroutines. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc We’ll just say Subroutines. –“Functions” generally means built-in.
Computer Science 210 Computer Organization Pointers.
CSE S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar.
Some Example C Programs. These programs show how to use the exec function.
Lecture 8: Basic concepts of subroutines. Functions In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements Return.
13r.1 Revision (Q&A). 13r.2 $scalar 13r.3 Multiple assignment my ($a,$b) = ('cow','dog'); = = (6,7,8,9,10);
subroutines and references
– Intermediate Perl 9/16/ Intermediate Perl - References Intermediate Perl Session 1 · references · complex data structres.
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
POINTERS. 1.a) POINTER EXPRESSIONS Pointer variables can be used in expression If p1 and p2 are properly declared and initialized pointers then following.
– Intermediate Perl 10/23/ Intermediate Perl - References Intermediate Perl Session 2 · references, continued · function.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Sed, awk, & perl CS 2204 Class meeting 13 *Notes by Mir Farooq Ali and other members of the CS faculty at Virginia Tech. Copyright 2003.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Perl Language Yize Chen CS354. History Perl was designed by Larry Wall in 1987 as a text processing language Perl has revised several times and becomes.
3 1 Sending Data Using an Online Form CGI/Perl Programming By Diane Zak.
Programming in Perl predefined variables Peter Verhás January 2002.
References. Analagous (somewhat) to pointers in C/C++ –Far less messy, and definitely less dangerous –Variables that "refer" to another variable You can.
Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Llama to Ram How to think like a perl weenie Rob Napier 3/18/99.
Perl Basics. sh-bang !!!! Every perl program starts with a sh-bang line #!/usr/bin/perl # hello.pl printf “Hello, world!\n”; printf STDOUT “Hello, world!\n”;
Perl Chapter 5 Hashes. Outside of world of Perl, know as associative arrays Also called hash tables Perl one of few languages that has hashes built-in.
1 More Perl Strings References Complex data structures –Multidimensional arrays Subprograms Perl OOP –Methods –Constructors and Instances –Inheritance.
Perl Chapter 6 Functions. Subprograms In Perl, all subprograms are functions – returns 0 or 1 value – although may have “side-effects” optional function.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Chapter Twelve sed, awk & perl1 System Programming sed, awk & perl.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Introduction to Perl NICOLE VECERE. Background General Purpose Language ◦ Procedural, Functional, and Object-oriented Developed for text manipulation.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Interpolation Variable Interpolation, Backslash Interpolation.
UNIT 8 Pointers.
BINF 634 Fall LECTURE061 Outline Lab 1 (Quiz 3) Solution Program 2 Scoping Algorithm efficiency Sorting Hashes Review for midterm Quiz 4 Outline.
The Scripting Programming Language
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
Perl References arrays and hashes can only contain scalars (numbers and strings)‏ if we want something more complicated (like an array of arrays) we use.
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
Programming Languages Meeting 3 September 9/10, 2014.
Programming in Perl references and objects Peter Verhás January 2002.
Perl Chapter 5 part 2 references / nested structures.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 1 Day 6: References suggested reading: perlreftut
Perl Backticks Hashes printf Variables Pragmas. Backticks.
References and Data Structures
Introduction to Perl: Part II
Intermediate Perl - References
AKA LET'S WRITE SOME FUCKING PERL by William Orr
EE 312 Exam I Review.
EE 312 Exam I Review.
EE 312 Exam I Review.
Presentation transcript:

Perl Refernces

Kinds of references: hard: a scalar variable that points to data symbolic: a variable that names another reference typeglob: a kind of symbolic reference $language = “english”; # a hard reference $english = “Brooklyn”; # another hard reference print “you speak with a ${$language} accent\n”; eval “ \$$language = ‘British’;”; # another symbolic ref Print “Now you speak with a $english accent\n”;

Symbolic references are tricky: You can disallow symbolic references using the pragma use strict “refs”;

Life revolves around hard references: A hard reference is a pointer; an address. You can point to –a scalar –a list or array –an associative array or hash –a subroutine Purpose is to allow you to create things like –array of arrays (n-dimensional arrays) –array of hashes –hash of hashes (a file hierarchy where directory == hash)

The \ Operator: Similar to & in C++. $p is a hard reference to the “a scalar” string; Like & in C++, you de-reference a field by using . $x = “a scalar”; $p = \$x; # a pointer to $x

Examples: $scalarref = \$foo; $arrayref = $hashref = \%ENV; $coderef = \&handler; $globref = \*STDOUT; $reftoref = \$scalarref;

De-referencing a pointer: The de-reference operator is the character that precedes the variable name and indicates the data type of the variable. $num = 5; $p = \$num; Printf ‘The address assigned to $p is ‘. $p. “\n”; Printf “The value stored there is $$p\n”; #output The address assigned to $p is SCALAR(0xb075c) The value stored there is 5

De-referencing another = qw/slinky yo-yo jack-in-the-box/; $num $ref1 = \$num; $ref2 = printf “There are $$ref1 toys\n”; printf “They printf “My favourite toy is $ref2->[0]\n”; # could also write this as $$ref2[0]

Anonymous variables: Variables with no names are called anonymous. Anonymous arrays are created with [ = (3, 4,5); $arrayref = [ 3, 4, = ( [1, 2, 3], [4, 5, 6], [7, 8, 9]); printf # prints a list of 3 addresses printf “matrix[0,0] = $matrix[0][0]\n’; # prints matrix[0][0] = 1 # can also be written $matrix[0]  [0] # print out the matrix using a loop for ($i = 0; $i < 3; $i++) { printf }

Purpose of  The  operator, called the arrow or infix operator, is used to de-reference an anonymous array or hash. $matrix[0] is a reference to an array  de-references to get first the actual array and then [0] gets the first element in it. $matrix[0]  [0]

Anonymous hashes: Anonymous hashes are created with { } $hashref = { Name => ”Woody”, Type => “Cowboy”}; printf $hashref  {Name}. “\n”; printf keys(%$hashref). “\n”; printf values(%$hashref). “\n”; #output Woody NameType WoodyCowboy