From: Peng Yu on
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';
From: RedGrittyBrick on
On 04/06/2010 16:20, Peng Yu wrote:
> 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";

> }
>
> mysub 'a', 'b';


--
RGB
From: Peter J. Holzer on
On 2010-06-04 15:20, Peng Yu <pengyu.ut(a)gmail.com> wrote:
> 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