Why does "$v = @ARGV[0]" work ?

Why does "$v = @ARGV[0]" work ?

Post by rlf » Sun, 06 Sep 2009 20:43:45


I've taken over some perl code that starts out with :

$v = @ARGV[0]

That's an @ sign on ARGV, not a dollar sign.

This seems to get the first element OK, but I don' know why. I've
never seen this construct used before, and I cannot find a parser rule
that says that you could use EITHER a $ or @ in this context.

Am curious to know why the result of this parse gets the same action
as taking the scalar with

$v = $ARGV[0]
 
 
 

Why does "$v = @ARGV[0]" work ?

Post by Peter Makh » Sun, 06 Sep 2009 21:35:51

rlf < XXXX@XXXXX.COM > writes:


@ARGV[0] makes an array slice with a single element. It is explained
in 'perldoc perldata' (look for the heading 'Slices'). I'm not really
sure if a single element slice has any usable function, but they are
allowed.

//Makholm

 
 
 

Why does "$v = @ARGV[0]" work ?

Post by Don Pive » Sun, 06 Sep 2009 21:45:44


@ARGV[0] is a one-element slice out of the @ARGV array, which is then
used as a list constructor for the assignment. Now, man perldata says
that "[in] a context not requiring a list value, the value of what
appears to be a list literal is simply the value of the final element",
working the same as C's and Perl's comma operator. More generally,
evaluating a list constructor in scalar context yields you that
last-element behavior, while evaluating a list VARIABLE in scalar
context gets you the length of the list.

Since the assignment is in scalar context, you get the last element of
the list constructor. For example:

@list = ('foo', 'bar', 'krunk', 'quux') ;
$a = $list[0] ; # sets $a to 'foo' as expected
$b = @list[0] ; # slices ('foo') out of @list and
# assigns last element to $b
$c = @list[2] ; # sets $c to 'krunk'

... which then leads to ...

@d = @list[1,3] ; # sets @d to ('bar', 'krunk', 'quux')
$e = @list[1,3] ; # sets $e to 'quux'

Don
 
 
 

Why does "$v = @ARGV[0]" work ?

Post by Ben Morro » Mon, 07 Sep 2009 00:42:00


Quoth Don Piven < XXXX@XXXXX.COM >:


It's worth noting that 'use warnings' will warn about this. If you have
inherited some code with dodgy coding standards, my recommendation would
be to start by adding

use strict;
use warnings;

at the top and fixing all the resultant diagnostics.

<snip>

An array. The difference between list and array is important.


Ben
 
 
 

Why does "$v = @ARGV[0]" work ?

Post by rlf » Mon, 07 Sep 2009 22:50:37


> use strict;> > use warning>; >>
> at the top and fixing all the resultant diagnostic>. >>< > <>ni>> >> >> >>> > > @ARGV[0] is a one-element slice out of the @ARGV array, which is th>n> > > used as a list constructor for the assignment. ow, man perldata sa>s> > > that "[in] a context not requiring a list value, the value of wh>t> > > appears to be a list literal is simply the value of the final element>,> > > working the same as C's and Perl's comma operator. ore generall>,> > > evaluating a list constructor in scalar context yields you th>t> > > last-element behavior, while evaluating a list VARIAB>E >>
> An array. The difference between list and array is importan>. >>> > > in scal>r> > > context gets you the length of the lis>. >>
> Ben

Thank you all for the clarification.
 
 
 

Why does "$v = @ARGV[0]" work ?

Post by Nathan Kee » Wed, 09 Sep 2009 04:19:34


It's called a slice. [0] is the first element. Not the ideal way to
write it.
 
 
 

Why does "$v = @ARGV[0]" work ?

Post by Uri Guttma » Wed, 09 Sep 2009 07:55:43

>>>>> "NK" == Nathan Keel < XXXX@XXXXX.COM > writes:


>> I've taken over some perl code that starts out with :
>>
>> $v = @ARGV[0]
>>
>> That's an @ sign on ARGV, not a dollar sign.

NK> It's called a slice. [0] is the first element. Not the ideal way to
NK> write it.

how helpful. at least tell him how to do it correctly. oh, you don't
help, you just snark on those who do. and it was already correctly
answered 2 days ago.

uri

--
Uri Guttman ------ XXXX@XXXXX.COM -------- http://www.yqcomputer.com/ --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://www.yqcomputer.com/ ---------
 
 
 

Why does "$v = @ARGV[0]" work ?

Post by Nathan Kee » Wed, 09 Sep 2009 08:33:08


Good thing you offered your "help" with _your_ "snarky" remark. You are
still a *** , I see.


I did no such thing.


So what? I don't have my news reader set the same way you do, and a lot
of people don't, which is why there are usually multiple replies to the
same question. Of course, you "have it out" for me (because I don't
put up with your ego bullshit here), so you'd only try and call me on
the same thing everyone else does (including you). What a weak debater
you are.

nal cake.

Uri, at least write your own name as a proper noun, if you want to think
you're as smart as you actually aren't. Now, *** off, troll boy.
 
 
 

Why does "$v = @ARGV[0]" work ?

Post by Uri Guttma » Wed, 09 Sep 2009 08:38:20

>>>>> "NK" == Nathan Keel < XXXX@XXXXX.COM > writes:


>>>>>>> "NK" == Nathan Keel < XXXX@XXXXX.COM > writes:
>>

>> >> I've taken over some perl code that starts out with :
>> >>
>> >> $v = @ARGV[0]
>> >>
>> >> That's an @ sign on ARGV, not a dollar sign.
>>
NK> It's called a slice. [0] is the first element. Not the ideal
>> way to NK> write it.
>>
>> how helpful. at least tell him how to do it correctly.

NK> Good thing you offered your "help" with _your_ "snarky" remark. You are
NK> still a *** , I see.

nope, i saw the correct answers and didn't need to add more. you added a
useless answer. and you still can't refrain from vulgarity. your momma
didn't raise you too well.

>> and it was already correctly
>> answered 2 days ago.

NK> So what? I don't have my news reader set the same way you do, and a lot
NK> of people don't, which is why there are usually multiple replies to the
NK> same question. Of course, you "have it out" for me (because I don't
NK> put up with your ego bullshit here), so you'd only try and call me on
NK> the same thing everyone else does (including you). What a weak debater
NK> you are.

2 days is long even on usenet time. but you don't care about helping as
we have seen. snark away!

>> uri
NK> nal cake.

how high school of you. now grow up and graduate kindergarten already.

NK> Uri, at least write your own name as a proper noun, if you want to think
NK> you're as smart as you actually aren't. Now, *** off, troll boy.

you don't even know what my name means, its origins or history. snarky
snark snark.

as for brains, your low educational tone and repeated use of the same
kiddie insults settles the matter in your case. the best punishment for
you is being you.

uri

--
Uri Guttman ------ XXXX@XXXXX.COM -------- http://www.yqcomputer.com/ --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://www.yqcomputer.com/ ---------
 
 
 

Why does "$v = @ARGV[0]" work ?

Post by Nathan Kee » Wed, 09 Sep 2009 09:52:08

ri Guttman wrote:


Perhaps in this case you did so, but in a lot of threads you do not.
Like I said, I don't have the posts show the same way you do, and so
what? Why don't you give grief to everyone else that also reply to all
of the threads here daily, even after other people do? Why? Because
you're just flipped out that I didn't put up with your ego bullshit in
another thread a while ago. You're just an arrogant worm and you need
to get over it.


It wasn't useless. You are useless. Do you think your arrogant attacks
on people are helpful? Or more helpful than a redundant answer? You
actually probably do think your arrogant, pointless and unhelpful and
uncivil interactions are more purposeful. Sad.


Not when you're acting like a worthless prick. Treat people with
civility and you'll find you get a different reaction.


And apparently yours did a great job with how arrogant and bullyish you
try and be?


Irrelevant.


I can say the same about you. You are more interested in attacking
people with your snarky arrogance. Grow up.


Well, that's pretty much about the value I place on you, to be honest.


Yeah, because _you_ are acting real mature yourself there, buddy.
Sadly, you really think you don't have to follow your own advice.
You've got serious emotional problems to think your own rules don't
apply to you and that you can try and bully people on "usenet" of all
places. What a fucking coward and a clown you are.


I actually do (and really, what difference would it make if I didn't?),
and it's a proper noun, so what I said still applies. Honestly, you
think you aren't snarky? You're acting like a complete bitch on this
group all of the time.


Yes, I know you think that whatever you "proclaim" about people you
personally decided to have a problem with, somehow makes it a "fact" to
your arrogant mind. This is not surprising. You are irrelevant and
stupid.


Funny, I was just thinking the same thing about you. Now, if only you
could practice what you preach, you'd maybe not come off looking like a
completely arrogant asshole without any class. And be honest, before I
ever referred to you in the accurately portrayed vulgar term(s), you
were already pulling and saying this same bullshit anyway. You can't
be friendly, because you're that much of a prick. You're just going to
have to deal with the fact that you can't "push" me around on usenet,
and how cowardly are you anyway? Go back to your bubble, you fucking
fruitcake.

nal cake.

 
 
 

Why does "$v = @ARGV[0]" work ?

Post by Nathan Kee » Wed, 09 Sep 2009 09:55:30


PS: Congrats on making yet another thread all about you, Uri. What a
trooper you are!
 
 
 

Why does "$v = @ARGV[0]" work ?

Post by Uri Guttma » Wed, 09 Sep 2009 10:07:32

>>>>> "NK" == Nathan Keel < XXXX@XXXXX.COM > writes:


>>
>> uri

NK> PS: Congrats on making yet another thread all about you, Uri. What a
NK> trooper you are!

pot meet kettle. you started it! nyah nyah nyah!! so's your old man!
your momma codes in python!! your sister is a lisper!!

uri

--
Uri Guttman ------ XXXX@XXXXX.COM -------- http://www.yqcomputer.com/ --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://www.yqcomputer.com/ ---------
 
 
 

Why does "$v = @ARGV[0]" work ?

Post by Nathan Kee » Wed, 09 Sep 2009 10:31:47


You think? Wow, I have all sorts of "emotional ailments" for not
putting up with your arrogant bullshit. You make me laugh. You think
this BS is contributing? I posted a response to the OP, I stated it
was a slice and not the best way to do it (they asked what it was).
You reply attacking me and you think that I am the problem?


What a retard you are. I can't tell if you were saying I said "you"
"started it", or if you were saying I did (but I didn't, if you want to
make that claim) Your English is awful, and so is your attitude. You
might not like me because I don't think you're anyone special, but your
actions here are going to just annoy other members of the group. If I
attack you and you defend yourself, that's one thing, but I've not
interacted with you in a while. You're just a worthless person, you
should end your life.


Pardon? Your grammar is atrocious, and it's difficult to parse what
you're saying. Anyway, your attitude is misplaced. You can't fault
someone simply because they didn't allow you to push them around...
well, maybe YOU can (and do). Get a life or find a better reason to
make an "enemy" of someone you don't even know.


Okay.

nal cake.

You're such a coward.
 
 
 

Why does "$v = @ARGV[0]" work ?

Post by Uri Guttma » Wed, 09 Sep 2009 12:35:23

>>>>> "NK" == Nathan Keel < XXXX@XXXXX.COM > writes:


>>>>>>> "NK" == Nathan Keel < XXXX@XXXXX.COM > writes:
>>

>> >>
>> >> uri
>>
NK> PS: Congrats on making yet another thread all about you, Uri.
>> What a NK> trooper you are!
>>
>> pot meet kettle.

NK> You think? Wow, I have all sorts of "emotional ailments" for not
NK> putting up with your arrogant bullshit. You make me laugh. You think
NK> this BS is contributing? I posted a response to the OP, I stated it
NK> was a slice and not the best way to do it (they asked what it was).
NK> You reply attacking me and you think that I am the problem?

your answer was useless. you seem to think you are helping others. that
is the core issue. stop thinking you are helping. you don't know perl
nor how to teach it.

>> uri
NK> nal cake.

NK> You're such a coward.

coward? hehe. show some of your code for review. check out mine and
slash away. cowardice is commenting without showing work. you don't know
from code nor perl.

uri

--
Uri Guttman ------ XXXX@XXXXX.COM -------- http://www.yqcomputer.com/ --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://www.yqcomputer.com/ ---------
 
 
 

Why does "$v = @ARGV[0]" work ?

Post by Nathan Kee » Fri, 11 Sep 2009 03:47:13


Ugh, *you* are useless. Move on and bug someone else.