Your SlideShare is downloading. ×
Perl Intro 9 Command Line Arguments
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Perl Intro 9 Command Line Arguments

821
views

Published on


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

  • Be the first to like this

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

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Perl Brown Bag Command Line Arguments Shaun Griffith May 8, 200605/13/12 ATI Confidential 1
  • 2. AgendaTIMTOWTDI•Argument Types•DIY (Do It Yourself)•-s Builtin•Getopt::Long•Getopt::Declare05/13/12 2
  • 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. 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. 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. -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. 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. 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. Next Time?Suggestions?05/13/12 9