| Start |
|---|
| Scalars Scalars are Perl's most basic data type. They're marked with the sigil
Perl converts between numbers and strings on the fly, based on what a particular operator expects. When converting a string into a number, Perl takes as many digits from the front of a string as it can – hence why Based on whether you want to treat the contents of a scalar as a string or a number, you need to use different operators. Do not mix them. Attempting to use string operations on numbers will not raise warnings; attempting to use number operations on non-numeric strings will. Do be aware that some non-digit strings such as |
| Array References Array References are scalars ( These can be created more short-hand as follows: Modifying / Using array references require dereferencing them first. New postfix dereference syntax, available by default from v5.24 When accessing an arrayref's contents by index you can use the Unlike arrays, arrayrefs can be nested: When used as Boolean, references are always true. |
| Arrays Arrays store an ordered sequence of values. You can access the contents by index, or iterate over them. The values will stay in the order you filled them in. Arrays are mutable: Finally, you can loop over the contents of an array: When used as booleans, arrays are true if they are not empty. |
| Scalar References A reference is a scalar variable (one prefixed by To get the referred-to data, you de-reference it. New postfix dereference syntax, available by default from v5.24 This "de-referenced value" can then be changed like it was the original variable. A reference is always truthy – even if the value it refers to is falsy (like You may want a Scalar Reference If:
You wish to implement a lightweight inside out object model, where objects handed to calling code don't carry user visible metadata: |
| Typeglobs, typeglob refs, filehandles and constants A typeglob Typeglobs are more commonly handled when dealing with files. Typeglobs can also be used to make global read-only variables, though |
| Hashes Hashes can be understood as lookup-tables. You can access its contents by specifiying a key for each of them. Keys must be strings. If they're not, they will be converted to strings. If you give the hash simply a known key, it will serve you its value. In the following example, note the brackets and sigil: you access an element of While Perl by default will try to use barewords as strings, Like with arrays, you can access multiple hash elements at the same time. This is called a hash slice. The resulting value is a list, so use the Iterate over the keys of an hash with If you do not actually need the keys like in the previous example, You can also use a while loop with Access to unset elements returns undef, not an error:
This requires some explanation. The contents of As those elements get assigned into the hash, odd elements become hash keys and even elements become hash values. When a key is specified multiple times in a list to be assigned to a hash, the last value wins. This effectively discards duplicates. A faster way to turn a list into a hash uses assignment to a hash slice. It uses the The following application of hashes also exploits the fact that hashes and lists can often be used interchangeably to implement named function args: When used as booleans, hashes are true if they are not empty. |
| Hash References Hash references are scalars which contain a pointer to the memory location containing the data of a hash. Because the scalar points directly to the hash itself, when it is passed to a subroutine, changes made to the hash are not local to the subroutine as with a regular hash, but instead are global. First, let's examine what happens when you pass a normal hash to a subroutine and modify it within there: Which results in: Notice that after we exit the subroutine, the hash remains unaltered; all changes to it were local to the modify subroutine, because we passed a copy of the hash, not the hash itself. In comparison, when you pass a hashref, you are passing the address to the original hash, so any changes made within the subroutine will be made to the original hash: This will result in: |
| Sigils Perl has a number of sigils: These look like sigils, but aren't: You can use braces after the sigil if you should be so inclined. Occasionally, this improves readability. While you use different sigils to define variables of different types, the same variable can be accessed in different ways based on what sigils you use. This is especially true of references. In order to use a referenced value you can combine sigils together. Here's a perhaps less confusing way to think about it. As we saw earlier, you can use braces to wrap what's on the right of a sigil. So you can think of As it turns out, ...and the same trick works for other sigils, too. ...but if the "argument" to a sigil is simple, you can leave the braces away. Things can get excessively extravagant. This works, but please Perl responsibly. For most normal use, you can just use subroutine names without a sigil. (Variables without a sigil are typically called "barewords".) The Syntax
|
Read Article
https://develop.horje.com/learn/1434/reference