Tuesday, November 4, 2014

How do we handle command line options passed to the program?

use Getopt::Long qw(GetOptions);

my %opts = ();

# Predeclare prototyped subs
sub debug(@);

GetOptions(\%opts,
    'debug!',
);

debug "Success";

sub debug(@){
  if($opts{debug}){
    (my $str=join '',@_)=~s/\s*\z/\n/;
    print $str;
  }
}

Note:
Execute the script as follows:
perl command_line_switch.pl --debug

No comments :