shroh has asked for the wisdom of the Perl Monks concerning the following question:

Hi Experts, I need a way to clear out the contents of a text file(or input file) after i am done using that text file. I am not looking to delete the file. Just clearing the contents of the file is enough. The reason is that , we are using an input file(txt file) to do some actions on some set of servers. Once those actions are completed i need to clear out the contents of the files so that nobody resuses those same contents when the action is taken on new set of servers. Below is my code. Just need to include the clear part, but not sure how?
#!C:\Perl\bin\perl.exe use Win32; use IO::Handle; use File::Find; use strict; use warnings; use File::Copy qw(copy); ###################################################################### +############## #This script is created to put the servers in unplanned outage as part + of the tasks# #that are received to stop the monitoring on the servers #due to some maintenance activity on the servers. # # # require "E:/Temp/inyrohs/omwNodeDetails.pm"; #expect values 'on|off' my ($SEC,$MIN,$HOUR, $DAY, $MON, $YEAR)= (localtime) [0..6]; my $year=$YEAR+1900; my $month=$MON+1; my $day=$DAY; my $date="$year\_$month\_$day"; my $LOG="E:/Temp/inyrohs/maintenanceMode_$date.log"; my $serverlist="E:/Temp/INYROHS/serverlist.txt"; open( MYFILE, "E:/Temp/INYROHS/serverlist.txt") or die "Can't open '$ +serverlist': $!"; my @outagenodes=<MYFILE>; #print "@outagenodes\n"; my $maintMode=$ARGV[0]; chomp($maintMode); open (LOG,">> $LOG") or die "Can't open $LOG file: $!\n"; printf LOG ("\nTime is %02d:%02d:%02d.\nStarting the maintenance mode +process to turn $maintMode outages.\n\n", $HOUR, $MIN, $SEC); #print "$date \n"; #print "starting the process \n"; #print "maintMode: $maintMode:\n"; if($maintMode =~ m/on/) { foreach my $NODES (@outagenodes) { my @parts=split /\./, $NODES; my $hostname=$parts[0]; chomp($hostname); my $fqdn = getNodeAttributes($hostname,"PrimaryNodeName"); print "$hostname\n"; #printLog("Node: $NODES, processing...\n"); #print "entered loop \n"; my $cmd="ovownodeutil.cmd -outage_node -unplanned -disable_ +heartbeat -delete_msgs -node_name $fqdn -$maintMode "; print "$cmd\n"; `$cmd`; print "done with command\n"; } } elsif($maintMode=~ m/off/) { foreach my $NODES (@outagenodes) { my @parts=split /\./, $NODES; my $hostname=$parts[0]; chomp($hostname); my $fqdn = getNodeAttributes($hostname,"PrimaryNodeName"); print "$hostname\n"; printLog("Node: $NODES, processing...\n"); printLog("Bringing the server $NODES out of outage.\n"); my $cmd="ovownodeutil.cmd -outage_node -unplanned -disable_ +heartbeat -delete_msgs -node_name $NODES -$maintMode"; my $cmdresopcmona="ovdeploy -cmd \"ovc -restart opcmona\" - +host $NODES"; my $cmdresopcle="ovdeploy -cmd \"ovc -restart ocpmona\" -ho +st $NODES"; print "$cmd\n"; `$cmd`; print "$cmdresopcmona\n"; `$cmdresopcmona`; print "$cmdresopcle\n"; `$cmdresopcle`; } } sub printLog { my ($logLine) = @_; chomp($logLine); $logLine=$logLine . "\n"; print "$logLine"; print LOG "$logLine"; }

Replies are listed 'Best First'.
Re: Clear the contents of the text file
by roboticus (Chancellor) on Jul 22, 2015 at 00:27 UTC

    shroh:

    Try perldoc -f truncate for a good function to clear the contents of a file. You may want to read through perldoc perlfunc and review the basic functions perl provides.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Clear the contents of the text file
by kernal (Initiate) on Jul 22, 2015 at 02:29 UTC

    If you are not worrying about programs colliding you might be able to get away with this code.

    open(MYFILE, ">E:/Temp/INYROHS/serverlist.txt"); close(MYFILE);

    The '>' sign creates a new file for writing erasing the old one.

      I was about to say the same
Re: Clear the contents of the text file
by Anonymous Monk on Jul 22, 2015 at 00:30 UTC
    Its the same as setting some contents
    use Path::Tiny qw/ path /; path( 'foo.txt' )->spew("");
      Hi, Can you explain me this in more detail. I am not aware of the functions you are using. Thanks Rohit

        Path::Tiny handles opening and closing and doing different things to a file. Its methods include spew(), but the module's own documentation recommends using append() for what you want since it locks the file and overwrites it, whereas spew() creates a temporary copy.

        [18:25][nick:~/monks]$ cat 1135741.pl #! perl -w use strict; use feature qw/ say /; use Path::Tiny qw/ path /; my $file = 'test.dat'; my @lines = path( $file )->lines( { chomp => 1 } ); # do something with @lines ... my @append = (); # empty array path( $file )->append( @append, { truncate => 1 } ); say "Done."; __END__
        [18:25][nick:~/monks]$ cat test.dat foo bar baz quux
        [18:26][nick:~/monks]$ perl 1135741.pl Done.
        [18:26][nick:~/monks]$ cat test.dat [18:26][nick:~/monks]$
        The way forward always starts with a minimal test.

        Hi, Can you explain me this in more detail. I am not aware of the functions you are using. Thanks Rohit

        I don't understand what you're asking me. You asked how to "clear the contents of the text file" and the code I posted does that .

        A reply falls below the community's threshold of quality. You may see it by logging in.