"be consistent" PerlMonks

### Re^2: shift vs @_

 on Oct 06, 2006 at 14:33 UTC ( #576684=note: print w/replies, xml ) Need Help??

in reply to Re: shift vs @_

I can't easily do my \$x, \$y = @_; It's valid perl. It assigns a scalar.

Only because the way you called my creates a scalar context. Try my( \$x, \$y ) = @_;, which creates a list context, and so produces the same result as my \$x = \$_[0]; my \$y = \$_[1];.

This works with any array or list. Check out the difference between:

``` \$x, \$y, \$z  = (2,3,4); # \$x = undef, \$y = undef, \$z = 4
(\$x, \$y, \$z) = (2,3,4); # \$x = 2,     \$y = 3,     \$z = 4

See, a list in scalar context will give its last element. An array in scalar context will give the number of its elements. However, if we make both sides of the assignment have list context, then elements get copied.

A collection of thoughts and links from the minds of geeks
The Code that can be seen is not the true Code
I haven't found a problem yet that can't be solved by a well-placed trebuchet

Replies are listed 'Best First'.
Re^3: shift vs @_
by exussum0 (Vicar) on Oct 06, 2006 at 14:39 UTC
Um, yeah. But it doesn't prevent me from being human and making a mistake. It's harder to get it wrong w/ a progression of shifts.

But it doesn't prevent me from being human and making a mistake.

Ok, firstly, you said that you can't easily do what you wanted, not that it was easy to make a mistake.

Secondly, attempting my \$x, \$y = @_ with strict in place (which is best practice anyhow) produces:

```Global symbol "\$y" requires explicit package name
Which would be a pretty big clue to me that something was wrong (you aren't creating a lexical \$y with my \$x, \$y). Doing the same thing with warnings turned on (another best practice) results in:
```Parentheses missing around "my" list

I'd say that pretty well prevents you from making this mistake, anyhow.

A collection of thoughts and links from the minds of geeks
The Code that can be seen is not the true Code
I haven't found a problem yet that can't be solved by a well-placed trebuchet
Frequently, I use strict and warnings, but there are times I don't. And sometimes prototypes become production code. They get copy pasted by some mad madness. It's possible to also do my \$x = @_, which will work w/ strict and warnings.

Unless I have a good reason, I prefer shifts. It's just a preference as defensive programming. 'cause one day, somehow a my \$x, \$y = @_, and someone will forget to use strict. And I'll be part blame for the assignment, someone else for the warnings and strictness.

Create A New User
Node Status?
node history
Node Type: note [id://576684]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2018-06-24 06:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should cpanminus be part of the standard Perl release?

Results (126 votes). Check out past polls.

Notices?