I wrote this code in perl:
shift( @interfaces = qx'ifconfig -s' );
And got this error:
Type of arg 1 to shift must be array (not list assignment)
When I write it this way:
@interfaces = qx'ifconfig -s';
shift @interfaces;
It does what I want, which is to get the output of the ifconfig command as an array of lines and remove the first element in the array (which is a header, not an actual interface).
My personal preference is to write this as a one liner. It seems to me the parentheses in the first example should cause the assignment to be resolved fully, therefore allowing shift to see @interfaces as an array, but clearly perl thinks it’s a “list assignment.”
This is surely an easy question for the perl gurus but I’ve googled and googled and haven’t found enlightenment.
If someone would please provide the specific semantics to accomplish what I want in one line I would appreciate it. If you would also please take the time to explain why my first version isn’t working I would be eternally grateful (teach a man to fish and all that).
Thank you in advance for your help.
As you saw,
shiftrequires a literal array, not the result of an assignment. This is because when perl parsesshift @interfacesit is actually rewriting it into something like&CORE::shift(\@interfaces)and you can not take a reference of an assignment and get an array ref.You could break it into two lines as you have found, you could hide the assignment inside a bracketed dereference as mob shows, or you could simply throw away the first value:
undefin an lvalue position is a placeholder for values that you don’t need.(parsing of
shifthas changed a bit in perl 5.14+, but the argument above still holds)a few more ways that you probably shouldn’t use, ordered only by increasing length 🙂