--- name: tied signature: 'tied VARIABLE' since: 5.0 status: documented categories: ["Classes and OO"] --- ```{index} single: tied; Perl built-in ``` *[Classes and OO](../perlfunc-by-category)* # tied Return the object backing a tied variable. `tied` hands you the same reference that [`tie`](tie) originally returned when `VARIABLE` was bound to a class. Through that reference you can call methods directly on the tie implementation — bypassing the usual [`FETCH`](tie) / [`STORE`](tie) dispatch — which is how tie classes expose extra operations that have no natural scalar / array / hash syntax. If `VARIABLE` is not tied, `tied` returns [`undef`](undef). ## Synopsis ```perl tied VARIABLE ``` ## What you get back A reference to the underlying tie object when `VARIABLE` is tied, and [`undef`](undef) when it is not. The reference is the exact value originally returned by the matching [`tie`](tie) call — same class, same blessed identity — so `ref(tied $v)` names the tie class and method calls on it dispatch normally: ```perl my $obj = tied %hash; $obj->flush if defined $obj; ``` Use `defined` (or a boolean test, when the tie class never blesses into a false-overloading package) to distinguish a tied variable from an untied one before calling methods on the result. ## The "tie handle" trick `tied` is the only supported way to talk to the tie implementation directly. Any method on the tie class that is not one of the standard hooks ([`FETCH`](tie), [`STORE`](tie), [`EXISTS`](tie), [`DELETE`](tie), [`CLEAR`](tie), [`FIRSTKEY`](tie), [`NEXTKEY`](tie), and their array / scalar / handle equivalents) is reachable only through the object `tied` returns: ```perl tie my %cache, 'My::LRU', size => 1024; tied(%cache)->stats; # class-specific method, no hash syntax tied(%cache)->evict_older_than(3600); ``` This is also the pattern for introspection — asking a tied variable "who are you tied to?" without assuming knowledge of the binding site: ```perl my $class = ref tied @array; # e.g. "Tie::File" ``` ## Examples Basic test for tiedness: ```perl tie my %h, 'My::Tied::Hash'; print defined(tied %h) ? "tied\n" : "not tied\n"; # tied ``` Calling a class-specific method through the tie object: ```perl tie my @log, 'Tie::File', 'app.log' or die $!; tied(@log)->flock(2); # LOCK_EX; Tie::File API # ... mutate @log ... tied(@log)->flock(8); # LOCK_UN ``` A plain scalar, array, hash, or filehandle that was never tied: ```perl my $x; print defined(tied $x) ? "yes" : "no", "\n"; # no ``` `tied` does **not** untie — the variable stays tied after the call, and successive calls return the same reference: ```perl tie my $t, 'My::Counter'; my $a = tied $t; my $b = tied $t; print $a == $b ? "same\n" : "different\n"; # same ``` Round-tripping through [`ref`](ref) to learn the tie class: ```perl tie my %db, 'DB_File', 'data.db', O_RDWR | O_CREAT, 0644; print ref tied %db, "\n"; # DB_File ``` ## Edge cases - **Not tied**: returns [`undef`](undef). No warning, no exception. This is the idiomatic way to check whether a variable is tied at all — there is no separate `is_tied` built-in. - **Filehandles**: `tied *FH` returns the object for a tied filehandle (bound with `tie *FH, ...`). A glob that was never tied yields [`undef`](undef) just like any other untied variable. - **`tied` does not untie**. It is a pure accessor. To release the binding, call [`untie`](untie). Holding the reference returned by `tied` past the point where the variable is untied keeps the tie object alive, which is deliberate — that is how [`untie`](untie) warnings about "untie attempted while N inner references still exist" are triggered. - **Tied element of an aggregate**: when an individual array or hash element is itself tied (separate from a tie on the container), `tied $array[0]` or `tied $hash{key}` returns the per-element object, not the container's. A container tie and an element tie are independent bindings. - **Copied values are not tied**. `my $copy = $tied_scalar;` stringifies through [`FETCH`](tie); `$copy` is a plain scalar and `tied $copy` returns [`undef`](undef). Tie is a property of the variable, not of the value. - **References to tied variables preserve the tie.** `\%hash` where `%hash` is tied still refers to the tied container, so `tied %{ $ref }` returns the same tie object. ## Differences from upstream Fully compatible with upstream Perl 5.42. ## See also - [`tie`](tie) — bind a variable to a class; establishes the tie relationship that `tied` queries - [`untie`](untie) — release the tie; after calling it, `tied` on the same variable returns [`undef`](undef) - [`ref`](ref) — get the class name of the object returned by `tied` without a separate method call - [`bless`](bless) — `tied` returns a blessed reference when the tie class blessed the object it returned from its `TIE*` constructor - [`defined`](defined) — the standard test for "is this variable tied?" is `defined tied $var`