# SOME DESCRIPTIVE TITLE # Copyright (C) YEAR Free Software Foundation, Inc. # This file is distributed under the same license as the PACKAGE package. # FIRST AUTHOR , YEAR. # #, fuzzy msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "POT-Creation-Date: 2026-06-05 06:34+0200\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" "Language: \n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" # #-#-#-#-# fedora-44: perlref.1.pot (PACKAGE VERSION) #-#-#-#-# # # #-#-#-#-# fedora-rawhide: perlref.1.pot (PACKAGE VERSION) #-#-#-#-# # # #-#-#-#-# mageia-cauldron: perlref.1.pot (PACKAGE VERSION) #-#-#-#-# # # #-#-#-#-# opensuse-leap-16-1: perlref.1.pot (PACKAGE VERSION) #-#-#-#-# # # #-#-#-#-# opensuse-tumbleweed: perlref.1.pot (PACKAGE VERSION) #-#-#-#-# # #. #-#-#-#-# debian-trixie: perlref.1.pot (PACKAGE VERSION) #-#-#-#-# #. ======================================================================== #. type: IX #. #-#-#-#-# debian-unstable: perlref.1.pot (PACKAGE VERSION) #-#-#-#-# #. ======================================================================== #. type: IX #. #-#-#-#-# fedora-44: perlref.1.pot (PACKAGE VERSION) #-#-#-#-# #. Required to disable full justification in groff 1.23.0. #. ======================================================================== #. type: IX #. #-#-#-#-# fedora-rawhide: perlref.1.pot (PACKAGE VERSION) #-#-#-#-# #. Required to disable full justification in groff 1.23.0. #. ======================================================================== #. type: IX #. #-#-#-#-# mageia-cauldron: perlref.1.pot (PACKAGE VERSION) #-#-#-#-# #. Required to disable full justification in groff 1.23.0. #. ======================================================================== #. type: IX #. #-#-#-#-# opensuse-leap-16-1: perlref.1.pot (PACKAGE VERSION) #-#-#-#-# #. Required to disable full justification in groff 1.23.0. #. ======================================================================== #. type: IX #. #-#-#-#-# opensuse-tumbleweed: perlref.1.pot (PACKAGE VERSION) #-#-#-#-# #. Required to disable full justification in groff 1.23.0. #. ======================================================================== #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Title" msgstr "" # #-#-#-#-# fedora-44: perlref.1.pot (PACKAGE VERSION) #-#-#-#-# # # #-#-#-#-# fedora-rawhide: perlref.1.pot (PACKAGE VERSION) #-#-#-#-# # # #-#-#-#-# mageia-cauldron: perlref.1.pot (PACKAGE VERSION) #-#-#-#-# # # #-#-#-#-# opensuse-leap-16-1: perlref.1.pot (PACKAGE VERSION) #-#-#-#-# # # #-#-#-#-# opensuse-tumbleweed: perlref.1.pot (PACKAGE VERSION) #-#-#-#-# # #. #-#-#-#-# debian-trixie: perlref.1.pot (PACKAGE VERSION) #-#-#-#-# #. ======================================================================== #. type: IX #. #-#-#-#-# debian-unstable: perlref.1.pot (PACKAGE VERSION) #-#-#-#-# #. ======================================================================== #. type: IX #. #-#-#-#-# fedora-44: perlref.1.pot (PACKAGE VERSION) #-#-#-#-# #. Required to disable full justification in groff 1.23.0. #. ======================================================================== #. type: IX #. #-#-#-#-# fedora-rawhide: perlref.1.pot (PACKAGE VERSION) #-#-#-#-# #. Required to disable full justification in groff 1.23.0. #. ======================================================================== #. type: IX #. #-#-#-#-# mageia-cauldron: perlref.1.pot (PACKAGE VERSION) #-#-#-#-# #. Required to disable full justification in groff 1.23.0. #. ======================================================================== #. type: IX #. #-#-#-#-# opensuse-leap-16-1: perlref.1.pot (PACKAGE VERSION) #-#-#-#-# #. Required to disable full justification in groff 1.23.0. #. ======================================================================== #. type: IX #. #-#-#-#-# opensuse-tumbleweed: perlref.1.pot (PACKAGE VERSION) #-#-#-#-# #. Required to disable full justification in groff 1.23.0. #. ======================================================================== #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "PERLREF 1" msgstr "" #. type: TH #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "PERLREF" msgstr "" #. type: TH #: debian-trixie #, no-wrap msgid "2025-07-27" msgstr "" #. type: TH #: debian-trixie debian-unstable #, no-wrap msgid "perl v5.40.1" msgstr "" #. type: TH #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Perl Programmers Reference Guide" msgstr "" #. type: SH #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "NAME" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "perlref - Perl references and nested data structures" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Xref" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "reference pointer data structure structure struct" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "NOTE" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Header" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "This is complete documentation about all aspects of references. For a " "shorter, tutorial introduction to just the essential features, see " "perlreftut." msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "DESCRIPTION" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Before release 5 of Perl it was difficult to represent complex data " "structures, because all references had to be symbolic--and even then it was " "difficult to refer to a variable instead of a symbol table entry. Perl now " "not only makes it easier to use symbolic references to variables, but also " "lets you have \"hard\" references to any piece of data or code. Any scalar " "may hold a hard reference. Because arrays and hashes contain scalars, you " "can now easily build arrays of arrays, arrays of hashes, hashes of arrays, " "arrays of hashes of functions, and so on." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Hard references are smart--they keep track of reference counts for you, " "automatically freeing the thing referred to when its reference count goes to " "zero. (Reference counts for values in self-referential or cyclic data " "structures may not go to zero without a little help; see \"Circular " "References\" for a detailed explanation.) If that thing happens to be an " "object, the object is destructed. See perlobj for more about objects. (In " "a sense, everything in Perl is an object, but we usually reserve the word " "for references to objects that have been officially \"blessed\" into a class " "package.)" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "Symbolic references are names of variables or other objects, just as a " "symbolic link in a Unix filesystem contains merely the name of a file. The " "CW<*glob> notation is something of a symbolic reference. (Symbolic " "references are sometimes called \"soft references\", but please don't call " "them that; references are confusing enough without useless synonyms.)" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "reference, symbolic reference, soft symbolic reference soft reference" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "In contrast, hard references are more like hard links in a Unix file system: " "They are used to access an underlying object without concern for what its " "(other) name is. When the word \"reference\" is used without an adjective, " "as in the following paragraph, it is usually talking about a hard reference." msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "reference, hard hard reference" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "References are easy to use in Perl. There is just one overriding principle: " "in general, Perl does no implicit referencing or dereferencing. When a " "scalar is holding a reference, it always behaves as a simple scalar. It " "doesn't magically start being an array or hash or subroutine; you have to " "tell it explicitly to do so, by dereferencing it." msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Making References" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "reference, creation referencing" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Subsection" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "References can be created in several ways." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "I" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "\\ backslash" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Backslash Operator" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "By using the backslash operator on a variable, subroutine, or value. (This " "works much like the & (address-of) operator in C.) This typically creates " "I reference to a variable, because there's already a reference to " "the variable in the symbol table. But the symbol table reference might go " "away, and you'll still have the reference that the backslash returned. Here " "are some examples:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& $scalarref = \\e$foo;\n" "\\& $arrayref = \\e@ARGV;\n" "\\& $hashref = \\e%ENV;\n" "\\& $coderef = \\e&handler;\n" "\\& $globref = \\e*foo;\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "It isn't possible to create a true reference to an IO handle (filehandle or " "dirhandle) using the backslash operator. The most you can get is a " "reference to a typeglob, which is actually a complete symbol table entry. " "But see the explanation of the CW<*foo{THING}> syntax below. However, you " "can still use type globs and globrefs as though they were IO handles." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "I" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "array, anonymous [ [] square bracket bracket, square arrayref array reference reference, array" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Square Brackets" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "A reference to an anonymous array can be created using square brackets:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "\\& $arrayref = [1, 2, [\\*(Aqa\\*(Aq, \\*(Aqb\\*(Aq, \\*(Aqc\\*(Aq]];\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "Here we've created a reference to an anonymous array of three elements whose " "final element is itself a reference to another anonymous array of three " "elements. (The multidimensional syntax described later can be used to " "access this. For example, after the above, CW<\\*(C`$arrayref-E[2]" "[1]\\*(C'> would have the value \"b\".)" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "Taking a reference to an enumerated list is not the same as using square " "brackets--instead it's the same as creating a list of references!" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& @list = (\\e$x, \\e@y, \\e%z);\n" "\\& @list = \\e($x, @y, %z); # same thing!\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "As a special case, CW<\\*(C`\\e(@foo)\\*(C'> returns a list of references to " "the contents of CW<@foo>, not a reference to CW<@foo> itself. Likewise for " "CW<%foo>, except that the key references are to copies (since the keys are " "just strings rather than full-fledged scalars)." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "I" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "hash, anonymous { {} curly bracket bracket, curly brace hashref hash reference reference, hash" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Curly Brackets" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "A reference to an anonymous hash can be created using curly brackets:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& $hashref = {\n" "\\& \\*(AqAdam\\*(Aq =E \\*(AqEve\\*(Aq,\n" "\\& \\*(AqClyde\\*(Aq =E \\*(AqBonnie\\*(Aq,\n" "\\& };\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Anonymous hash and array composers like these can be intermixed freely to " "produce as complicated a structure as you want. The multidimensional syntax " "described below works for these too. The values above are literals, but " "variables and expressions would work just as well, because assignment " "operators in Perl (even within B or B) are executable " "statements, not compile-time declarations." msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "Because curly brackets (braces) are used for several other things including " "BLOCKs, you may occasionally have to disambiguate braces at the beginning of " "a statement by putting a CW<\\*(C`+\\*(C'> or a CW<\\*(C`return\\*(C'> in " "front so that Perl realizes the opening brace isn't starting a BLOCK. The " "economy and mnemonic value of using curlies is deemed worth this occasional " "extra hassle." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "For example, if you wanted a function to make a new hash and return a " "reference to it, you have these options:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& sub hashem { { @_ } } # silently wrong\n" "\\& sub hashem { +{ @_ } } # ok\n" "\\& sub hashem { return { @_ } } # ok\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "On the other hand, if you want the other meaning, you can do this:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& sub showem { { @_ } } # ambiguous (currently ok,\n" "\\& # but may change)\n" "\\& sub showem { {; @_ } } # ok\n" "\\& sub showem { { return @_ } } # ok\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "The leading CW<\\*(C`+{\\*(C'> and CW<\\*(C`{;\\*(C'> always serve to " "disambiguate the expression to mean either the HASH reference, or the BLOCK." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "I" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "subroutine, anonymous subroutine, reference reference, subroutine scope, lexical closure lexical lexical scope" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Anonymous Subroutines" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "A reference to an anonymous subroutine can be created by using " "\\&CW<\\*(C`sub\\*(C'> without a subname:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "\\& $coderef = sub { print \"Boink!\\en\" };\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "Note the semicolon. Except for the code inside not being immediately " "executed, a CW<\\*(C`sub {}\\*(C'> is not so much a declaration as it is an " "operator, like CW<\\*(C`do{}\\*(C'> or CW<\\*(C`eval{}\\*(C'>. (However, no " "matter how many times you execute that particular line (unless you're in an " "\\&CW), CW<$coderef> will still have a reference to the " "I anonymous subroutine.)" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "Anonymous subroutines act as closures with respect to B variables, " "that is, variables lexically visible within the current scope. Closure is a " "notion out of the Lisp world that says if you define an anonymous function " "in a particular lexical context, it pretends to run in that context even " "when it's called outside the context." msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "In human terms, it's a funny way of passing arguments to a subroutine when " "you define it as well as when you call it. It's useful for setting up " "little bits of code to run later, such as callbacks. You can even do object-" "oriented stuff with it, though Perl already provides a different mechanism " "to do that--see perlobj." msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "You might also think of closure as a way to write a subroutine template " "without using B. Here's a small example of how closures work:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& sub newprint {\n" "\\& my $x = shift;\n" "\\& return sub { my $y = shift; print \"$x, $y!\\en\"; };\n" "\\& }\n" "\\& $h = newprint(\"Howdy\");\n" "\\& $g = newprint(\"Greetings\");\n" "\\&\n" "\\& # Time passes...\n" "\\&\n" "\\& &$h(\"world\");\n" "\\& &$g(\"earthlings\");\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "This prints" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& Howdy, world!\n" "\\& Greetings, earthlings!\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "Note particularly that CW<$x> continues to refer to the value passed into " "B I \"my CW<$x>\" having gone out of scope by the time " "the anonymous subroutine runs. That's what a closure is all about." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "This applies only to lexical variables, by the way. Dynamic variables " "continue to work as they have always worked. Closure is not something that " "most Perl programmers need trouble themselves about to begin with." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "I" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "constructor new" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Constructors" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "References are often returned by special subroutines called constructors. " "Perl objects are just references to a special type of object that happens to " "know which package it's associated with. Constructors are just special " "subroutines that know how to create that association. They do so by " "starting with an ordinary reference, and it remains an ordinary reference " "even while it's also being an object. Constructors are often named " "CW. You I call them indirectly:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "\\& $objref = new Doggie( Tail =E \\*(Aqshort\\*(Aq, Ears =E \\*(Aqlong\\*(Aq );\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "But that can produce ambiguous syntax in certain cases, so it's often better " "to use the direct method invocation approach:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& $objref = Doggie-Enew(Tail =E \\*(Aqshort\\*(Aq, Ears =E \\*(Aqlong\\*(Aq);\n" "\\&\n" "\\& use Term::Cap;\n" "\\& $terminal = Term::Cap-ETgetent( { OSPEED =E 9600 });\n" "\\&\n" "\\& use Tk;\n" "\\& $main = MainWindow-Enew();\n" "\\& $menubar = $main-EFrame(-relief =E \"raised\",\n" "\\& -borderwidth =E 2)\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "This indirect object syntax is only available when \\&CW<\\*(C`use feature " "\"indirect\"\\*(C'> is in effect, and that is not the case when CW<\\*(C`use " "v5.36\\*(C'> (or higher) is requested, it is best to avoid indirect object " "syntax entirely." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "I" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "autovivification" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Autovivification" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "References of the appropriate type can spring into existence if you " "dereference them in a context that assumes they exist. Because we haven't " "talked about dereferencing yet, we can't show you any examples yet." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "I" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "*foo{THING} *" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Typeglob Slots" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "A reference can be created by using a special syntax, lovingly known as the " "*foo{THING} syntax. *foo{THING} returns a reference to the THING slot in " "*foo (which is the symbol table entry which holds everything known as foo)." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& $scalarref = *foo{SCALAR};\n" "\\& $arrayref = *ARGV{ARRAY};\n" "\\& $hashref = *ENV{HASH};\n" "\\& $coderef = *handler{CODE};\n" "\\& $ioref = *STDIN{IO};\n" "\\& $globref = *foo{GLOB};\n" "\\& $formatref = *foo{FORMAT};\n" "\\& $globname = *foo{NAME}; # \"foo\"\n" "\\& $pkgname = *foo{PACKAGE}; # \"main\"\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Most of these are self-explanatory, but CW<*foo{IO}> deserves special " "attention. It returns the IO handle, used for file handles (\"open\" in " "perlfunc), sockets (\"socket\" in perlfunc and \"socketpair\" in perlfunc), " "and directory handles (\"opendir\" in perlfunc). For compatibility with " "previous versions of Perl, CW<*foo{FILEHANDLE}> is a synonym for CW<*foo{IO}" ">, though it is discouraged, to encourage a consistent use of one name: IO. " "On perls between v5.8 and v5.22, it will issue a deprecation warning, but " "this deprecation has since been rescinded." msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "\\&CW<*foo{THING}> returns undef if that particular THING hasn't been used " "yet, except in the case of scalars. CW<*foo{SCALAR}> returns a reference to " "an anonymous scalar if CW<$foo> hasn't been used yet. This might change in " "a future release." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "\\&CW<*foo{NAME}> and CW<*foo{PACKAGE}> are the exception, in that they " "return strings, rather than references. These return the package and name " "of the typeglob itself, rather than one that has been assigned to it. So, " "after \\&CW<\\*(C`*foo=*Foo::bar\\*(C'>, CW<*foo> will become \"*Foo::bar\" " "when used as a string, but CW<*foo{PACKAGE}> and CW<*foo{NAME}> will " "continue to produce \"main\" and \"foo\", respectively." msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "\\&CW<*foo{IO}> is an alternative to the CW<*HANDLE> mechanism given in " "\"Typeglobs and Filehandles\" in perldata for passing filehandles into or " "out of subroutines, or storing into larger data structures. Its " "disadvantage is that it won't create a new filehandle for you. Its " "advantage is that you have less risk of clobbering more than you want to " "with a typeglob assignment. (It still conflates file and directory handles, " "though.) However, if you assign the incoming value to a scalar instead of a " "typeglob as we do in the examples below, there's no risk of that happening." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& splutter(*STDOUT); # pass the whole glob\n" "\\& splutter(*STDOUT{IO}); # pass both file and dir handles\n" "\\&\n" "\\& sub splutter {\n" "\\& my $fh = shift;\n" "\\& print $fh \"her um well a hmmm\\en\";\n" "\\& }\n" "\\&\n" "\\& $rec = get_rec(*STDIN); # pass the whole glob\n" "\\& $rec = get_rec(*STDIN{IO}); # pass both file and dir handles\n" "\\&\n" "\\& sub get_rec {\n" "\\& my $fh = shift;\n" "\\& return scalar E$fhE;\n" "\\& }\n" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Using References" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "reference, use dereferencing dereference" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "That's it for creating references. By now you're probably dying to know how " "to use references to get back to your long-lost data. There are several " "basic methods." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "I" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Simple Scalar" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "Anywhere you'd put an identifier (or chain of identifiers) as part of a " "variable or subroutine name, you can replace the identifier with a simple " "scalar variable containing a reference of the correct type:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& $bar = $$scalarref;\n" "\\& push(@$arrayref, $filename);\n" "\\& $$arrayref[0] = \"January\";\n" "\\& $$hashref{\"KEY\"} = \"VALUE\";\n" "\\& &$coderef(1,2,3);\n" "\\& print $globref \"output\\en\";\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "It's important to understand that we are specifically I dereferencing " "\\&CW<$arrayref[0]> or CW<$hashref{\"KEY\"}> there. The dereference of the " "scalar variable happens I it does any key lookups. Anything more " "complicated than a simple scalar variable must use methods 2 or 3 below. " "However, a \"simple scalar\" includes an identifier that itself uses method " "1 recursively. Therefore, the following prints \"howdy\"." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& $refrefref = \\e\\e\\e\"howdy\";\n" "\\& print $$$$refrefref;\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "I" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Block" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "Anywhere you'd put an identifier (or chain of identifiers) as part of a " "variable or subroutine name, you can replace the identifier with a BLOCK " "returning a reference of the correct type. In other words, the previous " "examples could be written like this:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& $bar = ${$scalarref};\n" "\\& push(@{$arrayref}, $filename);\n" "\\& ${$arrayref}[0] = \"January\";\n" "\\& ${$hashref}{\"KEY\"} = \"VALUE\";\n" "\\& &{$coderef}(1,2,3);\n" "\\& $globref-Eprint(\"output\\en\"); # iff IO::Handle is loaded\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "Admittedly, it's a little silly to use the curlies in this case, but the " "BLOCK can contain any arbitrary expression, in particular, subscripted " "expressions:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "\\& &{ $dispatch{$index} }(1,2,3); # call correct routine\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "Because of being able to omit the curlies for the simple case of CW<$$x>, " "people often make the mistake of viewing the dereferencing symbols as proper " "operators, and wonder about their precedence. If they were, though, you " "could use parentheses instead of braces. That's not the case. Consider the " "difference below; case 0 is a short-hand version of case 1, \\&I case 2:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& $$hashref{\"KEY\"} = \"VALUE\"; # CASE 0\n" "\\& ${$hashref}{\"KEY\"} = \"VALUE\"; # CASE 1\n" "\\& ${$hashref{\"KEY\"}} = \"VALUE\"; # CASE 2\n" "\\& ${$hashref-E{\"KEY\"}} = \"VALUE\"; # CASE 3\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "Case 2 is also deceptive in that you're accessing a variable called " "CW<%hashref>, not dereferencing through CW<$hashref> to the hash it's " "presumably referencing. That would be case 3." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "I" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Arrow Notation" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Subroutine calls and lookups of individual array elements arise often enough " "that it gets cumbersome to use method 2. As a form of syntactic sugar, the " "examples for method 2 may be written:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& $arrayref-E[0] = \"January\"; # Array element\n" "\\& $hashref-E{\"KEY\"} = \"VALUE\"; # Hash element\n" "\\& $coderef-E(1,2,3); # Subroutine call\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "The left side of the arrow can be any expression returning a reference, " "including a previous dereference. Note that CW<$array[$x]> is I the " "same thing as CW<\\*(C`$array-E[$x]\\*(C'> here:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "\\& $array[$x]-E{\"foo\"}-E[0] = \"January\";\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "This is one of the cases we mentioned earlier in which references could " "spring into existence when in an lvalue context. Before this statement, " "CW<$array[$x]> may have been undefined. If so, it's automatically defined " "with a hash reference so that we can look up \\&CW<\\*(C`{\"foo\"}\\*(C'> in " "it. Likewise CW<\\*(C`$array[$x]-E{\"foo\"}\\*(C'> will automatically " "get defined with an array reference so that we can look up " "CW<\\*(C`[0]\\*(C'> in it. This process is called I." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "One more thing here. The arrow is optional I brackets subscripts, " "so you can shrink the above down to" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "\\& $array[$x]{\"foo\"}[0] = \"January\";\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "Which, in the degenerate case of using only ordinary arrays, gives you " "multidimensional arrays just like C's:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "\\& $score[$x][$y][$z] += 42;\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "Well, okay, not entirely like C's arrays, actually. C doesn't know how to " "grow its arrays on demand. Perl does." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "I" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Objects" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "If a reference happens to be a reference to an object, then there are " "probably methods to access the things referred to, and you should probably " "stick to those methods unless you're in the class package that defines the " "object's methods. In other words, be nice, and don't violate the object's " "encapsulation without a very good reason. Perl does not enforce " "encapsulation. We are not totalitarians here. We do expect some basic " "civility though." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "I" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Miscellaneous Usage" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Using a string or number as a reference produces a symbolic reference, as " "explained above. Using a reference as a number produces an integer " "representing its storage location in memory. The only useful thing to be " "done with this is to compare two references numerically to see whether they " "refer to the same location." msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "reference, numeric context" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& if ($ref1 == $ref2) { # cheap numeric compare of references\n" "\\& print \"refs 1 and 2 refer to the same thing\\en\";\n" "\\& }\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "Using a reference as a string produces both its referent's type, including " "any package blessing as described in perlobj, as well as the numeric address " "expressed in hex. The B operator returns just the type of thing the " "reference is pointing to, without the address. See \"ref\" in perlfunc for " "details and examples of its use." msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "reference, string context" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "The B operator may be used to associate the object a reference " "points to with a package functioning as an object class. See perlobj." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "A typeglob may be dereferenced the same way a reference can, because the " "dereference syntax always indicates the type of reference desired. So " "CW<\\*(C`${*foo}\\*(C'> and CW<\\*(C`${\\e$foo}\\*(C'> both indicate the " "same scalar variable." msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "Here's a trick for interpolating a subroutine call into a string:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "\\& print \"My sub returned @{[mysub(1,2,3)]} that time.\\en\";\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "The way it works is that when the CW<\\*(C`@{...}\\*(C'> is seen in the " "double-quoted string, it's evaluated as a block. The block creates a " "reference to an anonymous array containing the results of the call to " "CW<\\*(C`mysub(1,2,3)\\*(C'>. So the whole block returns a reference to an " "array, which is then dereferenced by CW<\\*(C`@{...}\\*(C'> and stuck into " "the double-quoted string. This chicanery is also useful for arbitrary " "expressions:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "\\& print \"That yields @{[$n + 5]} widgets\\en\";\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Similarly, an expression that returns a reference to a scalar can be " "dereferenced via CW<\\*(C`${...}\\*(C'>. Thus, the above expression may be " "written as:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "\\& print \"That yields ${\\e($n + 5)} widgets\\en\";\n" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Circular References" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "circular reference reference, circular" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "It is possible to create a \"circular reference\" in Perl, which can lead to " "memory leaks. A circular reference occurs when two references contain a " "reference to each other, like this:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& my $foo = {};\n" "\\& my $bar = { foo =E $foo };\n" "\\& $foo-E{bar} = $bar;\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "You can also create a circular reference with a single variable:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& my $foo;\n" "\\& $foo = \\e$foo;\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "In this case, the reference count for the variables will never reach 0, and " "the references will never be garbage-collected. This can lead to memory " "leaks." msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "Because objects in Perl are implemented as references, it's possible to have " "circular references with objects as well. Imagine a TreeNode class where " "each node references its parent and child nodes. Any node with a parent will " "be part of a circular reference." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "You can break circular references by creating a \"weak reference\". A weak " "reference does not increment the reference count for a variable, which means " "that the object can go out of scope and be destroyed. You can weaken a " "reference with the CW<\\*(C`weaken\\*(C'> function exported by the " "Scalar::Util module, or available as CW<\\*(C`builtin::weaken\\*(C'> " "directly in Perl version 5.35.7 or later." msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "Here's how we can make the first example safer:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& use Scalar::Util \\*(Aqweaken\\*(Aq;\n" "\\&\n" "\\& my $foo = {};\n" "\\& my $bar = { foo =E $foo };\n" "\\& $foo-E{bar} = $bar;\n" "\\&\n" "\\& weaken $foo-E{bar};\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "The reference from CW<$foo> to CW<$bar> has been weakened. When the " "\\&CW<$bar> variable goes out of scope, it will be garbage-collected. The " "next time you look at the value of the CW<\\*(C`$foo-E{bar}\\*(C'> key, " "it will be CW<\\*(C`undef\\*(C'>." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "This action at a distance can be confusing, so you should be careful with " "your use of weaken. You should weaken the reference in the variable that " "will go out of scope I. That way, the longer-lived variable will " "contain the expected reference until it goes out of scope." msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Symbolic references" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "We said that references spring into existence as necessary if they are " "undefined, but we didn't say what happens if a value used as a reference is " "already defined, but I a hard reference. If you use it as a " "reference, it'll be treated as a symbolic reference. That is, the value of " "the scalar is taken to be the I of a variable, rather than a direct " "link to a (possibly) anonymous value." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "People frequently expect it to work like this. So it does." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& $name = \"foo\";\n" "\\& $$name = 1; # Sets $foo\n" "\\& ${$name} = 2; # Sets $foo\n" "\\& ${$name x 2} = 3; # Sets $foofoo\n" "\\& $name-E[0] = 4; # Sets $foo[0]\n" "\\& @$name = (); # Clears @foo\n" "\\& &$name(); # Calls &foo()\n" "\\& $pack = \"THAT\";\n" "\\& ${\"${pack}::$name\"} = 5; # Sets $THAT::foo without eval\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "This is powerful, and slightly dangerous, in that it's possible to intend " "(with the utmost sincerity) to use a hard reference, and accidentally use a " "symbolic reference instead. To protect against that, you can say" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "\\& use strict \\*(Aqrefs\\*(Aq;\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "and then only hard references will be allowed for the rest of the enclosing " "block. An inner block may countermand that with" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "\\& no strict \\*(Aqrefs\\*(Aq;\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "Only package variables (globals, even if localized) are visible to symbolic " "references. Lexical variables (declared with B) aren't in a symbol " "table, and thus are invisible to this mechanism. For example:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& local $value = 10;\n" "\\& $ref = \"value\";\n" "\\& {\n" "\\& my $value = 20;\n" "\\& print $$ref;\n" "\\& }\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "This will still print 10, not 20. Remember that B affects package " "variables, which are all \"global\" to the package." msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Not-so-symbolic references" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Brackets around a symbolic reference can simply serve to isolate an " "identifier or variable name from the rest of an expression, just as they " "always have within a string. For example," msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& $push = \"pop on \";\n" "\\& print \"${push}over\";\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "has always meant to print \"pop on over\", even though push is a reserved " "word. This is generalized to work the same without the enclosing double " "quotes, so that" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "\\& print ${push} . \"over\";\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "and even" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "\\& print ${ push } . \"over\";\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "will have the same effect. This construct is I considered to be a " "symbolic reference when you're using strict refs:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& use strict \\*(Aqrefs\\*(Aq;\n" "\\& ${ bareword }; # Okay, means $bareword.\n" "\\& ${ \"bareword\" }; # Error, symbolic reference.\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Similarly, because of all the subscripting that is done using single words, " "the same rule applies to any bareword that is used for subscripting a hash. " "So now, instead of writing" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "\\& $hash{ \"aaa\" }{ \"bbb\" }{ \"ccc\" }\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "you can write just" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "\\& $hash{ aaa }{ bbb }{ ccc }\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "and not worry about whether the subscripts are reserved words. In the rare " "event that you do wish to do something like" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "\\& $hash{ shift }\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "you can force interpretation as a reserved word by adding anything that " "makes it more than a bareword:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& $hash{ shift() }\n" "\\& $hash{ +shift }\n" "\\& $hash{ shift @_ }\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "The CW<\\*(C`use warnings\\*(C'> pragma or the B<-w> switch will warn you if " "it interprets a reserved word as a string. But it will no longer warn you " "about using lowercase words, because the string is effectively quoted." msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Pseudo-hashes: Using an array as a hash" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "pseudo-hash pseudo hash pseudohash" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "Pseudo-hashes have been removed from Perl. The 'fields' pragma remains " "available." msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Function Templates" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "scope, lexical closure lexical lexical scope subroutine, nested sub, nested subroutine, local sub, local" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "As explained above, an anonymous function with access to the lexical " "variables visible when that function was compiled, creates a closure. It " "retains access to those variables even though it doesn't get run until " "later, such as in a signal handler or a Tk callback." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Using a closure as a function template allows us to generate many functions " "that act similarly. Suppose you wanted functions named after the colors " "that generated HTML font changes for the various colors:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "\\& print \"Be \", red(\"careful\"), \"with that \", green(\"light\");\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "The B and B functions would be similar. To create these, " "we'll assign a closure to a typeglob of the name of the function we're " "trying to build." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& @colors = qw(red blue green yellow orange purple violet);\n" "\\& for my $name (@colors) {\n" "\\& no strict \\*(Aqrefs\\*(Aq; # allow symbol table manipulation\n" "\\& *$name = *{uc $name} = sub { \"EFONT COLOR=\\*(Aq$name\\*(AqE@_E/FONTE\" };\n" "\\& }\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "Now all those different functions appear to exist independently. You can " "call B, B, B, B, B, etc. This " "technique saves on both compile time and memory use, and is less error-prone " "as well, since syntax checks happen at compile time. It's critical that any " "variables in the anonymous subroutine be lexicals in order to create a " "proper closure. That's the reasons for the CW<\\*(C`my\\*(C'> on the loop " "iteration variable." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "This is one of the only places where giving a prototype to a closure makes " "much sense. If you wanted to impose scalar context on the arguments of " "these functions (probably not a wise idea for this particular example), you " "could have written it this way instead:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "\\& *$name = sub ($) { \"EFONT COLOR=\\*(Aq$name\\*(AqE$_[0]E/FONTE\" };\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "However, since prototype checking happens at compile time, the assignment " "above happens too late to be of much use. You could address this by putting " "the whole loop of assignments within a BEGIN block, forcing it to occur " "during compilation." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Access to lexicals that change over time--like those in the " "CW<\\*(C`for\\*(C'> loop above, basically aliases to elements from the " "surrounding lexical scopes-- only works with anonymous subs, not with named " "subroutines. Generally said, named subroutines do not nest properly and " "should only be declared in the main package scope." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "This is because named subroutines are created at compile time so their " "lexical variables get assigned to the parent lexicals from the first " "execution of the parent block. If a parent scope is entered a second time, " "its lexicals are created again, while the nested subs still reference the " "old ones." msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "Anonymous subroutines get to capture each time you execute the " "CW<\\*(C`sub\\*(C'> operator, as they are created on the fly. If you are " "accustomed to using nested subroutines in other programming languages with " "their own private variables, you'll have to work at it a bit in Perl. The " "intuitive coding of this type of thing incurs mysterious warnings about " "\"will not stay shared\" due to the reasons explained above. For example, " "this won't work:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& sub outer {\n" "\\& my $x = $_[0] + 35;\n" "\\& sub inner { return $x * 19 } # WRONG\n" "\\& return $x + inner();\n" "\\& }\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "A work-around is the following:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& sub outer {\n" "\\& my $x = $_[0] + 35;\n" "\\& local *inner = sub { return $x * 19 };\n" "\\& return $x + inner();\n" "\\& }\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Now B can only be called from within B, because of the " "temporary assignments of the anonymous subroutine. But when it does, it has " "normal access to the lexical variable CW<$x> from the scope of \\&B " "at the time outer is invoked." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "This has the interesting effect of creating a function local to another " "function, something not normally supported in Perl." msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Postfix Dereference Syntax" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Beginning in v5.20.0, a postfix syntax for using references is available. " "It behaves as described in \"Using References\", but instead of a prefixed " "sigil, a postfixed sigil-and-star is used." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "For example:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable #, no-wrap msgid "" "\\& $r = \\e@a;\n" "\\& @b = $r-E@*; # equivalent to @$r or @{ $r }\n" "\\&\n" "\\& $r = [ 1, [ 2, 3 ], 4 ];\n" "\\& $r-E[1]-E@*; # equivalent to @{ $r-E[1] }\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "In Perl 5.20 and 5.22, this syntax must be enabled with CW. As of Perl 5.24, no feature declarations are " "required to make it available." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Postfix dereference should work in all circumstances where block (circumfix) " "dereference worked, and should be entirely equivalent. This syntax allows " "dereferencing to be written and read entirely left-to-right. The following " "equivalencies are defined:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& $sref-E$*; # same as ${ $sref }\n" "\\& $aref-E@*; # same as @{ $aref }\n" "\\& $aref-E$#*; # same as $#{ $aref }\n" "\\& $href-E%*; # same as %{ $href }\n" "\\& $cref-E&*; # same as &{ $cref }\n" "\\& $gref-E**; # same as *{ $gref }\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Note especially that CW<\\*(C`$cref-E&*\\*(C'> is I equivalent to " "CW<$cref-E()>, and can serve different purposes." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Glob elements can be extracted through the postfix dereferencing feature:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "\\& $gref-E*{SCALAR}; # same as *{ $gref }{SCALAR}\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Postfix array and scalar dereferencing I be used in interpolating " "strings (double quotes or the CW<\\*(C`qq\\*(C'> operator), but only if the " "\\&CW<\\*(C`postderef_qq\\*(C'> feature is enabled. Interpolation of postfix " "array highest index access (CW<\\*(C`-E$#*\\*(C'>) is also supported " "when the CW<\\*(C`postderef_qq\\*(C'> feature is enabled." msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Postfix Reference Slicing" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Value slices of arrays and hashes may also be taken with postfix " "dereferencing notation, with the following equivalencies:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& $aref-E@[ ... ]; # same as @$aref[ ... ]\n" "\\& $href-E@{ ... }; # same as @$href{ ... }\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Postfix key/value pair slicing, added in 5.20.0 and documented in the Key/" "Value Hash Slices section of perldata, also behaves as expected:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& $aref-E%[ ... ]; # same as %$aref[ ... ]\n" "\\& $href-E%{ ... }; # same as %$href{ ... }\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "As with postfix array, postfix value slice dereferencing I be used in " "interpolating strings (double quotes or the CW<\\*(C`qq\\*(C'> operator), " "but only if the CW<\\*(C`postderef_qq\\*(C'> feature is enabled." msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Assigning to References" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Beginning in v5.22.0, the referencing operator can be assigned to. It " "performs an aliasing operation, so that the variable name referenced on the " "left-hand side becomes an alias for the thing referenced on the right-hand " "side:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& \\e$x = \\e$y; # $x and $y now point to the same scalar\n" "\\& \\e&foo = \\e&bar; # foo() now means bar()\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "This syntax must be enabled with CW<\\*(C`use feature " "\\*(Aqrefaliasing\\*(Aq\\*(C'>. It is experimental, and will warn by " "default unless CW is " "in effect." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "These forms may be assigned to, and cause the right-hand side to be " "evaluated in scalar context:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& \\e$scalar\n" "\\& \\e@array\n" "\\& \\e%hash\n" "\\& \\e&sub\n" "\\& \\emy $scalar\n" "\\& \\emy @array\n" "\\& \\emy %hash\n" "\\& \\estate $scalar # or @array, etc.\n" "\\& \\eour $scalar # etc.\n" "\\& \\elocal $scalar # etc.\n" "\\& \\elocal our $scalar # etc.\n" "\\& \\e$some_array[$index]\n" "\\& \\e$some_hash{$key}\n" "\\& \\elocal $some_array[$index]\n" "\\& \\elocal $some_hash{$key}\n" "\\& condition ? \\e$this : \\e$that[0] # etc.\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Slicing operations and parentheses cause the right-hand side to be evaluated " "in list context:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& \\e@array[5..7]\n" "\\& (\\e@array[5..7])\n" "\\& \\e(@array[5..7])\n" "\\& \\e@hash{\\*(Aqfoo\\*(Aq,\\*(Aqbar\\*(Aq}\n" "\\& (\\e@hash{\\*(Aqfoo\\*(Aq,\\*(Aqbar\\*(Aq})\n" "\\& \\e(@hash{\\*(Aqfoo\\*(Aq,\\*(Aqbar\\*(Aq})\n" "\\& (\\e$scalar)\n" "\\& \\e($scalar)\n" "\\& \\e(my $scalar)\n" "\\& \\emy($scalar)\n" "\\& (\\e@array)\n" "\\& (\\e%hash)\n" "\\& (\\e&sub)\n" "\\& \\e(&sub)\n" "\\& \\e($foo, @bar, %baz)\n" "\\& (\\e$foo, \\e@bar, \\e%baz)\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Each element on the right-hand side must be a reference to a datum of the " "right type. Parentheses immediately surrounding an array (and possibly also " "CW<\\*(C`my\\*(C'>/CW<\\*(C`state\\*(C'>/CW<\\*(C`our\\*(C'>/" "CW<\\*(C`local\\*(C'>) will make each element of the array an alias to the " "corresponding scalar referenced on the right-hand side:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& \\e(@x) = \\e(@y); # @x and @y now have the same elements\n" "\\& \\emy(@x) = \\e(@y); # likewise\n" "\\& \\e(my @x) = \\e(@y); # likewise\n" "\\& push @x, 3; # but now @x has an extra element that @y lacks\n" "\\& \\e(@x) = (\\e$x, \\e$y, \\e$z); # @x now contains $x, $y, and $z\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Combining that form with CW<\\*(C`local\\*(C'> and putting parentheses " "immediately around a hash are forbidden (because it is not clear what they " "should do):" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& \\elocal(@array) = foo(); # WRONG\n" "\\& \\e(%hash) = bar(); # WRONG\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Assignment to references and non-references may be combined in lists and " "conditional ternary expressions, as long as the values on the right-hand " "side are the right type for each element on the left, though this may make " "for obfuscated code:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& (my $tom, \\emy $dick, \\emy @harry) = (\\e1, \\e2, [1..3]);\n" "\\& # $tom is now \\e1\n" "\\& # $dick is now 2 (read-only)\n" "\\& # @harry is (1,2,3)\n" "\\&\n" "\\& my $type = ref $thingy;\n" "\\& ($type ? $type eq \\*(AqARRAY\\*(Aq ? \\e@foo : \\e$bar : $baz) = $thingy;\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "A reference assignment in list context returns a list of references to each " "left-hand value. So for example" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "\\& @b = ((\\e$l1, \\e$l2, \\e(@a)) = (\\e$r1, \\e$r2, $\\er3, \\e$4));\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "is equivalent to" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& (\\e$l1, \\e$l2, \\e(@a)) = (\\e$r1, \\e$r2, $\\er3, \\e$4);\n" "\\& @b = (\\e$l1, \\e$l2, \\e$a[0], \\e$a[1]);\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "The CW<\\*(C`foreach\\*(C'> loop can also take a reference constructor for " "its loop variable, though the syntax is limited to one of the following, " "with an optional CW<\\*(C`my\\*(C'>, CW<\\*(C`state\\*(C'>, or " "CW<\\*(C`our\\*(C'> after the backslash:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& \\e$s\n" "\\& \\e@a\n" "\\& \\e%h\n" "\\& \\e&c\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "No parentheses are permitted. This feature is particularly useful for " "arrays-of-arrays, or arrays-of-hashes:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& foreach \\emy @a (@array_of_arrays) {\n" "\\& frobnicate($a[0], $a[-1]);\n" "\\& }\n" "\\&\n" "\\& foreach \\emy %h (@array_of_hashes) {\n" "\\& $h{gelastic}++ if $h{type} eq \\*(Aqfunny\\*(Aq;\n" "\\& }\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "\\&B Aliasing does not work correctly with closures. If you try to " "alias lexical variables from an inner subroutine or CW<\\*(C`eval\\*(C'>, " "the aliasing will only be visible within that inner sub, and will not affect " "the outer subroutine where the variables are declared. This bizarre " "behavior is subject to change." msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "Declaring a Reference to a Variable" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Beginning in v5.26.0, the referencing operator can come after " "CW<\\*(C`my\\*(C'>, \\&CW<\\*(C`state\\*(C'>, CW<\\*(C`our\\*(C'>, or " "CW<\\*(C`local\\*(C'>. This syntax must be enabled with CW<\\*(C`use " "feature \\*(Aqdeclared_refs\\*(Aq\\*(C'>. It is experimental, and will warn " "by default unless CW<\\*(C`no warnings " "\\*(Aqexperimental::refaliasing\\*(Aq\\*(C'> is in effect." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "This feature makes these:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& my \\e$x;\n" "\\& our \\e$y;\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "equivalent to:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& \\emy $x;\n" "\\& \\eour $x;\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "It is intended mainly for use in assignments to references (see \"Assigning " "to References\", above). It also allows the backslash to be used on just " "some items in a list of declared variables:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "\\& my ($foo, \\e@bar, \\e%baz); # equivalent to: my $foo, \\emy(@bar, %baz);\n" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "WARNING: Don't use references as hash keys" msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "reference, string context reference, use as hash key" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "You may not (usefully) use a reference as the key to a hash. It will be " "converted into a string:" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "\\& $x{ \\e$x } = $x;\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "If you try to dereference the key, it won't do a hard dereference, and you " "won't accomplish what you're attempting. You might want to do something " "more like" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "" "\\& $r = \\e@a;\n" "\\& $x{ $r } = $r;\n" msgstr "" #. type: Plain text #: debian-trixie debian-unstable msgid "" "And then at least you can use the B, which will be real refs, " "instead of the B, which won't." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "The standard Tie::RefHash module provides a convenient workaround to this." msgstr "" #. type: IX #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "SEE ALSO" msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "Besides the obvious documents, source code can be instructive. Some " "pathological examples of the use of references can be found in the I regression test in the Perl source directory." msgstr "" #. type: Plain text #: debian-trixie debian-unstable fedora-44 fedora-rawhide mageia-cauldron #: opensuse-leap-16-1 opensuse-tumbleweed msgid "" "See also perldsc and perllol for how to use references to create complex " "data structures, and perlootut and perlobj for how to use them to create " "objects." msgstr "" #. type: TH #: debian-unstable #, no-wrap msgid "2025-11-16" msgstr "" #. type: TH #: fedora-44 opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "2026-03-09" msgstr "" #. type: TH #: fedora-44 opensuse-leap-16-1 opensuse-tumbleweed #, no-wrap msgid "perl v5.42.1" msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "Symbolic references are names of variables or other objects, just as a " "symbolic link in a Unix filesystem contains merely the name of a file. The " "CW<*glob> notation is something of a symbolic reference. (Symbolic " "references are sometimes called \"soft references\", but please don\\*(Aqt " "call them that; references are confusing enough without useless synonyms.)" msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "References are easy to use in Perl. There is just one overriding principle: " "in general, Perl does no implicit referencing or dereferencing. When a " "scalar is holding a reference, it always behaves as a simple scalar. It " "doesn\\*(Aqt magically start being an array or hash or subroutine; you have " "to tell it explicitly to do so, by dereferencing it." msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "By using the backslash operator on a variable, subroutine, or value. (This " "works much like the & (address-of) operator in C.) This typically creates " "I reference to a variable, because there\\*(Aqs already a reference " "to the variable in the symbol table. But the symbol table reference might " "go away, and you\\*(Aqll still have the reference that the backslash " "returned. Here are some examples:" msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "It isn\\*(Aqt possible to create a true reference to an IO handle " "(filehandle or dirhandle) using the backslash operator. The most you can " "get is a reference to a typeglob, which is actually a complete symbol table " "entry. But see the explanation of the CW<*foo{THING}> syntax below. " "However, you can still use type globs and globrefs as though they were IO " "handles." msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "Here we\\*(Aqve created a reference to an anonymous array of three elements " "whose final element is itself a reference to another anonymous array of " "three elements. (The multidimensional syntax described later can be used to " "access this. For example, after the above, CW<\\*(C`$arrayref-E[2]" "[1]\\*(C'> would have the value \"b\".)" msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "Taking a reference to an enumerated list is not the same as using square " "brackets--instead it\\*(Aqs the same as creating a list of references!" msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "Because curly brackets (braces) are used for several other things including " "BLOCKs, you may occasionally have to disambiguate braces at the beginning of " "a statement by putting a CW<\\*(C`+\\*(C'> or a CW<\\*(C`return\\*(C'> in " "front so that Perl realizes the opening brace isn\\*(Aqt starting a BLOCK. " "The economy and mnemonic value of using curlies is deemed worth this " "occasional extra hassle." msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "Note the semicolon. Except for the code inside not being immediately " "executed, a CW<\\*(C`sub {}\\*(C'> is not so much a declaration as it is an " "operator, like CW<\\*(C`do{}\\*(C'> or CW<\\*(C`eval{}\\*(C'>. (However, no " "matter how many times you execute that particular line (unless you\\*(Aqre " "in an \\&CW), CW<$coderef> will still have a reference to the " "I anonymous subroutine.)" msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "Anonymous subroutines act as closures with respect to B variables, " "that is, variables lexically visible within the current scope. Closure is a " "notion out of the Lisp world that says if you define an anonymous function " "in a particular lexical context, it pretends to run in that context even " "when it\\*(Aqs called outside the context." msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "In human terms, it\\*(Aqs a funny way of passing arguments to a subroutine " "when you define it as well as when you call it. It\\*(Aqs useful for " "setting up little bits of code to run later, such as callbacks. You can " "even do object-oriented stuff with it, though Perl already provides a " "different mechanism to do that--see perlobj." msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "You might also think of closure as a way to write a subroutine template " "without using B. Here\\*(Aqs a small example of how closures work:" msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "Note particularly that CW<$x> continues to refer to the value passed into " "B I \"my CW<$x>\" having gone out of scope by the time " "the anonymous subroutine runs. That\\*(Aqs what a closure is all about." msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "References are often returned by special subroutines called constructors. " "Perl objects are just references to a special type of object that happens to " "know which package it\\*(Aqs associated with. Constructors are just special " "subroutines that know how to create that association. They do so by " "starting with an ordinary reference, and it remains an ordinary reference " "even while it\\*(Aqs also being an object. Constructors are often named " "CW. You I call them indirectly:" msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "But that can produce ambiguous syntax in certain cases, so it\\*(Aqs often " "better to use the direct method invocation approach:" msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "References of the appropriate type can spring into existence if you " "dereference them in a context that assumes they exist. Because we " "haven\\*(Aqt talked about dereferencing yet, we can\\*(Aqt show you any " "examples yet." msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "\\&CW<*foo{THING}> returns undef if that particular THING hasn\\*(Aqt been " "used yet, except in the case of scalars. CW<*foo{SCALAR}> returns a " "reference to an anonymous scalar if CW<$foo> hasn\\*(Aqt been used yet. " "This might change in a future release." msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "\\&CW<*foo{IO}> is an alternative to the CW<*HANDLE> mechanism given in " "\"Typeglobs and Filehandles\" in perldata for passing filehandles into or " "out of subroutines, or storing into larger data structures. Its " "disadvantage is that it won\\*(Aqt create a new filehandle for you. Its " "advantage is that you have less risk of clobbering more than you want to " "with a typeglob assignment. (It still conflates file and directory handles, " "though.) However, if you assign the incoming value to a scalar instead of a " "typeglob as we do in the examples below, there\\*(Aqs no risk of that " "happening." msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "That\\*(Aqs it for creating references. By now you\\*(Aqre probably dying " "to know how to use references to get back to your long-lost data. There are " "several basic methods." msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "Anywhere you\\*(Aqd put an identifier (or chain of identifiers) as part of a " "variable or subroutine name, you can replace the identifier with a simple " "scalar variable containing a reference of the correct type:" msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "It\\*(Aqs important to understand that we are specifically I " "dereferencing \\&CW<$arrayref[0]> or CW<$hashref{\"KEY\"}> there. The " "dereference of the scalar variable happens I it does any key " "lookups. Anything more complicated than a simple scalar variable must use " "methods 2 or 3 below. However, a \"simple scalar\" includes an identifier " "that itself uses method 1 recursively. Therefore, the following prints " "\"howdy\"." msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "Anywhere you\\*(Aqd put an identifier (or chain of identifiers) as part of a " "variable or subroutine name, you can replace the identifier with a BLOCK " "returning a reference of the correct type. In other words, the previous " "examples could be written like this:" msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "Admittedly, it\\*(Aqs a little silly to use the curlies in this case, but " "the BLOCK can contain any arbitrary expression, in particular, subscripted " "expressions:" msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "Because of being able to omit the curlies for the simple case of CW<$$x>, " "people often make the mistake of viewing the dereferencing symbols as proper " "operators, and wonder about their precedence. If they were, though, you " "could use parentheses instead of braces. That\\*(Aqs not the case. " "Consider the difference below; case 0 is a short-hand version of case 1, " "\\&I case 2:" msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "Case 2 is also deceptive in that you\\*(Aqre accessing a variable called " "CW<%hashref>, not dereferencing through CW<$hashref> to the hash it\\*(Aqs " "presumably referencing. That would be case 3." msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "This is one of the cases we mentioned earlier in which references could " "spring into existence when in an lvalue context. Before this statement, " "CW<$array[$x]> may have been undefined. If so, it\\*(Aqs automatically " "defined with a hash reference so that we can look up \\&CW<\\*(C`{\"foo\"}" "\\*(C'> in it. Likewise CW<\\*(C`$array[$x]-E{\"foo\"}\\*(C'> will " "automatically get defined with an array reference so that we can look up " "CW<\\*(C`[0]\\*(C'> in it. This process is called I." msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "Which, in the degenerate case of using only ordinary arrays, gives you " "multidimensional arrays just like C\\*(Aqs:" msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "Well, okay, not entirely like C\\*(Aqs arrays, actually. C doesn\\*(Aqt " "know how to grow its arrays on demand. Perl does." msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "If a reference happens to be a reference to an object, then there are " "probably methods to access the things referred to, and you should probably " "stick to those methods unless you\\*(Aqre in the class package that defines " "the object\\*(Aqs methods. In other words, be nice, and don\\*(Aqt violate " "the object\\*(Aqs encapsulation without a very good reason. Perl does not " "enforce encapsulation. We are not totalitarians here. We do expect some " "basic civility though." msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "Using a reference as a string produces both its referent\\*(Aqs type, " "including any package blessing as described in perlobj, as well as the " "numeric address expressed in hex. The B operator returns just the " "type of thing the reference is pointing to, without the address. See " "\"ref\" in perlfunc for details and examples of its use." msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "Here\\*(Aqs a trick for interpolating a subroutine call into a string:" msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "The way it works is that when the CW<\\*(C`@{...}\\*(C'> is seen in the " "double-quoted string, it\\*(Aqs evaluated as a block. The block creates a " "reference to an anonymous array containing the results of the call to " "CW<\\*(C`mysub(1,2,3)\\*(C'>. So the whole block returns a reference to an " "array, which is then dereferenced by CW<\\*(C`@{...}\\*(C'> and stuck into " "the double-quoted string. This chicanery is also useful for arbitrary " "expressions:" msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "Because objects in Perl are implemented as references, it\\*(Aqs possible to " "have circular references with objects as well. Imagine a TreeNode class " "where each node references its parent and child nodes. Any node with a " "parent will be part of a circular reference." msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "Here\\*(Aqs how we can make the first example safer:" msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "We said that references spring into existence as necessary if they are " "undefined, but we didn\\*(Aqt say what happens if a value used as a " "reference is already defined, but I a hard reference. If you " "use it as a reference, it\\*(Aqll be treated as a symbolic reference. That " "is, the value of the scalar is taken to be the I of a variable, rather " "than a direct link to a (possibly) anonymous value." msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "This is powerful, and slightly dangerous, in that it\\*(Aqs possible to " "intend (with the utmost sincerity) to use a hard reference, and accidentally " "use a symbolic reference instead. To protect against that, you can say" msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "Only package variables (globals, even if localized) are visible to symbolic " "references. Lexical variables (declared with B) aren\\*(Aqt in a " "symbol table, and thus are invisible to this mechanism. For example:" msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "will have the same effect. This construct is I considered to be a " "symbolic reference when you\\*(Aqre using strict refs:" msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "Pseudo-hashes have been removed from Perl. The fields pragma remains " "available." msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "As explained above, an anonymous function with access to the lexical " "variables visible when that function was compiled, creates a closure. It " "retains access to those variables even though it doesn\\*(Aqt get run until " "later, such as in a signal handler or a Tk callback." msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "The B and B functions would be similar. To create these, " "we\\*(Aqll assign a closure to a typeglob of the name of the function " "we\\*(Aqre trying to build." msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "Now all those different functions appear to exist independently. You can " "call B, B, B, B, B, etc. This " "technique saves on both compile time and memory use, and is less error-prone " "as well, since syntax checks happen at compile time. It\\*(Aqs critical " "that any variables in the anonymous subroutine be lexicals in order to " "create a proper closure. That\\*(Aqs the reasons for the CW<\\*(C`my\\*(C'> " "on the loop iteration variable." msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "Anonymous subroutines get to capture each time you execute the " "CW<\\*(C`sub\\*(C'> operator, as they are created on the fly. If you are " "accustomed to using nested subroutines in other programming languages with " "their own private variables, you\\*(Aqll have to work at it a bit in Perl. " "The intuitive coding of this type of thing incurs mysterious warnings about " "\"will not stay shared\" due to the reasons explained above. For example, " "this won\\*(Aqt work:" msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed #, no-wrap msgid "" "\\& $r = \\e@a;\n" "\\& @b = $r-E@*; # equivalent to @$r or @{ $r }\n" "\\&\n" "\\& $r = [ 1, [ 2, 3 ], 4 ];\n" "\\& $r-E[1]-E@*; # equivalent to @{ $r-E[1] }\n" "\\&\n" "\\& $aref-E@* = (1, 2, 3); # same as @{ $aref } = (1, 2, 3)\n" msgstr "" #. type: SH #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed #, no-wrap msgid "WARNING: Don\\*(Aqt use references as hash keys" msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "If you try to dereference the key, it won\\*(Aqt do a hard dereference, and " "you won\\*(Aqt accomplish what you\\*(Aqre attempting. You might want to do " "something more like" msgstr "" #. type: Plain text #: fedora-44 fedora-rawhide mageia-cauldron opensuse-leap-16-1 #: opensuse-tumbleweed msgid "" "And then at least you can use the B, which will be real refs, " "instead of the B, which won\\*(Aqt." msgstr "" #. type: TH #: fedora-rawhide #, no-wrap msgid "2026-03-30" msgstr "" #. type: TH #: fedora-rawhide #, no-wrap msgid "perl v5.42.2" msgstr "" #. type: TH #: mageia-cauldron #, no-wrap msgid "2025-06-24" msgstr "" #. type: TH #: mageia-cauldron #, no-wrap msgid "perl v5.42.0" msgstr ""