Forum Moderators: coopster & phranque

Message Too Old, No Replies

print command outputs only partial content

         

vygbuyhnmj

12:26 pm on Jun 15, 2006 (gmt 0)

10+ Year Member



#!/usr/bin/perl -w
$import_file="file-list-to-import.txt";

open(DAT, $import_file) ¦¦ die("Could not open file: $import_file");
@data=split('s.',<DAT>); # Splits each newline
close(DAT);

print "Content-type: text/html\n\n"; # Default header
print "<html><title>Perl CHMOD</title><body>";

foreach $item (@data)
{
chmod (0666, $item);
print "File changed: $item";
print "<br>";
$count++
}

print "Total files changed: $count\n";
print "</body></html>";

The above code prints the following:

File changed: /home/rp
File changed: public_html/myrp
File changed: config.php
Total files changed: 3

Here is the imported file:

/home/rps/public_html/myrps/config.php
/home/rps/public_html/myrps/config_override.php
/home/rps/public_html/myrps/config.php

(Note: there are only line returns after each line in the file, there are NOT any additional spaces)

perl_diver

5:39 pm on Jun 15, 2006 (gmt 0)

10+ Year Member



@data=split('s.',<DAT>); # Splits each newline

the above regexp means to split on the letter 's' followed by any single character. Which is what your output shows. You probably want to do this:


#!/usr/bin/perl -w
$import_file="file-list-to-import.txt";

open(DAT, $import_file) or die "Could not open file: $import_file";
@data=<DAT>;
close(DAT);
chomp(@data);

print "Content-type: text/html\n\n"; # Default header
print "<html><title>Perl CHMOD</title><body>";

foreach $item (@data)
{
if (chmod (0666, $item)) {
print "File changed: $item<br>\n";
$count++
}
else {
print "File not changed: $item<br>\n";
}
}
print "Total files changed: $count\n";
print "</body></html>";

vygbuyhnmj

6:41 pm on Jun 15, 2006 (gmt 0)

10+ Year Member



Yeah, that did it! What's weird is that the files were actually modified still, eh.

I'm having another problem with the "or die" command.

My output is a blank page for this:

#!/usr/bin/perl -w
$import_file="file-list-to-import.txt";

open(DAT, $import_file) ¦¦ die("Could not open file: $import_file");
@data=<DAT>;
close(DAT);

print "Content-type: text/html\n\n"; # Default header
print "<html>
<head>
<title>Perl CHMOD</title>
</head>

<body>\n";

foreach $item (@data)
{
$temp = 0;
chmod (0644, $item) or die $temp = 1;

if ($temp == 0 ) {
print "File changed: $item\n<br />";
}
elsif ($temp == 1) {
print "Could not change file: $item\n<br />";
$temp = 0;
}

# chmod (0755, $item) ¦¦ print("Could not change file: $item\n<br />");
# print "File changed: $item";
# print "<br />";

$tot_count++
}

print "Total files changed: $tot_count\n";
print "
</body>
</html>";

I want to have a message displayed if the file was changed ok, and then another displayed if it was not changed ok. My idea was on the "or die" command, I change the value of a variable, then use an if statement below to print the correct statement.

[edited by: vygbuyhnmj at 6:51 pm (utc) on June 15, 2006]

vygbuyhnmj

11:20 pm on Jun 15, 2006 (gmt 0)

10+ Year Member



For some reason I am unable to edit my above message, however, here is slightly updated code:

#!/usr/bin/perl -w
$import_file="file-list-to-import.txt";

open(DAT, $import_file) ¦¦ die("Could not open file: $import_file");
@data=<DAT>;
close(DAT);

print "Content-type: text/html\n\n"; # Default header
print "<html>
<head>
<title>Perl CHMOD</title>
</head>

<body>\n";

foreach $item (@data)
{
$temp = 0;
chmod (0644, $item) or $temp = 1;

if ($temp == 0 ) {
print "File changed: $item\n<br />";
$tot_count++
}
if ($temp == 1) {
print "Could not change file: $item\n<br />";
$temp = 0;
}

# chmod (0755, $item) ¦¦ print("Could not change file: $item\n<br />");
# print "File changed: $item";
# print "<br />";

}

print "Total files changed: $tot_count\n";
print "
</body>
</html>";

vygbuyhnmj

3:01 am on Jun 16, 2006 (gmt 0)

10+ Year Member



Ah, when you read a file that has a new line as the spacer, you need to chomp the array. :)

chomp(@data=<DAT>);