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

Posted on 1998-10-24
Last Modified: 2010-03-05
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?
Question by:dda
  • 3
  • 3

Author Comment

ID: 1205695
Thanks. It works.
LVL 84

Expert Comment

ID: 1205694
#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")

Author Comment

ID: 1205696
2ozo: Why do you leave this question unlocked? Are you on a vacation? :)
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 84

Expert Comment

ID: 1205697
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.

Author Comment

ID: 1205698
Ok, I'm getting more familiar with all this stuff, thanks. Please take your points. Your assistance is greatly appreciated.
LVL 84

Accepted Solution

ozo earned 50 total points
ID: 1205699
 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]
  my $login = $ARGV[0]
#is the value assigned

#the scalar value of the assignment
  ($login) = $ARGV[0]
  my ($login) = $ARGV[0]
 (my $login) = $ARGV[0]
#is the number of values assigned

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Remove Malware code from PHP file 6 98
html form to write data to csv 24 160
Able to retrieve only 1 row through email amongst multiple rows 3 75
Perl Snippet to Parse String 1 34
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question