Perl is a magical language! The addition or omission of a single character can result in a dramatic change in the working of the script!
Try writing a C program and intentionally add an extra character or remove an existing character from the program. What do you get?
Most certainly a syntax error, of course!
Now try that in Perl. What do you get?
Most probably, a script without any syntax errors!
Why?
Perl has a whole lot of implicit rules that “try to make sense” out of “whatever given”! Just to give you an idea of how a single character can drastically change the meaning in the script, here’s my compilation of various identifiers in Perl with the same basic name “x”, but with completely different meanings!
No. | Expression | Meaning |
---|---|---|
1 | x | File/directory handle x or format name x |
2 | $x | Scalar variable x |
3 | \$x | Reference to scalar variable x |
4 | ${$x} | Value of the reference x to a scalar variable |
5 | $$x | Value of the reference x to a scalar variable |
6 | $x[0] | First scalar in the array x |
7 | $x->[0] | First scalar in the array referenced by x |
8 | $x{0} | Value of the key 0 in the hash x |
9 | $x->{0} | Value of the key 0 in the hash referenced by x |
10 | ($x) | List containing scalar x |
11 | @x | Array x |
12 | \@x | Reference to array x |
13 | @{$x} | Array referenced by x |
14 | @$x | Array referenced by x |
15 | @x[0] | Subarray (array slice) of array x made of just the first element in array x |
16 | [$x] | Reference to an anonymous array containing the value of scalar x |
17 | %x | Hash x |
18 | \%x | Reference to hash x |
19 | %{$x} | Hash referenced by x |
20 | %$x | Hash referenced by x |
21 | {$x} | Reference to an anonymous hash containing the value of scalar x as a key |
22 | @x{0} | Hash slice of hash x containing just the value of the key 0 |
23 | &x | Subroutine x |
24 | \&x | Reference to subroutine x |
25 | &{$x} | Subroutine referenced by x |
26 | &$x | Subroutine referenced by x |
27 | *x | Typeglob x |
28 | *x{SCALAR} | Reference to scalar variable x |
29 | *x{ARRAY} | Reference to array x |
30 | *x{HASH} | Reference to hash x |
31 | *x{CODE} | Reference to subroutine x |
32 | *x{IO} | Reference to file/sirectory handle x |
33 | *x{GLOB} | Reference to typeglob x |
34 | *x{FORMAT} | Reference to format x |
Related Articles
No user responded in this post