The usage of $_[0]

Discussion in 'Perl Misc' started by Peng Yu, Jun 4, 2010.

  1. Peng Yu

    Peng Yu Guest

    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';
    Peng Yu, Jun 4, 2010
    #1
    1. Advertising

  2. 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
    RedGrittyBrick, Jun 4, 2010
    #2
    1. Advertising

  3. On 2010-06-04 15: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
    >


    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
    Peter J. Holzer, Jun 5, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. metfan
    Replies:
    2
    Views:
    4,833
    Robert Olofsson
    Oct 21, 2003
  2. Colin J. Williams

    Webchecker Usage - a problem with local usage

    Colin J. Williams, Feb 25, 2004, in forum: Python
    Replies:
    1
    Views:
    520
    Colin J. Williams
    Feb 26, 2004
  3. hvt
    Replies:
    0
    Views:
    1,192
  4. hvt
    Replies:
    0
    Views:
    1,449
  5. Krist
    Replies:
    8
    Views:
    6,339
    Arne Vajhøj
    Feb 10, 2010
Loading...

Share This Page