in reply to Re: Way to remove the new line characters at the end of the content in a file
in thread Way to remove the new line characters at the end of the content in a file

Hi Rob, This is the error when i am using your logic. Variable "$MYFILE" is not imported at outage_nodes_updated.pl line 63. Global symbol "$MYFILE" requires explicit package name at outage_nodes_updated.pl line 63. Execution of outage_nodes_updated.pl aborted due to compilation errors.
#!C:\Perl\bin\perl.exe use Win32; use IO::Handle; use File::Find; use strict; use warnings; use File::Copy qw(copy); #use Path::Tiny qw/ path /; ###################################################################### +############## #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>; #chomp(@outagenodes); #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"; while (<$MYFILE>){ s/\s+$//; s/^\s+//; next if /^$/; 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 $fqdn -$maintMode"; my $cmdresopcmona="ovdeploy -cmd \"ovc -restart opcmona\" - +host $fqdn"; my $cmdresopcle="ovdeploy -cmd \"ovc -restart opcle\" -host + $fqdn"; print "$cmd\n"; `$cmd`; print "$cmdresopcmona\n"; `$cmdresopcmona`; print "$cmdresopcle\n"; `$cmdresopcle`; } } } truncate <MYFILE>,1; sub printLog { my ($logLine) = @_; chomp($logLine); $logLine=$logLine . "\n"; print "$logLine"; print LOG "$logLine"; }
  • Comment on Re^2: Way to remove the new line characters at the end of the content in a file
  • Download Code

Replies are listed 'Best First'.
Re^3: Way to remove the new line characters at the end of the content in a file
by roboticus (Chancellor) on Jul 22, 2015 at 10:47 UTC

    shroh:

    As the anonymous monk says, $MYFILE and MYFILE are two different things. You're opening your file with:

    open( MYFILE, "E:/Temp/INYROHS/serverlist.txt") or die "Can't open '$s +erverlist': $!";

    Using this method, you have to use MYFILE to read from the file, not $MYFILE. My code uses $FH because current practice in perl is to open files like this:

    open my $MYFILE, '<', "E:/Temp/INYROHS/serverlist.txt" or die "Can't o +pen '$serverlist': $!";

    There are several advantages to this method: It's easier to pass file handles to subroutines when you use lexical variables. Files will automatically close for you when they go out of scope (i.e. you get to the closing '}' of the block containing the definition.

    ...roboticus

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

Re^3: Way to remove the new line characters at the end of the content in a file
by Anonymous Monk on Jul 22, 2015 at 02:04 UTC
    Why do you think the bareword filehandle MYFILE is related to a variable $MYFILE?