Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Passing the files as an argument in Subroutines

by shroh (Acolyte)
on Jul 20, 2015 at 00:59 UTC ( [id://1135389]=perlquestion: print w/replies, xml ) Need Help??

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

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:

Replies are listed 'Best First'.
Re: Passing the files as an argument in Subroutines
by 1nickt (Canon) on Jul 20, 2015 at 01:36 UTC

    Why have you commented out use strict and use warnings?

    They will help you discover errors in your code and syntax, before you even run the program!

    Please replace those lines and let us know what perl tells you....

    The way forward always starts with a minimal test.
      Hi James, This is what i am getting when i use when using strict and warning.

      E:\Temp\inyrohs>perl outage_nodes.pl off Can't locate warning.pm in @INC (@INC contains: E:/apps/Perl64/site/lib E:/apps/ Perl64/lib .) at outage_nodes.pl line 6. BEGIN failed--compilation aborted at outage_nodes.pl line 6. E:\Temp\inyrohs>perl outage_nodes.pl on Can't locate warning.pm in @INC (@INC contains: E:/apps/Perl64/site/lib E:/apps/ Perl64/lib .) at outage_nodes.pl line 6. BEGIN failed--compilation aborted at outage_nodes.pl line 6.

        Hi shroh,

        Hi James, This is what i am getting when i use when using strict and warning....E:\Temp\inyrohs>perl outage_nodes.pl off Can't locate warning.pm..

        perl does not have use warning but warnings. Note the s. After that is corrected, then try running the code again.
        You can also include use diagnostics; so the pragmas will go like thus:

        use strict; use warnings; use diagnostics;

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me
        Hi shroh,

        the correct pragma is:

        use warnings;
        (i.e. warnings must be plural, not use warning;).

        Update: Sorry for the noice, I had not seen 2teez's earlier post to the same effect when I posted that.

        Psst- i think you meant to reply to 1nickt ;) but maybe I can help. What OS are you running? Where is Perl installed? What architecture?
        sorry i am having a double post problem.
Re: Passing the files as an argument in Subroutines
by GotToBTru (Prior) on Jul 20, 2015 at 13:05 UTC

    Starting with:

    sub deleteTemp { my ($delTemp)=@_[0]; $cmddelTemp="DEL /Q $delTemp\n"; system($cmddelTemp); my($serverlist)=@_[1]; $cmdcopy="xcopy $serverlist $delTemp"; system($cmdcopy); }

    Here's my suggestions:

    • my ($delTemp,$serverlist) = @_; #your code works, mine is prettier ;)
    • Use unlink to delete the file in a single step instead of two.
    • Use File::Copy to copy in a single step instead of two. It's a core module, you should already have it.
    • Those two last suggestions make your code more portable.
    Dum Spiro Spero
Re: Passing the files as an argument in Subroutines
by james28909 (Deacon) on Jul 20, 2015 at 01:52 UTC
    this is your first mistake :)
    #use strict; #use warning;
Re: Passing the files as an argument in Subroutines
by Anonymous Monk on Jul 20, 2015 at 02:35 UTC
    #!C:\Perl\bin\perl.exe #!/usr/bin/perl -- ## ## ## ## perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if while for " +-otr -opr -ce -nibc -i=4 -pt=0 "-nsak=*" ## perltidy -olq -csc -csci=3 -cscl="sub : BEGIN END if while for " - +otr -opr -ce -nibc -i=4 -pt=0 "-nsak=*" #!/usr/bin/perl -- use strict; use warnings; use Path::Tiny qw/ path /; ## use File::Find::Rule qw/ find rule /; use Time::Piece qw/ localtime /; Main( @ARGV ); exit( 0 ); sub Main { my( @argv ) = @_; ... my $date = localtime()->strftime( '%Y_%m_%d' ); Frobnicate( "E:/Temp/inyrohs/maintenanceMode_$date.log", "E:/Temp/inyrohs/temp/outagenodes_temp.txt", "E:/Temp/INYROHS/serverlist.txt", ); } ## end sub Main sub Frobnicate { my( $LOG, $tempfile, $serverlist ) = @_; my @outagenodes = path( $serverlist )->lines_raw; my @tempnodes = path( $tempfile )->lines_raw; print "@outagenodes\n"; slog( $LOG, "Starting the maintenance\n\n" ); ... } ## end sub Frobnicate sub slog { my( $log, @rest ) = @_; my $logfh = fileno( $log ) ? $log : path( $log )->opena_raw; print $logfh localtime()->strftime( '%H:%M:%S' ), "\n", @rest; } ## end sub slog
      Hi, I think you moulded my code, can you explain your version of code now?
Re: Passing the files as an argument in Subroutines
by Anonymous Monk on Jul 20, 2015 at 02:38 UTC
    Also use the list form of system, as in system( 'C:/...foo.exe', 'arg', 'arg', 'arg' );
Re: Passing the files as an argument in Subroutines
by james28909 (Deacon) on Jul 20, 2015 at 01:52 UTC
    woops, double post.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1135389]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-19 16:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found