Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>shift @{EXPR}</code> is valid syntax, so</p> <pre><code>shift @{$interfaces = [qx'ifconfig -s']} </code></pre> <p>will give you an array reference that has the first element removed.</p> <p>I discovered this from the <code>diagnostics</code> output about calling <code>shift</code> from list assignment:</p> <pre><code>$ perl -Mdiagnostics -e 'print shift(@a = (2,3,4))' </code></pre> <pre> Type of arg 1 to shift must be array (not list assignment) at -e line 1, at end of line Execution of -e aborted due to compilation errors (#1) (F) This function requires the argument in that position to be of a certain type. Arrays must be @NAME <b>or @{EXPR}</b>. Hashes must be %NAME or %{EXPR}. No implicit dereferencing is allowed--use the {EXPR} forms as an explicit dereference. See perlref. </pre> <hr> <p>Perl enforces this behavior on any user-defined subroutine or builtin that is prototyped with <code>\@</code> or <code>\%</code> characters. The prototype is a clue for the interpreter that Perl should treat an array or hash function argument as an array or hash type, and not try to unroll the list into several arguments.</p> <p>One way to think about it (though I'm not sure if this is accurate for the builtins) is that Perl will read the array or hash variable from your list of arguments to a function call, but actually pass a reference to that variable to the prototyped function. So the interpreter needs to identify an array or hash in your argument list, and it needs to be able to get a reference to that array or hash. Perl doesn't or can't (hands waving here) do that with a list assignment expression -- note that the result of</p> <pre><code>\(@a = (1,2,3)) </code></pre> <p>is a list of 3 references to scalars, not a reference to a list with 3 scalars.</p> <p>You can see the prototypes (if any) for most of the Perl builtins with the <code>prototype</code> function:</p> <pre><code>$ perl -e 'print prototype("CORE::shift")' ===&gt; \@ $ perl -e 'print prototype("CORE::each")' ===&gt; \% $ perl -e 'print prototype("CORE::push")' ===&gt; \@@ </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload