I am trying to come up with some logic to give my script an option of where to send the output.
This is a test script I started to write, I started to fizzle out trying to think of the combonations of the two options. I know I am overthinking this too much.
#!/usr/bin/perl
use strict;
use warnings;
# Modules to load
use Getopt::Long;
use Term::ANSIColor qw(:constants);
my $output = 0;
my $logfile = '';
GetOptions(
'o' =>开发者_运维技巧; \$output,
'l' => \$logfile
);
if (($output == 1) && (! $logfile eq '')){
} elsif (($output == 0)($logfile eq '')){
}
If this is of any use be my guest.
Pretty much I want 3 options
0 = off 1 = stdout 2 = logfile
Where I threw a bit of a wrench at myself if when I wanted to add a custom logfile argument. I am under the impression I cannot combine the 2 arg into the same arg, can I?
The places where I will have output to write, I will control with simple if
statments based on a condition, in my first interation which just allows for output to stdout. I just used the -o
option and specified 0
or 1
. If it was 1 it wrote the line, if it was 0 it did not.
If any has an easier solution than the one above I am open to anything.
Thanks in advance.
In the vein of TLP I suggest a $verbose
and a $logfile
option, I would also recommend that $verbose
be implicitly set to true if $logfile
is used. Use $verbose
to control print
commands as usual. The big magic is to use select
to control where the print
sends its output if no filehandle is given.
#!/usr/bin/perl
use strict;
use warnings;
# Modules to load
use Getopt::Long;
use Term::ANSIColor qw(:constants);
my $verbose = 0;
my $logfile;
GetOptions(
'verbose' => \$verbose,
'logfile=s' => \$logfile,
);
if (defined $logfile) {
$verbose = 1;
open my $log_handle, '>', $logfile or die "Could not open $logfile";
# select makes print point to LOGFILE
select($log_handle);
}
# do stuff
print "Stuff" if $verbose;
Also since Getopt::Long
gives you long options, I have changed the options names to the human readable verbose
and logfile
, however you can use short -v
or long --verbose
to your taste.
I would use $verbose
as an option, and an optional $logfile
. So, if $verbose
is set, you print, if $logfile
is set, you print to the log.
Then use:
if ($logfile) {
open STDOUT, '>', $logfile or die $!;
}
print "Yada\n" if $verbose;
Or for simplicity:
sub myprint {
return unless $verbose;
print @_;
}
myprint("Yada\n");
Ok, first off, forgive me if this syntax is wrong. Its been a LONG time since I've done any perl, so take this more as a "do this sortof thing" rather than a "copy and paste my answer". With that said, I'd probably just do this:
#!/usr/bin/perl
use strict;
use warnings;
# Modules to load
use Getopt::Long;
use Term::ANSIColor qw(:constants);
my $output = 0;
my $logfile = '';
GetOptions(
'o' => \$output,
'l' => \$logfile
);
if (($output == 1) && (! $logfile eq '')){
open STDOUT, $logfile
} elsif (($output == 0)($logfile eq '')){
open STDOUT, "/dev/null"
}
... (do stuff normally here)
All I am doing is changing where STDOUT is going by either sending it to the log file or sending it to /dev/null
精彩评论