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

I have following code
#!/usr/bin/perl $startDir = q{c:\test}; $newDir = q{c:\new}; &myReadDir($startDir); exit 0; sub myReadDir { my ($dir) = @_; my (@dirs,$list); opendir(DIR,$dir) || warn "can't open the directory $dir: $!\n +"; @dirs=grep {!(/^\./) && -d "$dir\\$_"} readdir(DIR); closedir (DIR); for $list(0..$#dirs) { mkdir ($newDir) unless -d $newDir; $ndir = $dir; $ndir =~ s/c:\\test/c:\\new/; print $ndir."\\".$dirs[$list],"\n"; mkdir ($ndir."\\".$dirs[$list]); &myReadDir($dir."\\".$dirs[$list]); } return 1; }
&myReadDir($startDir);
what does & mean in this line of code ? If I want to use subroutine, I just need to use it as myReadDir($startDir). What is "&" used for as prefix ?

my ($dir) = @_;
Why is variable $dir surrounded by () ?

my (@dirs,$list);
What does this code do ? Usually we declare list in bracket on right hand side and that contains values. What does this code do ? Where can I find more details about this code ?

Replies are listed 'Best First'.
Re: please explain some code
by ikegami (Patriarch) on Jul 18, 2011 at 08:40 UTC

    what does & mean in this line of code ?

    To Perl, it means "ignore the prototype". To some people, it seems to mean something else I don't know.

    Why is variable $dir surrounded by () ?

    my $dir = @_; would assign the number of elements in @_ to $dir.

    What does [my (@dirs,$list);] do ?

    A short way of writing

    my @dirs; my $list;

    Usually we declare list in bracket on right hand side and that contains values.

    This makes no sense. For starters, you can't declare lists since they're not variables.

    Where can I find more details about this code ?

    my


    A question of my own: Why do you add a space in front of every "?"?

      whats the difference between "my $dir = @_" and "my ($dir) = @_" ?
      If both mean same thing, then why use () around $dir in first case ?
        "my $dir = @_" is scalar context, so $dir will contain number of elements in @_
        "my ($dir) = @_" is list context, so $dir will contain first value of @_ (if exists)
Re: please explain some code
by jethro (Monsignor) on Jul 18, 2011 at 10:53 UTC

    '&' is a deprecated (or just old-fashioned) form of calling a subroutine.

    $dir is surrounded by () to force list context. Read about scalar and list context in perldata or google for 'perl context'

      It's not deprecated and it has a purpose. As previously stated, it causes the prototype of the sub to be ignored.
      It is also useful in cases where you are using in built perl keywords for your subroutine names:

      sort(); #wrong as sort is inbuilt keyword

      &sort(); #correct..ignores sort as keyword

Re: please explain some code
by Anonymous Monk on Jul 18, 2011 at 11:38 UTC

    If you want to declare more than one my variable at one time, the list of variables must be enclosed in parentheses.   Many Perl coders get into the habit of always putting the list in parentheses, even if it contains only one entry.

    The statement, my (@dirs, $list); could have been written as two statements.