CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Creating a log file clearing sub routine?

 



brian.hayes
User

Jan 18, 2000, 4:11 PM

Post #1 of 6 (1414 views)
Creating a log file clearing sub routine? Can't Post

Below is my vision of how to create a log file clearing utility. It's seems to work, but It does not look like the most efective way of doing it. Can anyone give me insite on this. Thanks,

Brian Hayes
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


sub change_log{
## This sub will clear data in one file and append
## that information to another file.
## works with text file only...
##
## Usage:
##
## Returns two values. 0 = something is wrong 1 = OK....
## my $rotate_it = &change_log('c:\input.txt','c:\backup.log');
## if ($rotate_it eq 0){
## Do something there is a problem......
## } elsif ($rotate_it eq 1){
## Do somthing everything is ok.....
## } else { Something is wrong......}
##
##
##
## Static variables are:
##
## $debug = 1;
$debug = 1;

# Get the two Variables.......
my $IN = $_[0] unless !defined($_[0]);
my $OUT = $_[1] unless !defined($_[1]);


# Check to ensure we were passed our two Vars...
if(!$IN or !$OUT){
if ($debug){ print "Error getting params.\n"; }
return 0;
} elsif ($IN && $OUT){

if ($debug){
print "Input file is $IN \nOutput File is $OUT\n";
}

# Open File for input. or return 0;.
open (IN, "$IN") or return 0;
# Open OUT going file for appending to or return 0;.
open (OUT, ">>$OUT") or return 0;

## While true write the data from IN file to OUT file.
while (<IN> ){
print OUT $_;
}

if ($debug){ print "Success writing data to
file: $OUT\n"; }
# Close both files...or return 0;
close OUT or return 0;
close IN or return 0;

if ($debug){ print "Successfull backup.\n";
}


# Empty out our IN file.

if ($debug){ print "Starting log rotation.\n";
}

open (IN,">$IN") or return 0;
print IN "" or return 0;
close(IN) or return 0;

if ($debug){ print "Log Rotated.\n"; }

# Get this far then everything is good. Return
1.
return 1;

} else {
# anything else if assumed bad, return 0;
return 0;
}
} # End sub.
</pre><HR></BLOCKQUOTE>


Also I have writen a script that will create a User Agent to perform some funcions against another web site. Nothing bad just trying to create a short cut for user's. Does anyone know if I should worry about doing such things as this? Other wise I will post the code for that on as well..


japhy
Enthusiast

Jan 18, 2000, 6:44 PM

Post #2 of 6 (1414 views)
Re: Creating a log file clearing sub routine? [In reply to] Can't Post

Here's my approach to a backup-generating function. You can send it filenames, or properly formatted filehandles:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


use Carp; # for proper error messages
use Fcntl qw( :flock ); # for file locking

# backup($file1,$file2)
# backup(*FH,$file2)
# backup(\*FH,$file2)
# or any combo of $filename, *FH, and \*FH

sub backup {
# ensure correct number of arguments
croak "not enough arguments to backup()" if @_ < 2;
croak "too many arguments to backup()" if @_ > 2;

# assign values to variables
my ($in,$out) = @_;

# in case we need these filehandles
# they'll be closed when the subroutine ends
local (*IN,*OUT);

if (not ref $in and ref \$in eq "SCALAR") {
# got a filename, not a filehandle
open IN, "+<$in" or croak "cannot open $in for reading and writing: $!";
$in = *IN;
}
flock $in, LOCK_EX;

if (not ref $out and ref \$out eq "SCALAR") {
# got a filename (again)
open OUT, ">>$out" or croak "cannot open $out for appending: $!";
$out = *OUT;
}
flock $out, LOCK_EX;

# now we're ready

{
# print ALL the contents as ONE string
# quickly, and efficiently
local $/;
print $out <$in>;
}

# clear $in
seek $in, 0, 0;
truncate $in, 0;
}
</pre><HR></BLOCKQUOTE>

To print any debugging messages, you could use the carp() function (instead of warn(), or instead of print()ing to STDOUT). Historically, error messages, warnings, and debugging statements go to STDERR, rather than STDOUT.

Your code is ok, but for security, you may want to lock the filehandles so that their data is not corrupted mid-stream. Also, you needn't say "$in", when a simple $in will do -- this is called stringification, and can get you in trouble if you work with references.

As for user agent programming, have you looked the libwww-perl (LWP) bundle? It allows for simple creation and use of user agents, and easy access to files on the internet.

That's all for now.


brian.hayes
User

Jan 18, 2000, 7:15 PM

Post #3 of 6 (1414 views)
Re: Creating a log file clearing sub routine? [In reply to] Can't Post

Well I see I have a long way to go...

My question about the LWP::UserAgent
is. Are there leagal issues to worry about for using them? I.E. Like making submition tools, Sending pages through so pager Comp. Web site, Get news updates from a news web site, Etc....

I have created such things but am afraid to use them or contact the companies, not to give then any Ideas. They have enough..You know..


brian.hayes
User

Jan 18, 2000, 7:29 PM

Post #4 of 6 (1414 views)
Re: Creating a log file clearing sub routine? [In reply to] Can't Post

Also windows does not support the flock. I assume it is apart of muli treading. Also You are using word like

if(not $in and $in);

is this a part of a

use vars qw(not,and);

some where in one of the Modules listed above?

I see where you are going I will just need to not use the flock untill it is supported or untill I get a Linux box at work.."This is harder than it seems".

Thanks,


Brian Hayes


brian.hayes
User

Jan 18, 2000, 7:37 PM

Post #5 of 6 (1414 views)
Re: Creating a log file clearing sub routine? [In reply to] Can't Post

you needn't say "$in", when a simple $in will do -- this is called stringification, and can get you in trouble if you work with references.

This is confusing as well??



japhy
Enthusiast

Jan 18, 2000, 9:46 PM

Post #6 of 6 (1414 views)
Re: Creating a log file clearing sub routine? [In reply to] Can't Post

First, regarding "stringification". This is when you place quotes around an expression to make Perl see it explicitly as a string. Here are some cases where it is DEFINITELY not needed (and possibly shouldn't be used):

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$octal = "0644"; # that's a string, not an octal number
$number = "10"; # numbers shouldn't be quoted
$fname = "$names[0]"; # variables needn't be quoted when they stand alone like that
</pre><HR></BLOCKQUOTE>

When I mentioned references, I meant a data structure like:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$names = [ "Jeff", "Joe", "Jack" ]; # array reference
function("$names");
</pre><HR></BLOCKQUOTE>

Placing a reference in quotes can break your Perl program, in that it can't use the reference correctly. When you stringify a reference, you get a string like "ARRAY(0x12345)" or "SCALAR(0x09876)", and you can't get to the data stored in the reference through that string.

Regarding 'and' and 'or' and 'not'... these are built-in synonyms for && and &#0124; &#0124; and !. They can make code more readable to less experienced programmers.

Finally, regarding LWP. As long as you do not violate a companies content policy, you're not breaking a law. I use LWP::Simple to grab the weather off www.weather.com. It's ok.

--
japhy

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives