CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
$var .= $_; what it mean?

 



perl1006
New User

Sep 4, 2006, 4:16 AM

Post #1 of 4 (1874 views)
$var .= $_; what it mean? Can't Post

 
hi,

any body can tellme what is assigned to the variable in the following code.

open (IN,"./template.html");
while (<IN>){
$var .= $_;
}
close IN;

in the above code open function takes the template files into IN
but i can't unserstand the expr
$var .= $_;

can any body tell me what is the meaning of the above line.

Thanks
Kanth


KevinR
Veteran


Sep 4, 2006, 1:07 PM

Post #2 of 4 (1870 views)
Re: [perl1006] $var .= $_; what it mean? [In reply to] Can't Post

.= (dot equal) is the concatenation assingment operator. Each value of $_ is added to (concatenated onto) the end of $var. In your code, $_ is the value or each read of the filehandle being read in the <> operator. That value is added to $var until the end of the file is reached.
-------------------------------------------------


davorg
Thaumaturge / Moderator

Sep 5, 2006, 7:55 AM

Post #3 of 4 (1864 views)
Re: [perl1006] $var .= $_; what it mean? [In reply to] Can't Post

KevinR has given you the literal translation of the code. But what he hasn't told you is the "real" meaning of that code - which is that the person who wrote it really didn't know what they were doing.

What you're trying to do here is to read in the contents of a file into a scalar variable. The person who wrote this knows that the default behaviour of the file input operator is to return a single row when called in scalar context. But he didn't know how to get round that either by a) calling it in list context:


Code
$var = join '', <IN>;


Or by changing its default "line at a time" behaviour:


Code
$var = do { local $/; <IN>; }


I'd be very loathe to trust any Perl code written by the person who wrote that snippet.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


KevinR
Veteran


Sep 5, 2006, 12:49 PM

Post #4 of 4 (1862 views)
Re: [davorg] $var .= $_; what it mean? [In reply to] Can't Post


Quote
But what he hasn't told you is the "real" meaning of that code - ....


Sounds like the plot for a book..... Cool
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives