chiburashka has asked for the wisdom of the Perl Monks concerning the following question:

What does this mean "my($data) = @_;" ?

20040519 Edit by castaway: Changed title from 'What does this mean ?'

20040519 Edit by castaway: Changed title from 'What does my () = @_'

Replies are listed 'Best First'.
Re: What does my ($data) = @_ mean?
by b10m (Vicar) on May 16, 2004 at 15:31 UTC

    It takes the first element of the array that's being passed to it. Example:

    #!/usr/bin/perl go ( "foo", "bar", "baz" ); sub go { my ($data) = @_; print $data; }

    This will print "foo".

    Update: Maybe this snippet from perldoc perlvar is interesting for you:

    @_      Within a subroutine the array @_ contains the
            parameters passed to that subroutine. See perl-
            sub.
    
    --
    b10m

    All code is usually tested, but rarely trusted.
Re: What does my ($data) = @_ mean?
by kvale (Monsignor) on May 16, 2004 at 15:31 UTC
    This assigns the first element of @_ to $data. It is the same as writing
    my $data = $_[0];
    but is easier to generalize should more parameters be added:
    my ($data1, $data2) = @_;

    -Mark

Re: What does my ($data) = @_ mean?
by tinita (Parson) on May 16, 2004 at 16:23 UTC
    additionally to the other comments and reading tips:
    i have the impression there are more basic things you might want to know. read the excellent perlintro and save yourself a lot of time.
Re: What does my ($data) = @_ mean?
by wolv (Pilgrim) on May 16, 2004 at 22:19 UTC
    I recommend that you get your hands on some good documentation. Perldoc ("perldoc perl" on your commandline/shell) will be always helpful. I also recommend Beginning Perl (available online), which is a really good book for Perl beginners.
      Had I posted this, I would have lost all my points! Anyway, here is the explaination:

      my = declare a variable.
      $data = variable name.
      () = similar to shift or pop.
      @_ = an array that you populate and send it off to a sub routine or a function.

      Example
      $var = 'some sort of data'; &My_Sub($var); sub My_Sub { my($data) = @_; Print $data . "\n"; } output: some sort of data
      Blackadder
Re: What does my ($data) = @_ mean?
by pbeckingham (Parson) on May 16, 2004 at 18:28 UTC

    Given that you know what it does, it means:

    Instantiate a scalar called $data, put it in a list of one element, then assign to that one-element list the contents of list @_. Because the left-hand side contains only one element, only one element (the first) is copied from @_.

Re: What does my ($data) = @_ mean?
by japhy (Canon) on May 16, 2004 at 15:33 UTC
    It means you don't understand Perl. What part of it doesn't make sense to you? The my()? The $data? The =? The @_?

    Try reading some documentation.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;
    A reply falls below the community's threshold of quality. You may see it by logging in.