Perl Brown Bag             Command                Line             Arguments              Shaun Griffith               May...
AgendaTIMTOWTDI•Argument Types•DIY (Do It Yourself)•-s Builtin•Getopt::Long•Getopt::Declare05/13/12                 2
Argument TypesWhat kinds of arguments to expect?     -xyz            toggle switch     -xyz 17 value     -xyz=17 value (pa...
Criteria for Solution?What do we want a solution toaccomplish?     • Easy to use     • Argument validation        • intege...
Do It YourselfHandle arguments manually     while (@ARGV)     { my $argv = shift(@ARGV);       if ($argv =~ /^-x$/){$x=1;n...
-s OptionPerl has a builtin option for basicargument handling: -sIn the script, use it on the shebang line:#!/usr/local/bi...
Getopt::Longuse Getopt::Long;my $data = "file.dat";my $length = 24;my $verbose;$result = GetOptions(   "length=i" => $leng...
Getopt::Declareuse Getopt::Declare;my $spec = qq{   [nocase] # case insenstive options   -in <in:if>          input file  ...
Next Time?Suggestions?05/13/12                    9
Upcoming SlideShare
Loading in...5
×

Perl Intro 9 Command Line Arguments

849

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
849
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Perl Intro 9 Command Line Arguments

  1. 1. Perl Brown Bag Command Line Arguments Shaun Griffith May 8, 200605/13/12 ATI Confidential 1
  2. 2. AgendaTIMTOWTDI•Argument Types•DIY (Do It Yourself)•-s Builtin•Getopt::Long•Getopt::Declare05/13/12 2
  3. 3. Argument TypesWhat kinds of arguments to expect? -xyz toggle switch -xyz 17 value -xyz=17 value (parsing) -wx combined (-w –x) -x 17 24 list --x turn off --long double dash value bare value -- end of argumentsWhere are the arguments? @ARGV05/13/12 3
  4. 4. Criteria for Solution?What do we want a solution toaccomplish? • Easy to use • Argument validation • integers • file names • Self-documenting • Usage statement (help) • Flexible • Powerful05/13/12 4
  5. 5. Do It YourselfHandle arguments manually while (@ARGV) { my $argv = shift(@ARGV); if ($argv =~ /^-x$/){$x=1;next} if ($argv =~ /^-y$/) { $y = shift(@ARGV) or die; next; } if ($argv =~ /^--$/) { last } # stop push @files, $argv; } @ARGV = (@files, @ARGV); while (<>) { do something here }Pros: full controlCons: tedious, check arguments manually documentation? extensibility?05/13/12 5
  6. 6. -s OptionPerl has a builtin option for basicargument handling: -sIn the script, use it on the shebang line:#!/usr/local/bin/perl –s…enabling this behavior: Option Variables -xyz $xyz = 1 -xyz=blue $xyz = “blue” Pros: easy Cons: documentation? shebang combined options? check arguments manually05/13/12 6
  7. 7. Getopt::Longuse Getopt::Long;my $data = "file.dat";my $length = 24;my $verbose;$result = GetOptions( "length=i" => $length, # numeric "file=s" => $data, # string "verbose" => $verbose # flag);Pros: argument checkingCons: 24 pages of docs for G:L script documentation? wordy05/13/12 7
  8. 8. Getopt::Declareuse Getopt::Declare;my $spec = qq{ [nocase] # case insenstive options -in <in:if> input file -out <out:of> output file [mutex –in –out] -off <offset:i> offset (int) -b[ells+]w[histles] -bw also works { $::BELLS = 1; } }my $options = Getopt::Declare->new($spec);Pros: Functionality! Spec is usage Argument checking Full Parser!Cons: Functionality! 32 pages of docs for G:D! Subtle gotchas (like []) Object oriented (more work?)05/13/12 8
  9. 9. Next Time?Suggestions?05/13/12 9
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×