473,408 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

can you explain a little bit for me

Hi, i am totally new to perl.
I saw one example script but can't figure some syntax out, could you help out.
Expand|Select|Wrap|Line Numbers
  1.     $collected = Get_Friends($id,$name,"$profile_file",$limit,$collected); 
  2.     $profile_file =~ s/>//;                
  3.  
These codes are in one subroutine, and these is another subroutine called Get_Friends., are these variables in the square parameters like JAVA, C#. And what does =~s mean ?

thanks in advanced
Sep 5 '07 #1
3 1500
KevinADC
4,059 Expert 2GB
The second line is a regular expression. "=~" is the binding operator. It is similar to "=" but return the values of a regular expression to the variable on the left instead of simply assigning a value like "=" does. s/// is the substitution regular expression. In the that line:

$profile_file =~ s/>//;

it is substituting the first instance of '>' in $profile_file with nothing,. so it is removing it.

The first line is calling the subroutine Get_Friends(). The data in the parenthesis to the right of the subroutine name are the arguments sent to the subroutine, the value returned is assigned to $collected on the left of the "=" symbol.
Sep 5 '07 #2
thank you very much.

I just had a look at the tutorial of perl, but still have some confusions

what does @ represent?
my $id = shift; what does this mean?

and $1,$2 what are these variables meaning?
Sep 6 '07 #3
KevinADC
4,059 Expert 2GB
@ usually defines an array

"shift" is a function that removes and returns the first element of an array. If no array is used with the function it removes and returns the first element of the system array: @_, which is used to pass arguments to subroutines. Perl has two very special variables:

$_
@_

whenever a perl functon is used without a specific variable perl almost always assumes $_ or @_ depending on the context. Which is why something like this works:

Expand|Select|Wrap|Line Numbers
  1. for (@array) {
  2.    print;
  3. }
the print function is not using any argument so perl assumes $_. Same with the for() loop. Perl assigns each element of @array to $_ if you do not specify a variable like below:

Expand|Select|Wrap|Line Numbers
  1. foreach my $var (@array) {
  2.    print $var;
  3. }
Dont get into the habit of using $_ and @_ too much and relying on perl to do what is right. At first it seems tempting to write code like the first example above, but in reality it's not good practice.

$1 and $2 (up to $9 I believe) are special variables perl uses to store pattern matches of regular expressions in memory. They are read only variables, which means they can not be modifed inplace.

Expand|Select|Wrap|Line Numbers
  1. $foo = 'foo';
  2. $foo =~ m/^(.)/;
  3. print $1; (prints 'f');
Your questions above indicate you are jumping around considerably. Data types should be the first chapter or near first of any perl tutorial or book. Stuff like "shift" and $1 will come later after you are familiar with the basic stuff.

You should read a perl book, here is a free online book:

Beginning Perl
Sep 6 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: srikanth | last post by:
static char buf = {1,2,3,4,5,6,7,8}; printf("Address: %u : Val = %d \n",(int*)buf+1,*((int*)buf+1)); Thanks Vija
10
by: Greg Stark | last post by:
This query is odd, it seems to be taking over a second according to my log_duration logs and according to psql's \timing numbers. However explain analyze says it's running in about a third of a...
4
by: Tweaxor | last post by:
Hey, forgive me of my ignorance! Can someone tell me why this won't work. I have this coded but it won't compile is this not permitted in C. kg = kilograms and d = distance // this is not...
22
by: Jaspreet | last post by:
I was recently asked this question in an interview. Unfortunately I was not able to answer it and the interviewer made a decision on my C strengths (or weekness) based on this single question and...
4
by: BluDog | last post by:
Hi I am trying to test dynamically created controls, to do this i have added a placeholder to a WbForm and added the following code behind: Private Property Count() As Integer Get If...
4
by: marklawford | last post by:
Not having earned my DBA badge from the scouts just yet I'm a little lost with an error I'm getting. We've just upgraded our development database from 7.2 to 8.2 as the first step in upgrading...
5
by: Petded | last post by:
Hi, can anyone explain to me why vc++ managed use this s= gcnew Socket( AddressFamily::InterNetwork,SocketType::Stream,ProtocolType::Tcp ); whereas c# use this
40
by: dev_cool | last post by:
hello everybody, i'm a new member of this group, this is my first ever post. i've found a c program that prints prime numbers through an infinite loop. But I really don't understand the code....
11
by: Faisal Vali | last post by:
Hi - I'm new to javascript and I was reading the book Javascript Professional Projects - there is a fragment that has me a little perplexed, and I was wondering if anyone could explain why and how...
2
by: TYF | last post by:
hi, can any one explain me what this little macro is doing? #define pos_of(h, f) ((short int) &((h *) 0)->f) h is a struct f is a member of it i have no clue, i only know it has...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.