perl white magic - command line switches and special variables

93
Perl White Magic Jos´ e Castro [email protected] June 27, 2006 Jos´ e Castro Perl White Magic

Upload: jose-castro

Post on 15-Jan-2015

2.661 views

Category:

Technology


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Perl White Magic - Command Line Switches and Special Variables

Perl White Magic

Jose [email protected]

June 27, 2006

Jose Castro Perl White Magic

Page 2: Perl White Magic - Command Line Switches and Special Variables

About this talk

What will we be talking about?

Command Line Switches

Special Variables

And others. . .

Jose Castro Perl White Magic

Page 3: Perl White Magic - Command Line Switches and Special Variables

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

Page 4: Perl White Magic - Command Line Switches and Special Variables

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

Page 5: Perl White Magic - Command Line Switches and Special Variables

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

Page 6: Perl White Magic - Command Line Switches and Special Variables

Problem 1

But wait, what are these?

$.-n-e

Jose Castro Perl White Magic

Page 7: Perl White Magic - Command Line Switches and Special Variables

Problem 1

Hum. . .

$. Current input line (record) number-n Loop around program-e Eval script

Jose Castro Perl White Magic

Page 8: Perl White Magic - Command Line Switches and Special Variables

Problem 1

-n

LINE:while (<>) {

... # your program goes here}

-e

Used to enter one line of program.

Jose Castro Perl White Magic

Page 9: Perl White Magic - Command Line Switches and Special Variables

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

Page 10: Perl White Magic - Command Line Switches and Special Variables

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

Page 11: Perl White Magic - Command Line Switches and Special Variables

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

Page 12: Perl White Magic - Command Line Switches and Special Variables

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

Page 13: Perl White Magic - Command Line Switches and Special Variables

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

Page 14: Perl White Magic - Command Line Switches and Special Variables

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

Page 15: Perl White Magic - Command Line Switches and Special Variables

About this talk

Before coming here. . .

I rehearsed this presentation. . .

He was quite amused!

Jose Castro Perl White Magic

Page 16: Perl White Magic - Command Line Switches and Special Variables

About this talk

See?

Jose Castro Perl White Magic

Page 17: Perl White Magic - Command Line Switches and Special Variables

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

Page 18: Perl White Magic - Command Line Switches and Special Variables

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

Page 19: Perl White Magic - Command Line Switches and Special Variables

Problem 2

Look! More stuff!

$. Current input line (record) number-e Eval script-i-p

Jose Castro Perl White Magic

Page 20: Perl White Magic - Command Line Switches and Special Variables

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

Page 21: Perl White Magic - Command Line Switches and Special Variables

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

Page 22: Perl White Magic - Command Line Switches and Special Variables

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

Page 23: Perl White Magic - Command Line Switches and Special Variables

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

Page 24: Perl White Magic - Command Line Switches and Special Variables

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

Page 25: Perl White Magic - Command Line Switches and Special Variables

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

Page 26: Perl White Magic - Command Line Switches and Special Variables

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

Page 27: Perl White Magic - Command Line Switches and Special Variables

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

Page 28: Perl White Magic - Command Line Switches and Special Variables

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

Page 29: Perl White Magic - Command Line Switches and Special Variables

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

Page 30: Perl White Magic - Command Line Switches and Special Variables

About this talk

These are not switches. . .

Jose Castro Perl White Magic

Page 31: Perl White Magic - Command Line Switches and Special Variables

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

Page 32: Perl White Magic - Command Line Switches and Special Variables

Problem 3

Wow! More?

-i In-place editing-p Loop around program, printing lines-e Eval script-l

$/$\

Jose Castro Perl White Magic

Page 33: Perl White Magic - Command Line Switches and Special Variables

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

Page 34: Perl White Magic - Command Line Switches and Special Variables

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

Page 35: Perl White Magic - Command Line Switches and Special Variables

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

Page 36: Perl White Magic - Command Line Switches and Special Variables

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

Page 37: Perl White Magic - Command Line Switches and Special Variables

Problem 3

Problem 3

Trim lines to 80 columns

#!/usr/bin/perl -wpuse strict;BEGIN { $\ = "\n"; }

chomp;substr($_, 80) = "";

Jose Castro Perl White Magic

Page 38: Perl White Magic - Command Line Switches and Special Variables

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

Page 39: Perl White Magic - Command Line Switches and Special Variables

Problem 3

Problem 3

Trim lines to 80 columns

#!/usr/bin/perl -wpuse strict;BEGIN { $\ = "\n"; }

chomp;substr($_, 80) = "";

Jose Castro Perl White Magic

Page 40: Perl White Magic - Command Line Switches and Special Variables

Problem 3

Problem 3

Trim lines to 80 columns

#!/usr/bin/perl -wpuse strict;BEGIN { $\ = $/ ; }

chomp;substr($_, 80) = "";

Jose Castro Perl White Magic

Page 41: Perl White Magic - Command Line Switches and Special Variables

Problem 3

Problem 3

Trim lines to 80 columns

#!/usr/bin/perl -wpluse strict;

substr($_, 80) = "";

Jose Castro Perl White Magic

Page 42: Perl White Magic - Command Line Switches and Special Variables

Problem 3

Problem 3

Trim lines to 80 columns

#!/usr/bin/perl -wpluse strict;

substr($_, 80) = "";

Jose Castro Perl White Magic

Page 43: Perl White Magic - Command Line Switches and Special Variables

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

Page 44: Perl White Magic - Command Line Switches and Special Variables

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

Page 45: Perl White Magic - Command Line Switches and Special Variables

About this talk

Trust me!

Jose Castro Perl White Magic

Page 46: Perl White Magic - Command Line Switches and Special Variables

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

Page 47: Perl White Magic - Command Line Switches and Special Variables

Problem 4

I was expecting this. . .

-n Loop around program-e Eval script-a

@F

Jose Castro Perl White Magic

Page 48: Perl White Magic - Command Line Switches and Special Variables

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

Page 49: Perl White Magic - Command Line Switches and Special Variables

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

Page 50: Perl White Magic - Command Line Switches and Special Variables

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

Page 51: Perl White Magic - Command Line Switches and Special Variables

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

Page 52: Perl White Magic - Command Line Switches and Special Variables

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

Page 53: Perl White Magic - Command Line Switches and Special Variables

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

Page 54: Perl White Magic - Command Line Switches and Special Variables

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

Page 55: Perl White Magic - Command Line Switches and Special Variables

About this talk

Other sources.

You should also:

Read!

Read!

Read!

Jose Castro Perl White Magic

Page 56: Perl White Magic - Command Line Switches and Special Variables

About this talk

There are lots of good books out there!

Jose Castro Perl White Magic

Page 57: Perl White Magic - Command Line Switches and Special Variables

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

Page 58: Perl White Magic - Command Line Switches and Special Variables

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

Page 59: Perl White Magic - Command Line Switches and Special Variables

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

Page 60: Perl White Magic - Command Line Switches and Special Variables

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

Page 61: Perl White Magic - Command Line Switches and Special Variables

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

Page 62: Perl White Magic - Command Line Switches and Special Variables

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

Page 63: Perl White Magic - Command Line Switches and Special Variables

Problem 5

Problem 5

List users on /etc/passwd

#!/usr/bin/perl -wnla -F:use strict;

print $F [0] ;

Jose Castro Perl White Magic

Page 64: Perl White Magic - Command Line Switches and Special Variables

Problem 5

Problem 5

List users on /etc/passwd

#!/usr/bin/perl -wnla -F:use strict;

print $F[0];

Jose Castro Perl White Magic

Page 65: Perl White Magic - Command Line Switches and Special Variables

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

Page 66: Perl White Magic - Command Line Switches and Special Variables

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

Page 67: Perl White Magic - Command Line Switches and Special Variables

About this talk

It only looks strange on the first times. . .

Jose Castro Perl White Magic

Page 68: Perl White Magic - Command Line Switches and Special Variables

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

Page 69: Perl White Magic - Command Line Switches and Special Variables

Problem 6

Let’s see what we have this time.

-e Eval script@ARGV

$,

Jose Castro Perl White Magic

Page 70: Perl White Magic - Command Line Switches and Special Variables

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

Page 71: Perl White Magic - Command Line Switches and Special Variables

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

Page 72: Perl White Magic - Command Line Switches and Special Variables

Problem 6

Problem 6

Create a line for /etc/passwd

#!/usr/bin/perl -wuse strict;

print join ":", @ARGV ;

Jose Castro Perl White Magic

Page 73: Perl White Magic - Command Line Switches and Special Variables

Problem 6

Problem 6

Create a line for /etc/passwd

#!/usr/bin/perl -wuse strict;

$, = ":";

print @ARGV ;

Jose Castro Perl White Magic

Page 74: Perl White Magic - Command Line Switches and Special Variables

Problem 6

Problem 6

Create a line for /etc/passwd

#!/usr/bin/perl -wuse strict;

$, = ":";

print @ARGV;

Jose Castro Perl White Magic

Page 75: Perl White Magic - Command Line Switches and Special Variables

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

Page 76: Perl White Magic - Command Line Switches and Special Variables

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

Page 77: Perl White Magic - Command Line Switches and Special Variables

About this talk

It’s just a matter of practise!

Jose Castro Perl White Magic

Page 78: Perl White Magic - Command Line Switches and Special Variables

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

Page 79: Perl White Magic - Command Line Switches and Special Variables

Problem 7

These things never end, do they? :-)

-e Eval script-l Automatic line-ending processing

-M

Jose Castro Perl White Magic

Page 80: Perl White Magic - Command Line Switches and Special Variables

Problem 7

And I like it.

-e Eval script-l Automatic line-ending processing

-M Use module

Jose Castro Perl White Magic

Page 81: Perl White Magic - Command Line Switches and Special Variables

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

Page 82: Perl White Magic - Command Line Switches and Special Variables

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

Page 83: Perl White Magic - Command Line Switches and Special Variables

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

Page 84: Perl White Magic - Command Line Switches and Special Variables

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

Page 85: Perl White Magic - Command Line Switches and Special Variables

About this talk

Do remember all it takes is practice. . .

Important:

Be curious.

Try everything.

Jose Castro Perl White Magic

Page 86: Perl White Magic - Command Line Switches and Special Variables

About this talk

Well. . .Maybe not everything. . .

Jose Castro Perl White Magic

Page 87: Perl White Magic - Command Line Switches and Special Variables

About this talk

And with time. . .

Jose Castro Perl White Magic

Page 88: Perl White Magic - Command Line Switches and Special Variables

About this talk

You’ll do the most magnificent tricks!

Jose Castro Perl White Magic

Page 89: Perl White Magic - Command Line Switches and Special Variables

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

Page 90: Perl White Magic - Command Line Switches and Special Variables

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

Page 91: Perl White Magic - Command Line Switches and Special Variables

But that’s not all

See also

perldoc perlvar

perldoc perlrun

Jose Castro Perl White Magic

Page 92: Perl White Magic - Command Line Switches and Special Variables

Thank you

And remember. . .

Use Perl!

Jose Castro Perl White Magic

Page 93: Perl White Magic - Command Line Switches and Special Variables

You’re felling spleepy

And. . .

Go to Portugal!

Jose Castro Perl White Magic