Re: newbie with a question on syntax

2009-02-04 Thread Chas. Owens
On Tue, Feb 3, 2009 at 18:32, Dr.Ruud  wrote:
> Chas. Owens wrote:
>
>> [fat comma]
>>
>> it treats the thing on its left
>> like a string if the thing on the left matches this pattern
>> /^[-_a-zA-Z][-\w]*$/
>
> I don't believe that pattern. So lets test:
>
>
> perl -wle '
>  $,=" => ";
>  my %h = ( -- => "x");
>  print %h;
> '
> syntax error at -e line 3, near "-- =>"
> Execution of -e aborted due to compilation errors.
>
> Probably you meant the second character class to be just \w?
> Even then the pattern is not right:
>
>
> perl -wle '
>  $,=" => ";
>  my %h = ( 1.23 => "x");
>  print %h;
> '
> 1.23 => x

Yep, the Perl interpreter in my head is buggy again.


-- 
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/




Re: newbie with a question on syntax

2009-02-04 Thread Dr.Ruud

Chas. Owens wrote:

> [fat comma]

it treats the thing on its left
like a string if the thing on the left matches this pattern
/^[-_a-zA-Z][-\w]*$/


I don't believe that pattern. So lets test:


perl -wle '
  $,=" => ";
  my %h = ( -- => "x");
  print %h;
'
syntax error at -e line 3, near "-- =>"
Execution of -e aborted due to compilation errors.

Probably you meant the second character class to be just \w?
Even then the pattern is not right:


perl -wle '
  $,=" => ";
  my %h = ( 1.23 => "x");
  print %h;
'
1.23 => x

--
Ruud

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




Re: newbie with a question on syntax

2009-02-03 Thread Chas. Owens
On Mon, Feb 2, 2009 at 16:43, Tina  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/




newbie with a question on syntax

2009-02-03 Thread Tina
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?


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