Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5948927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:10:40+00:00 2026-05-22T17:10:40+00:00

I am trying to come up with some logic to give my script an

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:10:41+00:00Added an answer on May 22, 2026 at 5:10 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build a logic in c to come out from an
I'm trying to come up with some clear and concise names for a Permission
I am using VB.NET and I am trying to come up with some algorithm
I am trying to come up with the best way to render some hierarchical
I'm trying to come up with a way to make the computer do some
I'm using Stylecop to come up with some custom rules and I'm trying to
I have been trying some programs in the C Language and come across to
I am trying come up with a way to pull the tables out of
This all started because I am trying to FxCop analyze my assembly, which references
I am trying to figure out how to tackle this problem. I have to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.