newbie syntax question -> and =>

T

ToddAndMargo

Hi All,

I am a bit new (okay A LOT NEW) to Perl. My
background is mostly Modula2 and Bash script.

Looking over an example, I notices "->" and "=>"
being used. For example:

my($sock) = IO::Socket::INET->new(PeerAddr => $host,
PeerPort => 'exec(512)', Proto => 'tcp');
and
$sock->syswrite("0\0", 2);
$sock->syswrite($user . "\0", length($user) + 1);

What are they doing and what is are their rules?

Many thanks,
-T
 
T

ToddAndMargo

perldoc perlop

Thank you. I think i almost get it. "->" and
"=>" are the same thing but "->" has a
higher precedence than does "=>".

-T

big, big change over Modula2! I will learn
Perl if it kills me! (Well, may not kills me.)
 
U

usenet

Thank you. I think i almost get it. "->" and
"=>" are the same thing but "->" has a
higher precedence than does "=>".

In the example you gave, "=>" is really like a comma in an array
definition (it's called a 'fat comma'). You invoked the "new" method
(via the "->") of the INET module, and passed it a hash (an anonymous
hash, but a hash) of key/value pairs (using the fat comma syntax). You
could have constructed the hash another way if you had preferred and
passed that instead.
 
H

Henry Law

Thank you. I think i almost get it. "->" and
"=>" are the same thing but "->" has a
higher precedence than does "=>".

I'm not tremendously expert in Perl yet but I have to say I don't think
this is right. Quoting from perlop, and adding a bit of explanation for
you:
the => operator is a synonym for the comma, but ...

It's particularly used when setting up a hash, like this

my %outfit = (trousers=>"Blue", shirt=>"White", shoes=>"Brown");

which is the same as

my %outfit = ("trousers", "Blue", "shirt", "White", "shoes", "Brown");

but easier to type and understand.

Whereas
"->" is an infix dereference operator, just as it is in C and

I can't relate this to Modula2 and I know it doesn't exist in bash, but
it's rather like a pointer in C. If I create a reference to the hash above

my $ref_to_outfit = \%outfit;

.... that reference is a bit like a pointer and to get the stuff "at the
end of it" you need to dereference it. The -> operator is one way to do
that (TMTOWTDI, remember). To print out the colour of the trousers
you'd code

print "Trousers are $ref_to_outfit->{trousers}\n";
 
T

Tad McClellan

Please do not quote signatures. It is bad netiquette.

Have you seen the Posting Guidelines that are posted here frequently?

I think i almost get it.


You are not yet thinking correctly.

"->" and
"=>" are the same thing but "->" has a
higher precedence than does "=>".


You need to read further than just the precedence table.

Otherwise _every_ operator is the same except for precedence!

The -> operator is described in "The Arrow Operator" section of perlop.

The => operator is described in the "Comma Operator" section of perlop.

I will learn
Perl if it kills me!


Learning your way around the standard Perl docs will go a
long way towards that.

See also:

http://learn.perl.org
 
K

Keith Keller

In the example you gave, "=>" is really like a comma in an array
definition (it's called a 'fat comma').

I've never heard that term, 'fat comma'. Does anybody know its origin?
A quick google search didn't turn up much, nor did perldoc perlop.

--keith
 
D

David Squire

Keith said:
I've never heard that term, 'fat comma'. Does anybody know its origin?
A quick google search didn't turn up much, nor did perldoc perlop.

It's in Damian Conway's "Perl Best Practices", for one.


DS
 
T

Tad McClellan

Keith Keller said:
I've never heard that term, 'fat comma'. Does anybody know its origin?


No, I don't know where it originated.

A quick google search didn't turn up much, nor did perldoc perlop.


It is documented in the "List value constructors" section of perldata,
but it is not named there...
 
D

David H. Adler

It's in Damian Conway's "Perl Best Practices", for one.

But it certainly predates that. Unfortunately, that's about as helpful
as I can be at the moment. :-/

dha
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,619
Members
45,314
Latest member
HugoKeogh

Latest Threads

Top