On Thu, Oct 15, 2009 at 2:55 PM, Raheel Hassan <raheel.has...@gmail.com>wrote:
> Hi, > > I will be very thankful if someone explains under given code. > > #----- CGI Module ------------------------------ > use CGI; > > $q = CGI->new(); > > my ($myself) = split(/\?/,$q->self_url); #from where it will call the > url? > my @base_url = split(/\//, $myself); #what is \ / > my $base_url = $base_url[0]."//".$base_url[2]."/"; > my $validate_url = $q->self_url."&validate=1"; > my $id = $q->param("id"); #what is the role > of param here? > my $hub = $q->param("HUB"); > > Regards, > Raheel. > Lets have a go at this then :-) my ($myself) = split(/\?/,$q->self_url); #from where it will call the url? my @base_url = split(/\//, $myself); #what is \ / my $id = $q->param("id"); #what is the role of param here? To understand the first line you have to understand what the line before that does... $q = CGI->new(); This creates a new CGI object. As most objects a CGI object has various attributes one of them being the URL used to start the CGI script. So $q->self_url returns the value of this attribute. The next line is a lot easier: in a regular expression or a split where you use the default delimiters for the expression / and / means you cannot just tell perl to split on / as that would for perl indicate the end of the expression. So you escape the / part of the expression with a \ resulting in the following /\// meaning split the string on the character /. As we determined before $q contains a CGI object this object has attributes one of them is the URL used to start the script another one is a hash with parameters found behind the URL. Think of a URL like this: http://mysite.com/cgi-bin/script.cgi?id=1&start=hello The attribute self_url in this case is: mysite.com/cgi-bin/script.cgi The parameters are: start with the value hello and id with the value 1 *(Note, you might want to have a look at the difference between GET and POST variables. In the HTML 4.1 specification as understanding the difference can save you a lot of trouble when working with CGI)* Figuring out what these parameters are is done by calling CGI Object->param("<parameter name>"). Since the script stored the CGI Object in the variable $q making the following call $q->param("id") will return the value of the parameter with the name: id. Hope that helped a bit. Rob