About local($/) !

S

sonet

Which one is better?? Why?
----------------------------------------------------------------------------
---------
my $filename='Template.htm';
open(TEMPLATE, $filename) or die "can't open $filename: $!";
my $template = join('',<TEMPLATE>);
close(TEMPLATE);
print $template;
=====================================================
my $filename='Template.htm';
open(TEMPLATE, $filename) or die "can't open $filename: $!";
local($/) = undef;
my $template = <TEMPLATE>;
close(TEMPLATE);
print $template;
 
J

John Bokma

sonet said:
Which one is better?? Why?
-----------------------------------------------------------------------
----- ---------
my $filename='Template.htm';
open(TEMPLATE, $filename) or die "can't open $filename: $!";
my $template = join('',<TEMPLATE>);
close(TEMPLATE);
print $template;
=====================================================
my $filename='Template.htm';
open(TEMPLATE, $filename) or die "can't open $filename: $!";
local($/) = undef;
my $template = <TEMPLATE>;
close(TEMPLATE);
print $template;

use File::Slurp;

my $text = read_file( 'filename' );
 
U

usenet

sonet said:
Which one is better?? Why?

Is your intent is to read an entire file into a scalar variable but
preserve newline characters within the scalar?

You can use the Swiss Army Knife of all IO:

use IO::All;
my $text = io($filename) -> slurp;

You don't need worry about the record separator if it's "\n".
Otherwise, you can specify whatever separator you want:

my $text = io($filename) -> seperator("\t") -> slurp; #tab separator

This works in either scalar or array context. Basic error handling is
provided by the module.
 
J

John W. Krahn

sonet said:
Which one is better?? Why?
----------------------------------------------------------------------------
---------
my $filename='Template.htm';
open(TEMPLATE, $filename) or die "can't open $filename: $!";
my $template = join('',<TEMPLATE>);
close(TEMPLATE);
print $template;
=====================================================
my $filename='Template.htm';
open(TEMPLATE, $filename) or die "can't open $filename: $!";
local($/) = undef;
my $template = <TEMPLATE>;
close(TEMPLATE);
print $template;


read TEMPLATE, my $template, -s TEMPLATE;



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top