Solved

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

Posted on 1998-10-24
6
201 Views
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?
0
Comment
Question by:dda
  • 3
  • 3
6 Comments
 
LVL 4

Author Comment

by:dda
ID: 1205695
Thanks. It works.
0
 
LVL 84

Expert Comment

by:ozo
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")
0
 
LVL 4

Author Comment

by:dda
ID: 1205696
2ozo: Why do you leave this question unlocked? Are you on a vacation? :)
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 84

Expert Comment

by:ozo
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.
0
 
LVL 4

Author Comment

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

Accepted Solution

by:
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]
#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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
read PayflowPro reports using the report metadata using perl 11 40
Filtering a file to table 9 101
transpose into pipe delemited 8 67
Replace  text in a file 2 104
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now