On Mar 17, 7:40 am, [EMAIL PROTECTED] (Irfan Sayed) wrote:
> Hi All,
>
> Can somebody please let me know the meaning of this line.
>
> while (<$in>)
>
> {
>
> if(/,/) {print "before match: $`\t and after match: $'\n\n";}; $x=$';
> $y=$`; &mysubroutine($x,$y);
>
> }
>
> I know it is a while loop for the file handle ($in) and it will be
> executed till the end of file but not getting the meaning of if loop.
>
> What this if loop actually does. Please help

Try looking at it this way:

while (<$in>) {     ## Go through $in line by line
    if (/,/) {            ## Match a comma
        print "before match: $`\t and after match: $'\n\n";  ## Print
stuff before match and after using $` and $'
    }
    $x = $';           ## Assign $' to $x
    $y = $`;          ## Assign $` to $y
    &mysubroutine($y, $x);  ## Run $x and $y through <mysubroutine>
}

The if block goes through $in, line by line, checking for commas. The
variables $` $& and $' are special: $` = the string before a regular
expression match, $& = the match itself, and $' = the part of the
string after the regular expression match. When it finds a comma, it
prints the strings before and after the match of the comma in the
line.  That is, it assigns whatever in the line precedes the comma to
$` and whatever follows to $'. Then it assigns those matches to $x and
$y and runs those as parameters through a subroutine called
<mysubroutine>. So you can use these special variables in regular
expression matching (as this code does). But many people avoid them
like the plague since they slow down all matching. See perldoc perlvar
for more.

As to what the subroutine <mysubroutine> actually *does* to or with
those newly assigned variables, there's no way to say without seeing
that code.

Hope this helps, T


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to