SlideShare a Scribd company logo
1 of 13
Download to read offline
Perl Programming
                 Course
            References and nested
               data structures



Krassimir Berov

I-can.eu
Contents

1. What are references?
2. Reference types
3. Creating references
4. Using references
5. Reference interpolation
What are references?
• A piece of data that tells us the location
  of another piece of data
• A reference is a scalar value that refers to
  an array or a hash
  (or to just about anything else).
• References are the way to implement
  complicated data structures in Perl
• References are like pointers that know
  what they point to
Reference types
• Builtin types include:
   SCALAR
   ARRAY
   HASH
   CODE
   REF
   GLOB
   LVALUE
   FORMAT
   IO
   VSTRING
   Regexp
Reference types
• Most used:
     SCALAR    –   reference   to   scalar
     ARRAY     –   reference   to   array
     HASH      –   reference   to   hash
     CODE      –   reference   to   anonymous sub
Creating references
• Just two ways to make a reference
  1. By putting a  in front of a variable
 $aref = @array;    # $aref - reference to @array
 $href = %hash;     # $href - reference to %hash
 $sref = $scalar;   # $sref - reference to $scalar


  2. [ITEMS] makes a new, anonymous array,
     {ITEMS} makes a new, anonymous hash
 $aref = [ 1, "foo", undef, 13 ];
 # $aref now holds a reference to an array

 $href = { APR => 4, AUG => 8 };
 # $href now holds a reference to a hash
Using references
• Just two ways to use references
  1. Use curly braces preceded with the sigil
    of the variable you refer to
 @{$aref};               #   An array
 reverse @{$aref};       #   Reverse the array
 ${$aref}[3];            #   An element of the array
 ${$aref}[3] = 17;       #   Assigning an element

 %{$href};               #   A hash
 keys %{$href};          #   Get the keys from the hash
 ${$href}{'red'}         #   An element of the hash
 ${$href}{'red'} = 17;   #   Assigning an element
Using references
                                                             (2)
• Just two ways to use references
  2. Use the arrow operator to access
    elements
     ●   In between two subscripts, the arrow is optional.
 @$aref;              # An array
 reverse @$aref;      # Reverse the array
 $aref->[3];          # An element of the array
 $aref->[3] = 17;     # Assigning an element
 $aref->[0]->[2] can be written as $aref->[0][2]
 %$href;              # A hash
 keys %$href;         # Get the keys from the hash
 $href->{'red'}       # An element of the hash
 $href->{'red'} = 17; # Assigning an element
 $href->{'green'}->{leaf}
     can be written as $href->{'green'}{leaf}
Using references
• Example:
 my $self = {
      name =>'Krassi',
      family_name => 'Berov',
      children =>[qw|Maria Pavel Viktoria|],
    favorite_things =>{
    }
 };
 print "Hello! I am $self->{name} $self->{family_name}"'
 print 'I have '. scalar @{$self->{children}}
       .' children.';
 print 'They are named:'
         . map {$_ . "n"} @{$self->{children}};

 #see using.pl for more
Using references
• Example:
 my $self = {
      name =>'Krassi',
      family_name => 'Berov',
      children =>[qw|Maria Pavel Viktoria|],
    favorite_things =>{
    }
 };
 print "Hello! I am $self->{name} $self->{family_name}"'
 print 'I have '. scalar @{$self->{children}}
       .' children.';
 print 'They are named:'
         . map {$_ . "n"} @{$self->{children}};

 #see using.pl for more
Reference interpolation
• Example:
 my @pets = qw|Goldy Amelia Jako|;
 my $self = {
     name =>'Krassi',
     family_name => 'Berov',
     can => sub { return shift },
     pets => @pets,
     children =>[qw|Maria Pavel Victoria|],
 };

 print <<TXT;
  Hello!
      I am $self->{name} $self->{family_name}.
      I can ${$self->{can}('talk')}.
      My first child is $self->{children}[0].
      My last child is $self->{children}[-1].
      I do not have a pet named $self->{pets}[1].
 TXT
References
              and nested data structures
• Resources
  • perlreftut
  • perllol
  • perldsc
  • perlref
  • Beginning Perl (Chapter 7 – References)
References
and nested data structures




Questions?

More Related Content

Viewers also liked

Modern Perl for the Unfrozen Paleolithic Perl Programmer
Modern Perl for the Unfrozen Paleolithic Perl ProgrammerModern Perl for the Unfrozen Paleolithic Perl Programmer
Modern Perl for the Unfrozen Paleolithic Perl ProgrammerJohn Anderson
 
Things I Learned From Having Users
Things I Learned From Having UsersThings I Learned From Having Users
Things I Learned From Having UsersDave Cross
 
Moo the universe and everything
Moo the universe and everythingMoo the universe and everything
Moo the universe and everythingHenry Van Styn
 
Future of PERL in IT
Future of PERL in ITFuture of PERL in IT
Future of PERL in ITNexiilabs
 
Modern Perl Catch-Up
Modern Perl Catch-UpModern Perl Catch-Up
Modern Perl Catch-UpDave Cross
 

Viewers also liked (6)

Modern Perl for the Unfrozen Paleolithic Perl Programmer
Modern Perl for the Unfrozen Paleolithic Perl ProgrammerModern Perl for the Unfrozen Paleolithic Perl Programmer
Modern Perl for the Unfrozen Paleolithic Perl Programmer
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Things I Learned From Having Users
Things I Learned From Having UsersThings I Learned From Having Users
Things I Learned From Having Users
 
Moo the universe and everything
Moo the universe and everythingMoo the universe and everything
Moo the universe and everything
 
Future of PERL in IT
Future of PERL in ITFuture of PERL in IT
Future of PERL in IT
 
Modern Perl Catch-Up
Modern Perl Catch-UpModern Perl Catch-Up
Modern Perl Catch-Up
 

More from Krasimir Berov (Красимир Беров) (8)

Хешове
ХешовеХешове
Хешове
 
Списъци и масиви
Списъци и масивиСписъци и масиви
Списъци и масиви
 
Network programming
Network programmingNetwork programming
Network programming
 
Processes and threads
Processes and threadsProcesses and threads
Processes and threads
 
Working with text, Regular expressions
Working with text, Regular expressionsWorking with text, Regular expressions
Working with text, Regular expressions
 
Subroutines
SubroutinesSubroutines
Subroutines
 
IO Streams, Files and Directories
IO Streams, Files and DirectoriesIO Streams, Files and Directories
IO Streams, Files and Directories
 
Syntax
SyntaxSyntax
Syntax
 

Recently uploaded

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 

Recently uploaded (20)

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 

References and nested data structures

  • 1. Perl Programming Course References and nested data structures Krassimir Berov I-can.eu
  • 2. Contents 1. What are references? 2. Reference types 3. Creating references 4. Using references 5. Reference interpolation
  • 3. What are references? • A piece of data that tells us the location of another piece of data • A reference is a scalar value that refers to an array or a hash (or to just about anything else). • References are the way to implement complicated data structures in Perl • References are like pointers that know what they point to
  • 4. Reference types • Builtin types include: SCALAR ARRAY HASH CODE REF GLOB LVALUE FORMAT IO VSTRING Regexp
  • 5. Reference types • Most used: SCALAR – reference to scalar ARRAY – reference to array HASH – reference to hash CODE – reference to anonymous sub
  • 6. Creating references • Just two ways to make a reference 1. By putting a in front of a variable $aref = @array; # $aref - reference to @array $href = %hash; # $href - reference to %hash $sref = $scalar; # $sref - reference to $scalar 2. [ITEMS] makes a new, anonymous array, {ITEMS} makes a new, anonymous hash $aref = [ 1, "foo", undef, 13 ]; # $aref now holds a reference to an array $href = { APR => 4, AUG => 8 }; # $href now holds a reference to a hash
  • 7. Using references • Just two ways to use references 1. Use curly braces preceded with the sigil of the variable you refer to @{$aref}; # An array reverse @{$aref}; # Reverse the array ${$aref}[3]; # An element of the array ${$aref}[3] = 17; # Assigning an element %{$href}; # A hash keys %{$href}; # Get the keys from the hash ${$href}{'red'} # An element of the hash ${$href}{'red'} = 17; # Assigning an element
  • 8. Using references (2) • Just two ways to use references 2. Use the arrow operator to access elements ● In between two subscripts, the arrow is optional. @$aref; # An array reverse @$aref; # Reverse the array $aref->[3]; # An element of the array $aref->[3] = 17; # Assigning an element $aref->[0]->[2] can be written as $aref->[0][2] %$href; # A hash keys %$href; # Get the keys from the hash $href->{'red'} # An element of the hash $href->{'red'} = 17; # Assigning an element $href->{'green'}->{leaf} can be written as $href->{'green'}{leaf}
  • 9. Using references • Example: my $self = { name =>'Krassi', family_name => 'Berov', children =>[qw|Maria Pavel Viktoria|], favorite_things =>{ } }; print "Hello! I am $self->{name} $self->{family_name}"' print 'I have '. scalar @{$self->{children}} .' children.'; print 'They are named:' . map {$_ . "n"} @{$self->{children}}; #see using.pl for more
  • 10. Using references • Example: my $self = { name =>'Krassi', family_name => 'Berov', children =>[qw|Maria Pavel Viktoria|], favorite_things =>{ } }; print "Hello! I am $self->{name} $self->{family_name}"' print 'I have '. scalar @{$self->{children}} .' children.'; print 'They are named:' . map {$_ . "n"} @{$self->{children}}; #see using.pl for more
  • 11. Reference interpolation • Example: my @pets = qw|Goldy Amelia Jako|; my $self = { name =>'Krassi', family_name => 'Berov', can => sub { return shift }, pets => @pets, children =>[qw|Maria Pavel Victoria|], }; print <<TXT; Hello! I am $self->{name} $self->{family_name}. I can ${$self->{can}('talk')}. My first child is $self->{children}[0]. My last child is $self->{children}[-1]. I do not have a pet named $self->{pets}[1]. TXT
  • 12. References and nested data structures • Resources • perlreftut • perllol • perldsc • perlref • Beginning Perl (Chapter 7 – References)
  • 13. References and nested data structures Questions?