Softpanorama

May the source be with you, but remember the KISS principle ;-)
Home Switchboard Unix Administration Red Hat TCP/IP Networks Neoliberalism Toxic Managers
(slightly skeptical) Educational society promoting "Back to basics" movement against IT overcomplexity and  bastardization of classic Unix

Introduction to Perl 5.10 for Unix System Administrators

(Perl 5.10 without excessive complexity)

by Dr Nikolai Bezroukov

Contents : Foreword : Ch01 : Ch02 : Ch03 : Ch04 : Ch05 : Ch06 : Ch07 : Ch08 :


Prev | Up | Contents | Down | Next

Basic Information about References

Like in C, a reference is simply a pointer to something (machine address of the particular variable of start of structure or array). Perl reference is quote similar  and it is some kind of index in some symbol table of a Perl variable, array, hash (also known as an associative array), or even a subroutine. We will use the terms pointer and reference interchangeably.

In other words a reference is simply an address of a variable. References are useful in creating complex data structures in Perl. In fact, you cannot really define any complicated structures in Perl without using references.

Like links in Unix filesystems there are two types of references in Perl 5 are hard and symbolic.

The Backslash Operator

You can create a reference to any named variable or subroutine by using the unary backslash operator. (You may also use it on an anonymous scalar value.) This works much like the & (address-of) operator in C.

Here are some examples:

$scalar_ref = \$price;
$const_ref  = \3.14;
$array_ref  = \@ARGV;
$hash_ref   = \%ENV;
$sub_ref   = \&send_message;

A reference is a scalar value that refers to a variable or entire array or an entire hash (or to just about anything else.) To create a reference you put a \ in front of a variable on the left side of the assignment statement. It means "take address instead of value". Symbol @ would be probably better but it already used for arrays.

$scalar_var = 2009; 	 
$pointer = \$scalar_var; 

In the preceding code, the variable $pointer contains the address of a variable $scalar_var, not the value itself. To get the value, you have to de-reference $pointer with two $$, for example:

printf "\n Pointer *($pointer) points to value $$pointer\n";

This notation also logically suggest that   a single dollar before the variable signifies that it is a reference to the value, not the value itself.

References are printed by Perl with the word SCALAR is followed by a long hexadecimal number

Once the reference is stored in a variable like $pointer , you can copy it to other valuable with a scalar value:

$ref = $pointer;     # $xy now holds a reference to variable 
$p[3] = $ref;        # $p[3] now holds a reference to $scalar_var
$new_ref = $p[3];    # $new_ref now holds a reference to $scalar_var

With any complex language construct you can get 80% of usage in 20% of space and the other 20% of usage in 80% of space. That's why Perl man page often look so useless. They don't distinguish what is important what is not; what is used frequently what is not. Here are some tips that make references usage more transparent:

References to arrays

Now let's try to create a reference to array. There are two ways to accomplish this task:

References to hashes

References to hashes are created similarly to references to arrays. You also have two ways to create them

Good introduction to references to hashes can be found in Managing Rich Data Structures. Here is one example:

I thought about finding some way to store those hashes as an array of anonymous hashes (one hash per ad), but then I realized that an array wouldn't let me access a particular ad's data easily. The hashes would be in the order in which I saved them into the array, but that wouldn't translate easily to the ad for a particular date. For example, how would I know where to find the data for next Monday's newsletter? Is it in $array[8] or $array[17]?

Hmm. Each anonymous hash could be identified by a particular date--the key (!) to locating the ad for any particular date. What kind of data structure associates a unique key with a value? A hash, of course! My data would fit nicely into a hash of hashes.

The name I chose for the hash was %data_for_ad_on. Choosing a hash name that ends in a preposition provides a more natural-reading and meaningful name; the key for data for the December 8, 2005 banner ad would be 2005_12_08, for example, and the way to access the value associated with that key would be $data_for_ad_on{2005_12_08}.

In code, this is how the data for two days of newsletters could be represented as a hash of hashes:

%data_for_ad_on = ( '2005_12_08' => { 'url' => 'http://roadrunners-r-us.com/index.html', 'gif' => 'http://myserver.com/banners/roadrunners_banner.gif', 'headline' => 'Use Roadrunners R Us for speedy, reliable deliveries!', }, '2005_12_09' => { 'url' => 'http://acme.com/index.html', 'gif' => 'http://myserver.com/banners/acme_banner.gif', 'headline' => 'Look to Acme for quality, inexpensive widgets!', }, );

The keys of the named hash are 2005_12_08 and 2005_12_09. Each key's value is a reference to an anonymous hash that contains its own keys and values. When a hash is created using braces instead of parentheses, its value is a reference to that unnamed, "anonymous" hash. I need to use a reference because a hash is permitted to contain only scalar keys and scalar values; another hash can't be stored as a value. A reference to that hash works, because it acts like a scalar.

Variants of Notation for References

We already know that reference is a scalar value, and we need to store it in a scalar variable which assumes a specific type . There are just two more ways to use it:

If $aref contains a reference to an array, then you can use {$aref} anywhere you would normally put the name of an array. For example, @{$aref} instead of @array. In most cases curvy parentethesis can be dropped, so you can use @$array.

Let's assume that $aref=\@a. Then the following are equivalent ways to address the values of array @a:

@a		@{$aref}		An array
reverse @a	reverse @{$aref}	Reverse the array
$a[1]		${$aref}[1]		An element of the array
$a[2] = 1;	${$aref}[2] = 1  	Assigning an element

On each line above there are two expressions that do the same thing. The left-hand versions operate on the array @a, and the right-hand versions operate on the save array using reference $aref.

The same is true for references to a hash (again let's assume that $href=\%h), for example:

%h		%{$href}		A hash
keys %h		keys %{$href}		Get the keys from the hash
$h{'red'}	${$href}{'red'}		An element of the hash
$h{'red'} = 17	${$href}{'red'} = 17	Assigning an element

Most often, when you have an array or a hash, you want to get or set a single element from it. ${$aref}[3] and ${$href}{'red'} have too much punctuation, and Perl lets you abbreviate.

Tips:

References to File Handles

Sometimes, you have to write output to multiple output files. For example, an application programmer might want the output to go to the screen in one instance, the printer in another, and a file in another-or even all three at the same time.

There are several ways to make a reference to filehandle:

typeglobs

Typeglobbing automatically aliases all forms of variable to a new name. This way all other uses of identifier (such as  the scalar, the array, and the hash) will be aliases too, even if only an alias to the filehandle is required. To create typeglob alias you need to prefix variable with the asterisk:

*fhref;

When used in this manner, the asterisk operator is also known as a typeglob and like \ provides reference to the variable. This is a unique feature introduced in Perl 4 and linked to the specific organization of Perl symbol table. Typeglobbing links all forms of the identifier creating what in shell is called an alias, so the sort_array=*array1 typeglobs @array1, %array1, and $array1. The best explanation can be found in Advanced Perl:

Perl has a curious feature that is typically not seen in other languages: you can use the same name for both data and nondata types. For example, the scalar $spud , the array @spud , the hash %spud , the subroutine &spud , the filehandle spud , and the format name spud are all simultaneously valid and completely independent of each other. In other words, Perl provides distinct namespaces for each type of entity. I do not have an explanation for why this feature is present. In fact, I consider it a rather dubious facility and recommend that you use a distinct name for each logical entity in your program; you owe it to the poor fellow who's going to maintain your code (which might be you!).

Perl uses a symbol table (implemented internally as a hash table)[ 1 ] to map identifier names (the string "spud" without the prefix) to the appropriate values. But you know that a hash table does not tolerate duplicate keys, so you can't really have two entries in the hash table with the same name pointing to two different values. For this reason, Perl interposes a structure called a typeglob between the symbol table entry and the other data types, as shown in Figure 3.1 ; it is just a bunch of pointers to values that can be accessed by the same name, with one pointer for each value type. In the typical case, in which you have unique identifier names, all but one of these pointers are null.

[1] Actually, it is one symbol table per package, where each package is a distinct namespace. For now, this distinction does not matter. We'll revisit this issue in Chapter 6, Modules .

3.1: Symbol table and typeglobs

A typeglob is a real data type accessible from script space and has the prefix " * "; while you can think of it as a wildcard representing all values sharing the identifier name, there's no pattern matching going on. You can assign typeglobs, store them in arrays, create local versions of them, or print them out, just as you can for any fundamental type. More on this in a moment.

You can use a typeglob in the same way you use a reference because the de-reference syntax always indicates the type of reference you want. ${*fhref} and ${\$fhref} are equvalent ways to get the value of the scalar variable $fhref. Basically, *fhref refers to the entry in the internal _main associative array of all symbol names like @fhref, %fhref, $fhref for the _main package. translates to $_main{'fhref'} if you are in the _main package context. If you are in another package, the _packageName{} hash is used.

When evaluated, a typeglob produces a scalar value that represents the pointer to the variable of the type specified. In our case this be file handle. This mechanism is widely used with filehandles, much less with anything else.  Rather than make separate print statements for each filehandle, now you can pass filehandle as a parameter to the subroutine which will print a record to the particular file:

$stdin_ref=*STDIN;
$stder_ref=*STDER;
out($stdin_ref,"Script started);
... ... 
#now lets put a record in STDER file
out(stder_ref,"Something wrong");

Notice that the pointer to the  file handle is extracted using *HANDLE syntax. In the subroutine you simply assign this pointer to a variable use this variable in print statement (you do not need to de-reference it for filehandles):

sub out 
{
    my $fh = shift;
    print $fh "$_[0]\n"; 
}

Obtaining a pointer to anonymous filehandle using function new

If you want anonymous filehandles you can use the new method from the IO::File module, store it in a scalar variable, and use it as though it were a normal filehandle:

use IO::File;                     # make anon filehandle, Perl 5.004 or higher
$fh = IO::File->new();

Prev | Up | Contents | Down | Next



Etc

Society

Groupthink : Two Party System as Polyarchy : Corruption of Regulators : Bureaucracies : Understanding Micromanagers and Control Freaks : Toxic Managers :   Harvard Mafia : Diplomatic Communication : Surviving a Bad Performance Review : Insufficient Retirement Funds as Immanent Problem of Neoliberal Regime : PseudoScience : Who Rules America : Neoliberalism  : The Iron Law of Oligarchy : Libertarian Philosophy

Quotes

War and Peace : Skeptical Finance : John Kenneth Galbraith :Talleyrand : Oscar Wilde : Otto Von Bismarck : Keynes : George Carlin : Skeptics : Propaganda  : SE quotes : Language Design and Programming Quotes : Random IT-related quotesSomerset Maugham : Marcus Aurelius : Kurt Vonnegut : Eric Hoffer : Winston Churchill : Napoleon Bonaparte : Ambrose BierceBernard Shaw : Mark Twain Quotes

Bulletin:

Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks The efficient markets hypothesis : Political Skeptic Bulletin, 2013 : Unemployment Bulletin, 2010 :  Vol 23, No.10 (October, 2011) An observation about corporate security departments : Slightly Skeptical Euromaydan Chronicles, June 2014 : Greenspan legacy bulletin, 2008 : Vol 25, No.10 (October, 2013) Cryptolocker Trojan (Win32/Crilock.A) : Vol 25, No.08 (August, 2013) Cloud providers as intelligence collection hubs : Financial Humor Bulletin, 2010 : Inequality Bulletin, 2009 : Financial Humor Bulletin, 2008 : Copyleft Problems Bulletin, 2004 : Financial Humor Bulletin, 2011 : Energy Bulletin, 2010 : Malware Protection Bulletin, 2010 : Vol 26, No.1 (January, 2013) Object-Oriented Cult : Political Skeptic Bulletin, 2011 : Vol 23, No.11 (November, 2011) Softpanorama classification of sysadmin horror stories : Vol 25, No.05 (May, 2013) Corporate bullshit as a communication method  : Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law

History:

Fifty glorious years (1950-2000): the triumph of the US computer engineering : Donald Knuth : TAoCP and its Influence of Computer Science : Richard Stallman : Linus Torvalds  : Larry Wall  : John K. Ousterhout : CTSS : Multix OS Unix History : Unix shell history : VI editor : History of pipes concept : Solaris : MS DOSProgramming Languages History : PL/1 : Simula 67 : C : History of GCC developmentScripting Languages : Perl history   : OS History : Mail : DNS : SSH : CPU Instruction Sets : SPARC systems 1987-2006 : Norton Commander : Norton Utilities : Norton Ghost : Frontpage history : Malware Defense History : GNU Screen : OSS early history

Classic books:

The Peter Principle : Parkinson Law : 1984 : The Mythical Man-MonthHow to Solve It by George Polya : The Art of Computer Programming : The Elements of Programming Style : The Unix Hater’s Handbook : The Jargon file : The True Believer : Programming Pearls : The Good Soldier Svejk : The Power Elite

Most popular humor pages:

Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : IT Slang : C++ Humor : ARE YOU A BBS ADDICT? : The Perl Purity Test : Object oriented programmers of all nations : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor : C Humor : Scripting Humor : Real Programmers Humor : Web Humor : GPL-related Humor : OFM Humor : Politically Incorrect Humor : IDS Humor : "Linux Sucks" Humor : Russian Musical Humor : Best Russian Programmer Humor : Microsoft plans to buy Catholic Church : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor : Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor : Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor

The Last but not Least Technology is dominated by two types of people: those who understand what they do not manage and those who manage what they do not understand ~Archibald Putt. Ph.D


Copyright © 1996-2021 by Softpanorama Society. www.softpanorama.org was initially created as a service to the (now defunct) UN Sustainable Development Networking Programme (SDNP) without any remuneration. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available to advance understanding of computer science, IT technology, economic, scientific, and social issues. We believe this constitutes a 'fair use' of any such copyrighted material as provided by section 107 of the US Copyright Law according to which such material can be distributed without profit exclusively for research and educational purposes.

This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...

You can use PayPal to to buy a cup of coffee for authors of this site

Disclaimer:

The statements, views and opinions presented on this web page are those of the author (or referenced source) and are not endorsed by, nor do they necessarily reflect, the opinions of the Softpanorama society. We do not warrant the correctness of the information provided or its fitness for any purpose. The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be tracked by Google please disable Javascript for this site. This site is perfectly usable without Javascript.

Last modified: March, 12, 2019