Open file, print into file, what variable is used?

B

Billy

Real quick question... The below script does what I want... IE open all txt
files... reads every line that starts with ~ and adds (prints) it to 001.log
file.

But my question is: Where is the information held that gets put into the log
file (on the line "print SCRIPTLOG;" it prints the line from the txt file
that was found to start with a ~.) Is this line put into a variable? If so
what is it?
In other words can I open another file later and call that same variable?
IE:
open(SCRIPTLOG2, ">>002.log") || die "Can't open myscript.log: $!";
print ?????????;
close SCRIPTLOG2;
Thank you,
Billy

#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);
use strict;
use warnings;

foreach my $file (<*.txt>) {
open FILE, $file or die "Can not open $file $!\n";
while (<FILE>) {
if (/~/) {
s/~([^%\^]+).*/$1/;
open(SCRIPTLOG, ">>001.log") || die "Can't open myscript.log: $!";
print SCRIPTLOG;
close SCRIPTLOG;
}
}
close FILE;
}
 
J

Jürgen Exner

Billy said:
Real quick question... The below script does what I want... IE open
all txt files... reads every line that starts with ~ and adds
(prints) it to 001.log file.

You should drop this misconception as soon as possible or it will hurt you
very badly in the long run. IE certainly does not open any txt file and
never read a single line in your code below. IE would have no idea to how
execute the code to begin with. IE deals with HTML, nothing else.

What you got is a web browser, that makes an HTTP request to a web server,
which in turn calls a CGI program, which in turn creates some response and
as a side effect does some other stuff, too.
But my question is: Where is the information held that gets put into
the log file (on the line "print SCRIPTLOG;" it prints the line from
the txt file that was found to start with a ~.) Is this line put into
a variable? If so what is it?

See below
In other words can I open another file later and call that same
variable? IE:
open(SCRIPTLOG2, ">>002.log") || die "Can't open myscript.log: $!";
print ?????????;
close SCRIPTLOG2;
Thank you,
Billy

#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);
use strict;
use warnings;

foreach my $file (<*.txt>) {
open FILE, $file or die "Can not open $file $!\n";
while (<FILE>) {

Here one by one the said:
if (/~/) {
s/~([^%\^]+).*/$1/;
open(SCRIPTLOG, ">>001.log") || die "Can't open myscript.log: $!";
print SCRIPTLOG;

And print() defaults to printing $_ if no expression is provided.
At least this part you could have figured out very easily on your own.
From 'perldoc -f print':
print FILEHANDLE LIST
[...]
If LIST is also omitted, prints "$_" to the currently
selected output channel.
 
A

A. Sinan Unur

You should drop this misconception as soon as possible or it will hurt
you very badly in the long run. IE certainly does not open any txt

I think he meant 'i.e.' (id est) rather than Internet Explorer.

Sinan.
 
B

Billy

A. Sinan Unur said:
I think he meant 'i.e.' (id est) rather than Internet Explorer.

Sinan.

Exactly.... (IMHO this is the problem with typed text done in a hurry...
meaning is lost sometimes!)

Billy
 
A

A. Sinan Unur

But my question is: Where is the information held that gets put into
the log file (on the line "print SCRIPTLOG;" it prints the line from
the txt file that was found to start with a ~.) Is this line put into
a variable? If so what is it?

Are you thinking of filehandles?
In other words can I open another file later and call that same
variable? IE:
open(SCRIPTLOG2, ">>002.log") || die "Can't open myscript.log: $!";
print ?????????;
close SCRIPTLOG2;

Yes, you can do that, but it can get out of hand rather quickly.

Lexical filehandles can be useful because the file they refer to is
autmatically closed when the filehandle goes out of scope.
#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);

This does not seem to be a CGI script. Why do you have it here?
use strict;
use warnings;

foreach my $file (<*.txt>) {
open FILE, $file or die "Can not open $file $!\n";
while (<FILE>) {
if (/~/) {

This matches any line that contains a ~, not lines that begin with a ~
which is what you claim above.
s/~([^%\^]+).*/$1/;

Honestly, I am not sure what this line is supposed to do.
open(SCRIPTLOG, ">>001.log") || die "Can't open myscript.log: $!";
print SCRIPTLOG;
close SCRIPTLOG;

If there is a good reason to open/close for each line, I can't see it right
now. You stated that all you wanted was to print lines starting with ~.

Untested:

use strict;
use warnings;

my @logfn = qw(001.log 002.log);

for my $logfn (qw(001.log 002.log)) {
open my $SCRIPTLOG, '>>', $logfn
or die "Cannot open $logfn: $!";


for my $fn (<*.txt>) {
open my $FILE, '<', $fn
or die "Cannot open $fn: $!";

while(<$FILE>) {
print $SCRIPTLOG if substr($_, 0, 1) eq '~';
}
}
}
 
A

Anno Siegel

Billy said:
Real quick question...

So you don't know the answer, but you know what is involved in answering
it?

"Quick question" must mean that it didn't take you long to ask it. I
have read it three times, and I'm still not sure what it is you want
to know.

[incomprehensible question snipped]

Anno
 
J

Joe Smith

Billy said:
Is this line put into a variable? If so what is it?

It's time to step back and re-read the list of Perl's built-in
functions. Pay close attention to which functions take arguments,
and what default argument is used if you don't provide one.

perldoc perlfunc

Also, look at the list of Perl's special variables.

perldoc perlvar
while (<FILE>) {
print SCRIPTLOG;
}

Sounds like you don't realize that those three lines
and the following three lines are equivalent.

while (defined($_=<FILE>)) { # Read one line at at time into $_
print SCRIPTLOG $_; # $_ is default for many things
}


-Joe
 
B

Billy

Joe Smith said:
Sounds like you don't realize that those three lines
and the following three lines are equivalent.

while (defined($_=<FILE>)) { # Read one line at at time into $_
print SCRIPTLOG $_; # $_ is default for many things
}
-Joe

Joe,

Thank you... Civil answers and a push in the right direction is very well
appreciated. When your new around here and ask a beginner question some
people act as though you just kicked their dog (cat, favorite animal).

Thank you again,
Billy
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top