On Mon, Feb 2, 2009 at 16:43, Tina <tin...@gmail.com> wrote:
> Hello, I just started programming in perl and currently am working in
> an OOP sort of program. I am writing the constructor and I wasn't sure
> of the differences between the following syntax:
>
> 'count' => 0
>
> @count = 0
>
> Both seem to work fine, but I would like to know how they are
> different and whether or not one is better or more proper for
> programming in Perl.
>
> Can someone explain this to me?

The => operator* is called the fat-comma operator.  It can be used in
the same places a comma could be used.  Its normal usage is to denote
pairs of items to the programmer (Perl doesn't care).  For instance,
it is used here (along with some whitespace) to denote the key/value
relationship of the items being assigned to %hash.

my %hash = (
    key1 => "val1",
    key2 => "val2",
);

Compare this to the equivalent comma based code.

my %hash = (
    "key1", "val1",
    "key2", "val2",
);

Most find the first more aesthetically pleasing, but Perl doesn't
care; they both compile down to the same opcodes.  It has one special
property when compared with comma: it treats the thing on its left
like a string if the thing on the left matches this pattern
/^[-_a-zA-Z][-\w]*$/, so there is a difference in the compile-time
behaviour, but not the runtime behaviour.

The = operator** is called the assignment operator.  It puts a value
into a variable***:

my $scalar = 5;
my @array  = (1, 2, 3, 4, $scalar);

It is important to note that you should avoid saying

@count = 0;

For, while this may work with one value, the assignment operator has a
high precedence than the comma operator.  This means when you say

@count = 0, 1, 2, 3;

Perl sees this

(@count = 0), 1, 2, 3;

Almost always group items being assigned to an array:

@count = (0, 1, 2, 3);

Exceptions to this rule include copying one array into another and
assigning the results of one function to an array.

A final note, based nature of the assignment you wrote, it appears as
if you are not using the strict pragma.  The strict and warnings
pragmas are of great importance in modern Perl programming and should
always be used.  You can activate these pragmas by saying

use strict;
use warnings;

at the top of you program after the #! line.  You may find that you
get a lot of errors when you do this.  The errors strict is
complaining about are indeed errors even when strict is not in place;
it is just that Perl lets you get away with them.  This is roughly the
same as speeding when no cops are around.

* http://perldoc.perl.org/perlop.html#Comma-Operator
** http://perldoc.perl.org/perlop.html#Assignment-Operators
*** Well, really it puts an rvalue into an lvalue, but that is a more
complicated discussion

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to