icon Top 9 categories map      RocketAware > Perl >

CAVEAT ON PRECEDENCE

Tips: Browse or Search all pages for efficient awareness of Perl functions, operators, and FAQs.



Home

Search Perl pages


Subjects

By activity
Professions, Sciences, Humanities, Business, ...

User Interface
Text-based, GUI, Audio, Video, Keyboards, Mouse, Images,...

Text Strings
Conversions, tests, processing, manipulation,...

Math
Integer, Floating point, Matrix, Statistics, Boolean, ...

Processing
Algorithms, Memory, Process control, Debugging, ...

Stored Data
Data storage, Integrity, Encryption, Compression, ...

Communications
Networks, protocols, Interprocess, Remote, Client Server, ...

Hard World
Timing, Calendar and Clock, Audio, Video, Printer, Controls...

File System
Management, Filtering, File & Directory access, Viewers, ...

    

CAVEAT ON PRECEDENCE

Speaking of things like @{$LoL[$i]}, the following are actually the same thing:

    $listref->[2][2]    # clear
    $$listref[2][2]     # confusing

That's because Perl's precedence rules on its five prefix dereferencers (which look like someone swearing: $ @ * % &) make them bind more tightly than the postfix subscripting brackets or braces! This will no doubt come as a great shock to the C or C++ programmer, who is quite accustomed to using *a[i] to mean what's pointed to by the i'th element of a. That is, they first take the subscript, and only then dereference the thing at that subscript. That's fine in C, but this isn't C.

The seemingly equivalent construct in Perl, $$listref[$i] first does the deref of $listref, making it take $listref as a reference to an array, and then dereference that, and finally tell you the i'th value of the array pointed to by $LoL. If you wanted the C notion, you'd have to write ${$LoL[$i]} to force the $LoL[$i] to get evaluated first before the leading $ dereferencer.


Source: Perl Data Structures Cookbook
Copyright: Larry Wall, et al.
Next: Why you should always use strict

Previous: COMMON MISTAKES



(Corrections, notes, and links courtesy of RocketAware.com)


[Overview Topics]

Up to: Data structures (In memory)




Rapid-Links: Search | About | Comments | Submit Path: RocketAware > Perl > perldsc/CAVEAT_ON_PRECEDENCE.htm
RocketAware.com is a service of Mib Software
Copyright 2000, Forrest J. Cavalier III. All Rights Reserved.
We welcome submissions and comments