The usage of $_[0]

P

Peng Yu

I think that $_[0] should give me 'a' in the following example. But it
doesn't. Could you help understand why? How to get the first argument?

$ ./main.pl
ab
b

$ cat main.pl
#!/usr/bin/env perl

use strict;
use warnings;

sub mysub {
print @_, "\n";
print $_[0], print $_[1], "\n";
}

mysub 'a', 'b';
 
R

RedGrittyBrick

I think that $_[0] should give me 'a' in the following example. But it
doesn't. Could you help understand why? How to get the first argument?

$ ./main.pl
ab
b

$ cat main.pl
#!/usr/bin/env perl

use strict;
use warnings;

sub mysub {
print @_, "\n";
print $_[0], print $_[1], "\n";

print $_[0], $_[1], "\n";
 
P

Peter J. Holzer

I think that $_[0] should give me 'a' in the following example. But it
doesn't. Could you help understand why? How to get the first argument?

$ ./main.pl
ab
b

That's not true. Your program prints

ab <newline>
b <newline>
a1 <no newline here>

(The shell prompt may obscure the last line due to the missing newline,
but in any case there is no empty line after "b")

As an exercise, figure out why
print $_[0], print $_[1], "\n";
}

prints "b\na1".

hp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top