### Difference between my (\$foo) & my \$foo

by sandy1028 (Sexton)
 on Feb 12, 2009 at 07:36 UTC Need Help??

sandy1028 has asked for the wisdom of the Perl Monks concerning the following question:

Difference between my(\$Files) = @_; and my \$Files = @_

Replies are listed 'Best First'.
Re: Difference between my (\$foo) & my \$foo
by GrandFather (Saint) on Feb 12, 2009 at 07:50 UTC
```my(\$Files) = @_;

List context.

```my \$Files = @_;

Scalar context.

True laziness is hard work
Re: Difference between my (\$foo) & my \$foo
by ELISHEVA (Prior) on Feb 12, 2009 at 08:52 UTC
Re: Difference between my (\$foo) & my \$foo
by Anonymous Monk on Feb 12, 2009 at 07:50 UTC
```\$ perl -MO=Deparse,-p -
my(\$Files) = @_;
my \$FilesR = @_;

^D
(my(\$Files) = @_);
(my \$FilesR = @_);
- syntax OK
Its list context versus scalar context, example
```
@_ = 0 .. 3;
my( \$one, \$two, \$three ) = @_;
print "one(\$one)two(\$two)three(\$three)\n";
( \$one, \$two, \$three ) = scalar @_;
print "one(\$one)two(\$two)three(\$three)\n";

__END__
one(0)two(1)three(2)
one(4)two()three()
Re: Difference between my (\$foo) & my \$foo
by planetscape (Chancellor) on Feb 14, 2009 at 05:03 UTC
Re: Difference between my (\$foo) & my \$foo
by repellent (Priest) on Feb 12, 2009 at 19:54 UTC
With:
```my (undef, \$a, undef, \$b, @rest, \$c) = (1, 2, 3, 4, 5, 6, 7, 8);

Now:
• @rest will be greedily assigned: @rest = (5, 6, 7, 8)
• the other variables are respectively assigned: (\$a, \$b, \$c) = (2, 4, undef)

Then:
```my \$count   = @rest; # 4 - because there are four items (scalar contex
+t)

my (\$first) = @rest; # 5 - because first item is 5 (list context)

But:
```my \$last = (11, 22, 33, 999); # 999 - because assigning LIST to scalar
+ only picks last item

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://743238]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2023-06-03 00:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How often do you go to conferences?

Results (6 votes). Check out past polls.

Notices?