Home: Perl Programming Help: Beginner:


Apr 18, 2008, 7:36 AM

Views: 1111

print "Content-Type: text/html\n\n";for ( <DATA> ) { #skip blank lines next if /^$/; ($key, $value) = split /\s*,\s*/; $hyperlink{$key} = $value; #Once you've printed the header, #you can print diagnostics to the browser window! print "$key, $value<br>\n";}open HTML, "editing.html" or die $!;while( <HTML> ) { for $key ( keys %hyperlink ) { s(\s$key\s) ( <a href="$hyperlink{$key}" style="background:silver">$key</a> ); } print;}close HTML;__DATA__special, http://www.m-w.comcode, javascript:alert('Code!')example, course/index.html

In this example (taken from www.gossland.com), filehandle (<DATA>) is used with FOR loop and (<HTML>) is used with WHILE loop. Can we use FOR loop with (<HTML>) and WHILE loop with (<DATA>) ?


Apr 18, 2008, 12:42 PM

Views: 1108
Re: [jigneshjsoni] <DATA> FILEHANDLE

using "for" on a filehandle is not a common practice. Generally you would use "while" when reading in a file. They each loop through a file but "while" checks for EOF but "for" does not. "for" is really a list control not a file I/O control.

There might be a reason the code uses "for" instead of "while" that I am not aware of though.

Next time post formatted code that can be read.