Hi Monks, I am trying to create a subroutine in perl which takes two files as arguments in the subroutine call. The role of subroutine is to delete first file and copy the contents of second file in a new file. But i am not able to do that, may be because i dont know how to pass the files as arguments. My code is below: deleteTemp is the subroutine.
#!C:\Perl\bin\perl.exe use Win32; use IO::Handle; use File::Find; #use strict; #use warning; ###################################################################### +############## #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. # # # #expect values 'on|off' ($SEC,$MIN,$HOUR, $DAY, $MON, $YEAR)= (localtime) [0..6]; $year=$YEAR+1900; $month=$MON+1; $day=$DAY; $date="$year\_$month\_$day"; $LOG="E:/Temp/inyrohs/maintenanceMode_$date.log"; $tempfile="E:/Temp/inyrohs/temp/outagenodes_temp.txt"; $serverlist="E:/Temp/INYROHS/serverlist.txt"; open( MYFILE, "E:/Temp/INYROHS/serverlist.txt"); @outagenodes=<MYFILE>; #$ref2MYFILE=\*MYFILE; close MYFILE; open ( MYTEMPLIST, "E:/Temp/INYROHS/temp/outagenodes_temp.txt"); @tempnodes=<MYTEMPLIST>; # $ref2MYTEMPLIST=\*MYTEMPLIST; close MYTEMPLIST; print "@outagenodes\n"; #Below subroutine call is to delete the temp file if already existing +so that the outage is not run on the previously entered servers. deleteTemp($tempfile,$serverlist); 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"; $maintMode=$ARGV[0]; chomp($maintmode); print "maintMode: $maintMode:\n"; foreach $NODES (@tempnodes) { printLog("Node: $NODES, processing...\n"); if($maintMode =~ m/on/) { print "entered loop \n"; $cmd="ovownodeutil -outage_node -unplanned -disable_heartb +eat -delete_msgs -node_name $NODES -$maintMode "; print "$cmd\n"; system($cmd); print "done with command"; } elsif($maintMode=~ m/off/) { printLog("Putting the server $NODES in the outage.\n"); $cmd="ovownodeutil.cmd -outage_node -unplanned -disable_he +artbeat -delete_msgs -node_name $NODES -$maintMode"; $cmdstop="opcragt -stop $NODES"; $cmdstart="opcragt -start $NODES"; print "$cmd\n"; system($cmd); print "$cmdstop\n"; system($cmdstop); print "$cmdstart\n"; system($cmd); system($start); } } sub printLog { my ($logLine) = @_; chomp($logLine); $logLine=$logLine . "\n"; print "$logLine"; print LOG "$logLine"; } sub deleteTemp { my ($delTemp)=@_[0]; $cmddelTemp="DEL /Q $delTemp\n"; system($cmddelTemp); my($serverlist)=@_[1]; $cmdcopy="xcopy $serverlist $delTemp"; system($cmdcopy); }
I am getting the below error: E:\Temp\inyrohs>perl outage_nodes.pl on Invalid switch - "Temp". Invalid number of parameters 2015_7_19 starting the process maintMode: on:

In reply to Passing the files as an argument in Subroutines by shroh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.