Perl syntax


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl syntax
# 1  
Old 05-17-2013
Perl syntax

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

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

# 2  
Old 05-18-2013
$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:
Code:
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.
Code:
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.

Last edited by balajesuri; 05-18-2013 at 01:17 AM..
This User Gave Thanks to balajesuri For This Post:
# 3  
Old 05-18-2013
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.

Last edited by elixir_sinari; 05-18-2013 at 04:32 AM..
This User Gave Thanks to elixir_sinari For This Post:
# 4  
Old 05-18-2013
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.
This User Gave Thanks to DGPickett For This Post:
# 5  
Old 05-18-2013
Quote:
Originally Posted by balajesuri
$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:
Code:
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.
Code:
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.
Here is some more code. Comment on this.

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

# 6  
Old 05-19-2013
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?
This User Gave Thanks to balajesuri For This Post:
# 7  
Old 05-19-2013
Quote:
Originally Posted by balajesuri
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?
Actually I copied it wrong. Below is what it should be. Please explain that.


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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

A Perl Syntax Question.

Greetings! Here's what I believe is a "simple one" for the community tonight ;) What I'm trying to do is assign a "true/false" value to a variable depending upon whether a named process (some-process) exists; and then test for this value in the succeeding logic. I banged my head against the... (2 Replies)
Discussion started by: LinQ
2 Replies

2. UNIX for Dummies Questions & Answers

Perl syntax

Query with perl syntax Aim: is to change a perl script to use a new file I was required to replace - entries \"$entries\" with -lib <full_path_to_filename> So in the code detector.pm sub rundetector { my $class = shift; mkdir($resultDirectory); my... (3 Replies)
Discussion started by: sa@@
3 Replies

3. Programming

Perl syntax question

Hallo everybody, I have a following problem - I'm doing a map funciont to fill in a HTML table and I want to use some radiobutton groups. Unfortunatelly, they are grouped by names, so I have to add some "counter" that will divide one row from another, and I'm using CGI.pm for generating the... (3 Replies)
Discussion started by: duskos
3 Replies

4. Programming

Syntax error in perl program.

Hi, i am running this code but i am getting syntax error #!/usr/bin/perl use warnings; use strict; use XML::LibXML; use XML::LibXML::Reader; use Data::Dumper; my $file; open( $file, 'DTC_Specification_transformed.xml'); my $reader = XML::LibXML::Reader->new( IO => $file ) or die... (1 Reply)
Discussion started by: veerubiji
1 Replies

5. Shell Programming and Scripting

Perl syntax that I don't understand.

I'm just trying to confirm that I understand someone's code correctly. If someone has code that says: $foo ||= mysub(); I'm assuming that it means if $foo is nothing or undef, then assign it some value via mysub(). If I'm wrong on this, please let me know. Also, what's the difference... (4 Replies)
Discussion started by: mrwatkin
4 Replies

6. Shell Programming and Scripting

PERL Syntax Errors

Hi, I am a newbie to PERL and working on a script. When running it I get a lot of compilation errors. The actual command in the program (which is within a case structure) is given below # This gives the actual count of inquires from a log file (It works fine when I type this on the... (2 Replies)
Discussion started by: nurani
2 Replies

7. Shell Programming and Scripting

perl syntax help

Im new at scripting and im trying to write a script using perl that will make sure there are 2 command line integer arguments and then check if the 2nd argument is greater than the first. i believe im close but still receive my error message even when i have 2 arguments and the second part gives me... (6 Replies)
Discussion started by: livewire06
6 Replies

8. Shell Programming and Scripting

perl version for syntax errors

All, Does it matter what perl verios your running when you get syntax errors? on version 5.6.1 the code works fine, but on 5.8.0 the code gets errors? #!/usr/bin/perl #use strict; #use warnings; my $mess = 'messages'; my $mess1 = 'messages.1'; my $mess2 = 'messages.2'; my... (13 Replies)
Discussion started by: bigben1220
13 Replies

9. UNIX for Advanced & Expert Users

perl explain syntax !!!

hi all i was going through some perl code i came across this line and i am not getting what is exactly going on .. $$this{localtion} = GetName->GetVarName("EXE_DIR") ; what is the red part doing in above code (2 Replies)
Discussion started by: zedex
2 Replies

10. Shell Programming and Scripting

Perl command syntax for C:\dir

Hi Everyone, Perl command syntax that would display my ... C:\dir .... Regards, asabzevari (1 Reply)
Discussion started by: asabzevari
1 Replies
Login or Register to Ask a Question