• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
There's more than one way to empty it
 

There's more than one way to empty it

on

  • 1,963 views

Funny story of how Perl people can clear an array.

Funny story of how Perl people can clear an array.

Statistics

Views

Total Views
1,963
Views on SlideShare
1,460
Embed Views
503

Actions

Likes
0
Downloads
1
Comments
0

4 Embeds 503

http://onperl.ru 455
http://www.onperl.ru 41
http://www.informatica.md 6
http://sayperl.org 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    There's more than one way to empty it There's more than one way to empty it Presentation Transcript

    • There’s more than one way to empty it
    • Perlish more-than-one-way
    • We hired Delphi/SQL programmer
    • as a Perl developer
    • Good programmer writes good code in every language
    • But lots of fun
    • sub clear{    my $template = shift @args;    @{$self->{tokens}} = ();    foreach (map {$_->{name}} @{$template->{si}}){        while (my $r = shift @{$self->{$_}->{res}}){}    } }
    • Che cazzo di..?* * WTF? (Italian)
    • sub clear{    my $template = shift @args;    @{$self->{tokens}} = ();    foreach (map {$_->{name}} @{$template->{si}}){        while (my $r = shift @{$self->{$_}->{res}}){}    } } A subroutine to make an object empty
    • sub clear{    my $template = shift @args;    @{$self->{tokens}} = ();    foreach (map {$_->{name}} @{$template->{si}}){        while (my $r = shift @{$self->{$_}->{res}}){}    } } Note this
    • Posted that piece to Moscow.pm mailing list
    • And we bacame crazy
    • 1 while (my $r = shift @a) {}
    • 2 splice @a;
    • 3 delete(@a[0..$#a]);
    • 4 grep {shift @a} @a;
    • 5 @a = ();
    • 5 @a = (); Boring!
    • 6 undef @a;
    • ccn@ccn-laptop ~ $ perl -le 'my @a=(1,2,3); while(my $r = shift @a){}; print @a' ccn@ccn-laptop ~ $ perl -le 'my @a=(0,1,2,3); while(my $r = shift @a){}; print @a' 123
    • 7 while(defined(my $r = shift @a)) {}
    • ccn@ccn-laptop ~ $ perl -le 'my @a=(undef,1,2,3);while (defined(my $r=shift @a)){};print @a' 123
    • Doesn’t work
    • Let’s make it useful
    • $str = "abcc{0}def"; @a = split //, $str; $c++ while(shift @a); say $c - 1; Calculate zero-ending string length
    • 8 while(chomp $str) {};
    • 9 while(@a) {shift @a}
    • 10 shift @a while @a; Much better!
    • 11 shift @a while @a and die;
    • 12 shift @a while scalar @a;
    • 13 $#a = -1 Who remembers?!
    • use Benchmark qw(:all :hireswallclock); my $size = 1000; Be cmpthese timethese -3, { nc    '@a = ...'          => sub { my @a = (('test')x$size); h m        return; }, ar    'undef @a'          => sub { my @a = (('test')x$size); undef @a; ke        return; }, d    '@a=()'             => sub { my @a = (('test')x$size); @a = ();        return; },    'splice @a'         => sub { my @a = (('test')x$size); splice @a;        return; },    '$#a = -1'          => sub { my @a = (('test')x$size); $#a = -1;        return; },    'shift @a while @a' => sub { my @a = (('test')x$size); shift @a while @a;  return; },    'delete @a[0..#$a]' => sub { my @a = (('test')x$size); delete(@a[0..$#a]); return; }, }; при $size=1000; shift @a while @a 2642/s                --              -22%      -44%    -44%  -45%     -45%     -45% delete @a[0..#$a] 3376/s               28%                --      -28%    -29%  -29%     -30%     -30% splice @a         4722/s               79%               40%        --     -0%   -1%      -2%      -2%
    • TIMTOWTDI or die; Andrew Shitov talks.shitov.ru | andy@shitov.ru