Home Scripting Perl Tutorial References
Main Menu
Home
System Verilog
Verilog
Methodologies
Open Vera
Digital Concepts
Verification Basics
Protocols
Scripting
    - Perl Tutorial
        -- Perl Definition
        -- Syntax & Variables
        -- strings
        -- Arrays
        -- Associative Arrays
        -- If & While Loop
        -- File Input
        -- Print Output
        -- Strings and Regular Expressions
        -- Subroutines
        -- Running External Programs
        -- References
        -- Terse Perl
    - Bash
    - Makefile Tutorial
    - Perl Examples
    - Bash Examples
Articles
Videos
Interview Questions
Computer Architechture
C and C++
Blog/Article
AsicGuru Blog
Tags Cloud
Ads
Usefull Sites
Know Your IP/Location
Local Information India
Buy Car/Inverter Batteries
Real Estate India
Sports Accessories India
Perl : References
Share This Articale:

References
I'm happiest writing Perl code that does not use references because they always give me a mild headache. Here's the short version of how they work. The backslash operator (\) computes a reference to something. The reference is a scalar that points to the original thing. The '$' dereferences to access the original thing.

Suppose there is a string...


$str = "hello";        ## original string


And there is a reference that points to that string...


$ref = \$str;          ## compute $ref that points to $str


The expression to access $str is $$ref. Essentially, the alphabetic part of the variable, 'str', is replaced with the dereference expression '$ref'...


print "$$ref\n"; ## prints "hello" -- identical to "$str\n";


Here's an example of the same principle with a reference to an array...


@a = (1, 2, 3);        ## original array
$aRef = \@a;           ## reference to the array
print "a: @a\n";                 ## prints "a: 1 2 3"
print "a: @$aRef\n";             ## exactly the same


Curly braces { } can be added in code and in strings to help clarify the stack of @, $, ...


print "a: @{$aRef}\n";           ## use { } for clarity


Here's how you put references to arrays in another array to make it look two dimensional...

@a = (1, 2, 3);
@b = (4, 5, 6);
@root = (\@a, \@b);
print "a: @a\n";                       ## a: (1 2 3)
print "a: @{$root[0]}\n";              ## a: (1 2 3)
print "b: @{$root[1]}\n";              ## b: (4 5 6)
scalar(@root)                  ## root len == 2
scalar(@{$root[0]})            ## a len: == 3


For arrays of arrays, the [ ] operations can stack together so the syntax is more C like...
$root[1][0]                    ## this is 4

This Articles is written/submitted by puneet (Puneet Aggarwal). You can also contribute to Asicguru.com. Click here to start


Prev << Running External Programs

Next >> Terse Perl

 
Login/Register
Register
Sign In
Login with :-
gmail-yahoo-twitter-facebook
| | |  
  •  
  • Bookmark
    ADS