Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

what is the meaning of ();

by Anonymous Monk
on Jul 25, 2008 at 23:12 UTC ( #700249=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have this expression in the code that I'm studying : my @incdirs = (); Could somebody tell me what it is? thanks, Anshu

Replies are listed 'Best First'.
Re: what is the meaning of ();
by runrig (Abbot) on Jul 25, 2008 at 23:17 UTC
    In this case, it's just a useless use of (). You could just do my @incdirs;, and accomplish the same thing (my theory is that it's common for new perl programmers to add the "= ()" when declaring an empty array). When you want to empty an existing array, it can be useful to do @incdirs = ();, without the my.

      It could also be cargo-cultism. I have seen coding style documents that require explicitly initializing all variables. No defaults allowed.

      Update: Bah. Rethinking this one. I understand and (strangly for me, normally opinionated) agree with both sides (could be cargo cult and initializing variable, even to the default, is good) on this one. I need to do some introspection on this to see if they are mutually exclusive. I am not certain that they are.


        Are you saying that following coding style documents is cargo-cult programming? I tend to think that one should follow coding guidelines, no matter how stupid. If they suck, change the guidelines or change your job.

        I'm sure you meant that it could be cargo-cult OR it could be adherence to a 'no defaults' style guideline/preference. The only way to know for sure would be to ask the original programmer why he wrote the code that way.

        Consider the following code:

        my $foo = undef;

        The initialization to undef totally unnecessary, but it does communicate an idea: $foo really should be undef, it's not just an oversight.

        While I don't use either unnecessary initialization, I don't see them as harmful.

        Despite my personal preferences, I can see why someone would choose to explicitly initialize ALL their variables. It's not a bad habit to be in.

        I guess the main thing I am trying to say is, don't be in too much of a rush to call something cargo-cult. What we have here could be CC code or it could be the result of a decision based on some painful lessons.

        TGI says moo

        IMO it's always a good idea to initialise your variables, even if you initialise them to the same value as what the language would have done in the first place. It's a good idea because it teaches you good habits for when you use a language which doesn't have sensible defaults, such as C.
Re: what is the meaning of ();
by martin (Friar) on Jul 25, 2008 at 23:23 UTC
    The array @incdirs is defined as a lexically scoped variable and initialized to be empty. The empty pair of parentheses mean an empty list here. Lexically scoped means this variable is visible only in the block where it is defined.
      Thanks Martin!

        Note that, as runrig implied, the = () is redundant. Arrays and hashes start out life empty - there is no need to explicitly empty them. Scalars on the other hand start out with the value undef. It is common to declare and initialise scalars at the same time and to do so immediately before the scalar's value is first used where possible (to reduce scope of the variable to a minimum).

        Perl reduces RSI - it saves typing
Re: what is the meaning of ();
by repellent (Priest) on Jul 27, 2008 at 08:31 UTC
    FYI, given the code:
    my (undef, $a, undef, $b, @rest, $c) = (1, 2, 3, 4, 5, 6, 7, 8);
    @rest will be greedily assigned (5, 6, 7, 8) whereas ($a, $b, $c) will be (2, 4, undef) respectively.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://700249]
Approved by planetscape
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2018-02-24 05:08 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (310 votes). Check out past polls.