Perl syntax

I'm a newbie to perl scripting. Can someone please explain the syntax below. Specifically what does -> and => do?

$tz->site(['http','https'] => $site);

$tz is a reference variable that refers to the object obtained by blessing some package. site is a sub-routine (object method) in that package to which you're sending two arguments (as a list) viz. ['http','https'] and $site.

The single arrow (->) operator is called the dereference operator. In actual sense, a single arrow is used to refer to a value in a data structure (could be a scalar variable, list, hash, object). Observe the below example:

my $anonyArrRef = [ 1, 2, 3, 4, 5 ];
print "$anonyArrRef->[2]\n";

This will output 3, the 3rd number in the anonymous array whose reference is held in $anonyArrRef variable.

In your example, $tz is a reference variable that points to some object. And that object contains a sub-routine (object method) that is accessed by the thin arrow operator (->) -- $tz->site() A big arrow operator (=>) is just a notation used to represent key-value pairs while defining a hashmap.

my %hash = ('k1', 'v1', 'k2', 'v2); # One way to define a hash. Not so pleasing to human eye.
my %hash = ('k1' => 'v1', 'k2' => 'v2'); #Another way. Pleasing to human eye.
my %hash = (   'k1' => 'v1',
               'k2' => 'v2',
               'k3' => 'v3',
           ); # A little more creativity.

In your example, you're sending one key-value pair to sub-routine site, where the key is an anonymous array ['http','https'] and value is a scalar variable $site. Actually, the key would be a reference to this anonymous array.

Hope this helps.

1 Like

Technically, the -> is a binary infix dereference operator. In your case, since the right side of the operator is neither an array/hash subscript nor a subroutine argument list, it implies a method call with site being the method name and $tz could be an object reference or a class/package name.

The => operator is just a fancy comma. You can use it almost anywhere a , is used. Since this fat-arrow operator has the feature of autoquoting any Perl identifier to its immediate left (and also since it is eye-friendly), it is the preferred way to separate keys and values in a hash.

1 Like

First, I know almost no perl, but that never stops me!

-> is stolen from C, says point to a child member of, in this case a member function or method of a class/object/struct type.

=> is somewhat synonymous with a comma, and has functions by context as a list separator or left operand discard, like in c you can say "x = ( y == 3 ? printf("Equal\n"), 1 : 0 );", which is assign to x the result of test y equal 3 if true printf and then discard the printf return and supply a 1 else supply a 0, which is an ugly way to say if y is three than printf and set x to 1 else set x to 0. The comma discards the return of the printf, and connects two commands that run together like a {}, especially handyin contexts where {} are not legal. However, the comma has special meanings by context, like separating key from value in a hash table. (A hash table is a lookup by some integer derived (hashed) from the key, to pick one of N buckets where there may be multiple but few key value pairs that have to be searched linearly. However, the hash and bucket select can be far faster than traversing a tree, and buckets are cheap, so you can have many, and so few buckets with many pairs. A good hash helps a lot, too.

1 Like

Here is some more code. Comment on this.

my $tz = LWP::UserAgent->new; 
my $site = http://website.com 
$tz->site(['http','https'] => $site);

From what I know, there is no sub-routine 'site' in the LWP::UserAgent module, available in CPAN. Are you using a custom built variant of this module?

1 Like

Actually I copied it wrong. Below is what it should be. Please explain that.

my $tz = LWP::UserAgent->new; 
my $proxy = http://website.com 
$tz->proxy(['http','https'] => $proxy);

Look at this link.

1 Like

That helps alot. Can you explain the following...

$ua->proxy(['http', 'ftp'], 'http://proxy.sn.no:8001/');

Is there a http and ftp protocol because either protocol could be passed via proxy?

$ua->proxy(['http', 'ftp'], 'http://proxy.sn.no:8001/');

This means any http or ftp requests will be proxy'd via http://proxy.sn.no:8001/

For HTTP, there is a special mechanism called tunneling. Its not a different protocol, just that the GET/POST requests will be slightly different, so that actual requests can be piggy-backed.

Is that 'tunneling' the HTTP/1.1 persistent connection, where you can send many GET and POST and the return stream has ids to let you sort them out?

SSH has tunneling, but URLs that use it do not disclose it. It can only support ftp with extra tricks . . . .