Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get contents of a whole file 1

Status
Not open for further replies.

kaancho12

Technical User
Feb 22, 2005
191
is there a method to get the content of a file?
thanks
 
Does you mean read the contents of a file into a string. if so,

# get list of messages from $Backup_Path directory
local( *EM_DH, *FH, $/ );
opendir(EM_DH, $Backup_Path) or die "Cannot find directory $Backup_Path $!";
my @Files= readdir EM_DH;

# loop through all messages sent in last 24 hours and remove addresses
print "Searching emails\.>>>>>>>>\n";
foreach (@Files){

# Read MSG file
my $CurrFile = "$Backup_Path$_";
open(FH, $CurrFile) or die "Error opening $CurrFile:$!";
my $FileString = <FH>;
close(FH);
 
Sorry this is it.

Do you mean read the contents of a file into a string. if so,

# get list of messages from $Backup_Path directory
local( *EM_DH, *FH, $/ );
opendir(EM_DH, $Backup_Path) or die "Cannot find directory $Backup_Path $!";
my @Files= readdir EM_DH;

foreach (@Files){

# Read MSG file
my $CurrFile = "$Backup_Path$_";
open(FH, $CurrFile) or die "Error opening $CurrFile:$!";
my $FileString = <FH>;
close(FH);
 
Try perldoc -q "entire file" at a command prompt.
Should tell you everything you need to know, and maybe then some.

 
# 1. What is your file name?
my $file = 'my-file.txt';

# 2. Open the file for reading ( '<' )
open( FD, "<$file" ) or die "$0: error: $!\n";

# Slurp it up
my @lines = <FD>;

# Close the file
close FD;
 
hi, i tried to use the program below to try to get all the contents of file "table.html" but it only seems to get the first line....what do i need to change ?
thanks

#!/usr/bin/perl
use HTML::TableContentParser;

$tcp = HTML::TableContentParser->new();
my $CurrFile = "table.html";
open(FH , $CurrFile) or die "Error opening $CurrFile:$!";
my $FileString = <FH>;

print "fileString: ", $FileString, "\n";
$tables = $tcp->parse($FileString);
for $t (@$tables) {
for $r (@{$t->{rows}}) {
print "Row: ";
for $c (@{$r->{cells}}) {
print "[$c->{data}] ";
}
print "\n";
}
}
 
$tables is an array reference... so try changing:

for $t ( @$tables )

to

for $t ( @{$tables} );
 
this line is just reading the first line of the file:

my $FileString = <FH>;

to get the entire file into the string you can do this:

my $FileString = do { local $/; <FH> };

just change the above and give your code another try

 
If you want the whole file to be as a single scalar variable then
Code:
undef $/;
open FH, "yourfile.html";
$file = <FH>;
close FH;
if you want every line of the file to go into a different scalar variable, inside an array then
Code:
open FH, "yourfile.html";
foreach(<FH>){
    push (@file,$_);
}
close FH;


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
undef $/;
open FH, "yourfile.html";
$file = <FH>;
close FH;

if you do the above then any other files opened within the scope of the block will also be read in slurp mode, which may not be what is wanted. Which is why I almost always give this example:

my $FileString = do { local $/; <FH> };

as it only affects that one filehandle.
 
That is true, but undef=$/ is an easy way to get familiar with $/ , which is not widely known and used.
do {local $/; <FH>}; seems more complex to a new perl programmer.

But yes you are right about the scope of the block.
Glad you mention it, So take this star from me to you with a lot of respect.


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
I am very honored to recieve a star from you perluserpengo [dazed]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top