perl white magic - command line switches and special variables
DESCRIPTION
TRANSCRIPT
About this talk
What will we be talking about?
Command Line Switches
Special Variables
And others. . .
Jose Castro Perl White Magic
About this talk
Why are we talking about those things?
These things fasten your coding
These things clean your code
They’re fun!
You can’t expect to be a good programmer if you’re notcurious. . .
Jose Castro Perl White Magic
About this talk
But. . .
Haven’t we been through this already?
Well. . .
Some have, some haven’t
It’s never too much to repeat these things
You might be seeing different perspectives about these things
We might bump into something you didn’t know about. . .
Jose Castro Perl White Magic
Problem 1
Problem 1
Count words and lines in a file
#!/usr/bin/perl -wuse strict;my ($lines,$words,@words);while (<>) {
$lines++;@words = split /\W+/, $_;$words += @words;
}
print "lines -> $lines\n";print "words -> $words\n";
Jose Castro Perl White Magic
Problem 1
But wait, what are these?
$.-n-e
Jose Castro Perl White Magic
Problem 1
Hum. . .
$. Current input line (record) number-n Loop around program-e Eval script
Jose Castro Perl White Magic
Problem 1
-n
LINE:while (<>) {
... # your program goes here}
-e
Used to enter one line of program.
Jose Castro Perl White Magic
Problem 1
Problem 1
Count words and lines in a file
#!/usr/bin/perl -wuse strict;my ($lines,$words,@words);while (<>) {
$lines++;@words = split /\W+/, $_;$words += @words;
}
print "lines -> $lines\n";print "words -> $words\n";
Jose Castro Perl White Magic
Problem 1
Problem 1
Count words and lines in a file
#!/usr/bin/perl -wuse strict;my ( $words,@words);while (<>) {
@words = split /\W+/, $_;$words += @words;
}
print "lines -> $.\n";print "words -> $words\n";
Jose Castro Perl White Magic
Problem 1
Problem 1
Count words and lines in a file
#!/usr/bin/perl -wnuse strict;our( $words,@words);
@words = split /\W+/, $_;$words += @words;
END {print "lines -> $.\n";print "words -> $words\n"; }
Jose Castro Perl White Magic
Problem 1
Problem 1
Count words and lines in a file
#!/usr/bin/perl -wnuse strict;our( $w ,@w );
@w = split /\W+/ ;$w += @w;
END {print "lines -> $.\n";print "words -> $w\n"; }
Jose Castro Perl White Magic
Problem 1
Problem 1
Count words and lines in a file
#!/usr/bin/perl -wnuse strict;my @w = split /\W+/;our $w += @w;
END {print "lines -> $.\n";print "words -> $w\n"; }
Jose Castro Perl White Magic
Problem 1
Problem 1
Count words and lines in a file
#!/usr/bin/perl -wnuse strict;my @w = split /\W+/;our $w += @w;
END {print "lines -> $.\n";print "words -> $w\n"; }__END__perl -wn -e ’$w+=split/\W+/;END{
print"lines -> $.\nwords -> $w\n"}’
Jose Castro Perl White Magic
About this talk
Before coming here. . .
I rehearsed this presentation. . .
He was quite amused!
Jose Castro Perl White Magic
About this talk
See?
Jose Castro Perl White Magic
Problem 2
Problem 2
Adding a commit each 100 lines (rollback information would betoo much)
insert into table values (’1.1.0’ ,’10:30’ ,’11:30’);insert into table values (’1.1.1’ ,’08:15’ ,’08:30’);insert into table values (’1.1.2’ ,’11:00’ ,’11:45’);insert into table values (’1.1.3’ ,’07:50’ ,’09:20’);...
Jose Castro Perl White Magic
Problem 2
Problem 2
Adding a commit each 100 lines
#!/usr/bin/perl -wuse strict;
my $line;while (<>) {
$line++;unless ( $line % 100 ) {
print "commit;\n";}print;
}
Jose Castro Perl White Magic
Problem 2
Look! More stuff!
$. Current input line (record) number-e Eval script-i-p
Jose Castro Perl White Magic
Problem 2
Hum. . .
$. Current input line (record) number-e Eval script-i In-place editing-p Loop around program, printing lines
Jose Castro Perl White Magic
Problem 2
-i
Files processed by the ”<>” construct are to be edited in-place. Itdoes this by renaming the input file, opening [. . . ]
-p
LINE:while (<>) {
... # your program goes here} continue {
print or die "-p destination: $!\n";}
Jose Castro Perl White Magic
Problem 2
Problem 2
Adding a commit each 100 lines
#!/usr/bin/perl -wuse strict;
my $line;while (<>) {
$line++;unless ( $line % 100 ) {
print "commit;\n";}print;
}
Jose Castro Perl White Magic
Problem 2
Problem 2
Adding a commit each 100 lines
#!/usr/bin/perl -wuse strict;
while (<>) {
unless ( $. % 100 ) {print "commit;\n";
}print;
}
Jose Castro Perl White Magic
Problem 2
Problem 2
Adding a commit each 100 lines
#!/usr/bin/perl -wpuse strict;
unless ( $. % 100 ) {print "commit;\n";
}
Jose Castro Perl White Magic
Problem 2
Problem 2
Adding a commit each 100 lines
#!/usr/bin/perl -wpuse strict;
unless ( $. % 100 ) {print "commit;\n";
}
Jose Castro Perl White Magic
Problem 2
Problem 2
Adding a commit each 100 lines
#!/usr/bin/perl -wpuse strict;
unless ( $. % 100 ) {print "commit;\n";
}
__END__perl -wp -e ’unless ($.%100) {print"commit;\n"}’
Jose Castro Perl White Magic
Problem 2
Problem 2
Adding a commit each 100 lines
perl -wp -e ’unless ($.%100) {print"commit;\n"}’old_file > new_file
mv new_file old_file
Jose Castro Perl White Magic
Problem 2
Problem 2
Adding a commit each 100 lines
perl -wpi -e ’unless ($.%100) {print"commit;\n"}’old_file
Jose Castro Perl White Magic
About this talk
Things you should know about switches. . .
If they could speak, they would probably say:
We’re here to help.
Don’t run. We are your friends.
Jose Castro Perl White Magic
About this talk
These are not switches. . .
Jose Castro Perl White Magic
Problem 3
Problem 3
Trim lines to 80 columns
#!/usr/bin/perl -wuse strict;
while (<>) {chomp;substr($_, 80) = "";print "$_\n";
}
Jose Castro Perl White Magic
Problem 3
Wow! More?
-i In-place editing-p Loop around program, printing lines-e Eval script-l
$/$\
Jose Castro Perl White Magic
Problem 3
Sounds good. . .
-i In-place editing-p Loop around program, printing lines-e Eval script-l Automatic line-ending processing
$/ Input record separator$\ Output record separator
Jose Castro Perl White Magic
Problem 3
$/
This influences Perl’s idea of what a ”line” is.
$\Specify what is printed at the end of the print.
Jose Castro Perl White Magic
Problem 3
Problem 3
Trim lines to 80 columns
#!/usr/bin/perl -wuse strict;
while (<>) {chomp;substr($_, 80) = "";print "$_\n";
}
Jose Castro Perl White Magic
Problem 3
Problem 3
Trim lines to 80 columns
#!/usr/bin/perl -wuse strict;$\ = "\n";while (<>) {
chomp;substr($_, 80) = "";print $_ ;
}
Jose Castro Perl White Magic
Problem 3
Problem 3
Trim lines to 80 columns
#!/usr/bin/perl -wpuse strict;BEGIN { $\ = "\n"; }
chomp;substr($_, 80) = "";
Jose Castro Perl White Magic
Problem 3
-l[octnum]
Enables automatic line-ending processing.It has two separate effects.First, it automatically chomps $/ from $_:
chomp;
Second, it assigns ”$\” to have the value of ”octnum” or of ”$/”,if octnum is ommited:
$\ = $/;
Jose Castro Perl White Magic
Problem 3
Problem 3
Trim lines to 80 columns
#!/usr/bin/perl -wpuse strict;BEGIN { $\ = "\n"; }
chomp;substr($_, 80) = "";
Jose Castro Perl White Magic
Problem 3
Problem 3
Trim lines to 80 columns
#!/usr/bin/perl -wpuse strict;BEGIN { $\ = $/ ; }
chomp;substr($_, 80) = "";
Jose Castro Perl White Magic
Problem 3
Problem 3
Trim lines to 80 columns
#!/usr/bin/perl -wpluse strict;
substr($_, 80) = "";
Jose Castro Perl White Magic
Problem 3
Problem 3
Trim lines to 80 columns
#!/usr/bin/perl -wpluse strict;
substr($_, 80) = "";
Jose Castro Perl White Magic
Problem 3
Problem 3
Trim lines to 80 columns
#!/usr/bin/perl -wpluse strict;
substr($_, 80) = "";
__END__perl -wpl -e ’substr($_, 80) = ""’
Jose Castro Perl White Magic
About this talk
If this is your first time. . .
If you’re felling dizzy, remember:
You’re not the only one!
Jose Castro Perl White Magic
About this talk
Trust me!
Jose Castro Perl White Magic
Problem 4
Problem 4
Study commands from .bash history
#!/usr/bin/perl -wuse strict;
my %commands;while (<>) {
my @args = split ’ ’, $_;$commands{$args[0]}++;
}
for (keys %commands) {print "$_ -> $commands{$_}\n";
}
Jose Castro Perl White Magic
Problem 4
I was expecting this. . .
-n Loop around program-e Eval script-a
@F
Jose Castro Perl White Magic
Problem 4
Surprising. . .
-n Loop around program-e Eval script-a Autosplit mode
@F Fields of each line (only with -a)
Jose Castro Perl White Magic
Problem 4
-a
An implicit split command to the @F array is done as the firstthing inside the implicit while loop produced by the -n or -p.
perl -ane ’print pop(@F), "\n";’
is equivalent to
while (<>) {@F = split(’ ’);print pop(@F), "\n";
}
Jose Castro Perl White Magic
Problem 4
Problem 4
Study commands from .bash history
#!/usr/bin/perl -wuse strict;
my %commands;while (<>) {
my @args = split ’ ’, $_;$commands{$args[0]}++;
}
for (keys %commands) {print "$_ -> $commands{$_}\n";
}
Jose Castro Perl White Magic
Problem 4
Problem 4
Study commands from .bash history
#!/usr/bin/perl -wnuse strict;
our %commands;
my @args = split ’ ’, $_;$commands{$args[0]}++;
END {for (keys %commands) {
print "$_ -> $commands{$_}\n";} }
Jose Castro Perl White Magic
Problem 4
Problem 4
Study commands from .bash history
#!/usr/bin/perl -wnause strict;
our %commands;
$commands{$F [0]}++;
END {for (keys %commands) {
print "$_ -> $commands{$_}\n";} }
Jose Castro Perl White Magic
Problem 4
Problem 4
Study commands from .bash history
#!/usr/bin/perl -wnause strict;our %commands;
$commands{$F[0]}++;
END {for (keys %commands) {print "$_ -> $commands{$_}\n";}}
Jose Castro Perl White Magic
Problem 4
Problem 4
Study commands from .bash history
#!/usr/bin/perl -wnause strict;our %commands;
$commands{$F[0]}++;
END {for (keys %commands) {print "$_ -> $commands{$_}\n";}}__END__perl -wna -e ’$c{$F[0]}++;END{print"$_ -> $c{$_}\n"
for keys %c}’
Jose Castro Perl White Magic
About this talk
Other sources.
You should also:
Read!
Read!
Read!
Jose Castro Perl White Magic
About this talk
There are lots of good books out there!
Jose Castro Perl White Magic
Problem 5
Problem 5
List users on /etc/passwd
#!/usr/bin/perl -wuse strict;
while (<>) {my @user = split /:/, $_;print "$user[0]\n";
}
Jose Castro Perl White Magic
Problem 5
What are we using this time?
-n Loop around program-e Eval script-a Autosplit mode-l Automatic line-ending processing
@F Fields of each line (only with -a)-F
Jose Castro Perl White Magic
Problem 5
Hum. . .
-n Loop around program-e Eval script-a Autosplit mode-l Automatic line-ending processing
@F Fields of each line (only with -a)-F Specifies the pattern to split on (only with -a)
Jose Castro Perl White Magic
Problem 5
Problem 5
List users on /etc/passwd
#!/usr/bin/perl -wuse strict;
while (<>) {my @user = split /:/, $_;print "$user[0]\n";
}
Jose Castro Perl White Magic
Problem 5
Problem 5
List users on /etc/passwd
#!/usr/bin/perl -wnuse strict;
my @user = split /:/, $_;print "$user[0]\n";
Jose Castro Perl White Magic
Problem 5
Problem 5
List users on /etc/passwd
#!/usr/bin/perl -wnluse strict;
my @user = split /:/, $_;print $user[0] ;
Jose Castro Perl White Magic
Problem 5
Problem 5
List users on /etc/passwd
#!/usr/bin/perl -wnla -F:use strict;
print $F [0] ;
Jose Castro Perl White Magic
Problem 5
Problem 5
List users on /etc/passwd
#!/usr/bin/perl -wnla -F:use strict;
print $F[0];
Jose Castro Perl White Magic
Problem 5
Problem 5
List users on /etc/passwd
#!/usr/bin/perl -wnla -F:use strict;
print $F[0];
__END__perl -F: -wnlae ’print $F[0]’
Jose Castro Perl White Magic
About this talk
Too many switches?. . .
Here’s what I’d like to tell you:
You’ll get over it!
Don’t think of this as a nuisance.
Jose Castro Perl White Magic
About this talk
It only looks strange on the first times. . .
Jose Castro Perl White Magic
Problem 6
Problem 6
Create a line for /etc/passwd
#!/usr/bin/perl -wuse strict;
my @params;while (my $p = shift) {
push @params, $p;}
print join ":", @params;
Jose Castro Perl White Magic
Problem 6
Let’s see what we have this time.
-e Eval script@ARGV
$,
Jose Castro Perl White Magic
Problem 6
Hum. . .
-e Eval script@ARGV Contains the command-line arguments for the script
$, Output field separator for the print operator
Jose Castro Perl White Magic
Problem 6
Problem 6
Create a line for /etc/passwd
#!/usr/bin/perl -wuse strict;
my @params;while (my $p = shift) {
push @params, $p;}
print join ":", @params;
Jose Castro Perl White Magic
Problem 6
Problem 6
Create a line for /etc/passwd
#!/usr/bin/perl -wuse strict;
print join ":", @ARGV ;
Jose Castro Perl White Magic
Problem 6
Problem 6
Create a line for /etc/passwd
#!/usr/bin/perl -wuse strict;
$, = ":";
print @ARGV ;
Jose Castro Perl White Magic
Problem 6
Problem 6
Create a line for /etc/passwd
#!/usr/bin/perl -wuse strict;
$, = ":";
print @ARGV;
Jose Castro Perl White Magic
Problem 6
Problem 6
Create a line for /etc/passwd
#!/usr/bin/perl -wuse strict;
$, = ":";
print @ARGV;
__END__perl -e ’$,=":";print @ARGV’
Jose Castro Perl White Magic
About this talk
If this is all starting to look too much. . .
Remember:
There’s no need to worry.
You’ll get along just fine.
Jose Castro Perl White Magic
About this talk
It’s just a matter of practise!
Jose Castro Perl White Magic
Problem 7
Problem 7
List Perl modules installed
#!/usr/bin/perl -wuse strict;use ExtUtils::Installed;
for (ExtUtils::Installed->new()->modules) {print "$_\n";
}
Jose Castro Perl White Magic
Problem 7
These things never end, do they? :-)
-e Eval script-l Automatic line-ending processing
-M
Jose Castro Perl White Magic
Problem 7
And I like it.
-e Eval script-l Automatic line-ending processing
-M Use module
Jose Castro Perl White Magic
Problem 7
Problem 7
List Perl modules installed
#!/usr/bin/perl -wuse strict;use ExtUtils::Installed;
for (ExtUtils::Installed->new()->modules) {print "$_\n";
}
Jose Castro Perl White Magic
Problem 7
Problem 7
List Perl modules installed
#!/usr/bin/perl -wluse strict;use ExtUtils::Installed;
for (ExtUtils::Installed->new()->modules) {print $_ ;
}
Jose Castro Perl White Magic
Problem 7
Problem 7
List Perl modules installed
#!/usr/bin/perl -wluse strict;use ExtUtils::Installed;
print for (ExtUtils::Installed->new()->modules)
Jose Castro Perl White Magic
Problem 7
Problem 7
List Perl modules installed
#!/usr/bin/perl -wluse strict;use ExtUtils::Installed;
print for (ExtUtils::Installed->new()->modules)__END__perl -MExtUtils::Installed -wle ’print for
ExtUtils::Installed->new()->modules’
Jose Castro Perl White Magic
About this talk
Do remember all it takes is practice. . .
Important:
Be curious.
Try everything.
Jose Castro Perl White Magic
About this talk
Well. . .Maybe not everything. . .
Jose Castro Perl White Magic
About this talk
And with time. . .
Jose Castro Perl White Magic
About this talk
You’ll do the most magnificent tricks!
Jose Castro Perl White Magic
That’s it for today
This is what we went through today
$. $/ $\ $, @F -n -e -i -p -l -a -F -M @ARGV
But there is much more. . .
Jose Castro Perl White Magic
That’s it for today
This is what we went through today
$. $/ $\ $, @F -n -e -i -p -l -a -F -M @ARGV
But there is much more. . .
$& $‘ $’ $+ $^N @+ $* $| $" $; $#$% $= $- @- $~ $^ $: $^L $^A $? $!%! $^E $@ $$ $< $> $( $) $0 $[ $]$^C $^D $^F $^H $^I $^M $^0 $^P $^R $^S $^T
$^V $^W $^X$ARGV @INC @_ %INC %ENV $SIG
-0 -C -c -d -D -h -I -m -P -s-S -t -T -u -U -v -V -W -X -x
Jose Castro Perl White Magic
But that’s not all
See also
perldoc perlvar
perldoc perlrun
Jose Castro Perl White Magic
Thank you
And remember. . .
Use Perl!
Jose Castro Perl White Magic
You’re felling spleepy
And. . .
Go to Portugal!
Jose Castro Perl White Magic