"my ($var) = $ARGV[0]" vs "my ($var); $var = $ARGV[0]"

The following doesn't work if I ran script without parameters:

# doesn't work!
my ($login) = $ARGV[0] or die "$0: login parameter not set!\n";

And if I modify the text:

# it works
my ($login);
$login = $ARGV[0] or die "$0: login parameter not set!\n";

- everything is ok.

What is the difference? When it is possible to use my() in assignments?
LVL 4
ddaAsked:
Who is Participating?
 
ozoCommented:
 my ($login)
#is a list

  my $login
#is a scalar

# or evaluates its left argument as a scalar
#the scalar value of the assignment
  $login = $ARGV[0]
#or
  my $login = $ARGV[0]
#is the value assigned

#the scalar value of the assignment
  ($login) = $ARGV[0]
#or
  my ($login) = $ARGV[0]
#or
 (my $login) = $ARGV[0]
#is the number of values assigned
0
 
ddaAuthor Commented:
Thanks. It works.
0
 
ozoCommented:
#my ($login) is a list, and a list assignment returns its length.  try:
my $login = $ARGV[0] or die "$0: login parameter not set!\n";

#(but note that it also dies if you give the parameter "0")
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
ddaAuthor Commented:
2ozo: Why do you leave this question unlocked? Are you on a vacation? :)
0
 
ozoCommented:
I wanted to wait for any followup questions.
The difference is not in the my.  it's the difference between a list assignment and a scalar assignment.
 ($login) = $ARGV[0] or die "$0: login parameter not set!\n";
will do the same thing as your first example.
0
 
ddaAuthor Commented:
Ok, I'm getting more familiar with all this stuff, thanks. Please take your points. Your assistance is greatly appreciated.
Dmitry.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.