You still seem a bit confused so lets try to start over straight
to the point:

On Fri, Mar 18, 2011 at 4:31 PM, shawn wilson <ag4ve...@gmail.com> wrote:
> i ran across a peace of interesting code:
>
> my $writer = shift->(
>  [ 200, [ "Content-type" => "text/plain" ], $s ]
> );

To understand just this piece of code, ignore where it came from
and lets fill in the blanks with something simpler.

bamccaig@castopulence:~$ cat | perl
#!/usr/bin/env perl

use strict;
use warnings;

use Data::Dumper;

sub foo
{
    my ($array_ref) = @_;
    my ($status, $headers, $blah) = @{$array_ref};

    return {
        status => $status,
        headers => $headers,
        blah => $blah
    };
}

sub bar
{
    # The original code was this (wrapped for readability):
    #   my writer = shift->(
    #           [200, ['Content-Type' => 'text/plain'], $s]);
    #
    # Lets create an arbitrary $s (understanding this snippet
    # doesn't require knowing what $s actually is).
    my $s = 5;

    # As seen in previous posts, shift->(...) is shifting @_
    # and dereferencing the first argument as a subroutine.
    # Let's explicitly shift into a variable for clarity.
    my $foo = shift @_;

    # The dereferenced subroutine was passed an array reference,
    # the array of which contained a number, another array
    # reference, and whatever $s is. For clarity, lets assign
    # that array reference to a variable too.
    my $arg = [200, ['Content-Type', 'text/plain'], $s];

    # Now we finally get to see what's happening with all of
    # the clutter removed. A subroutine reference, $foo, is
    # being called with the argument, $arg.
    my $writer = $foo->($arg);

    # $sub_ref->() can also be written as &{$sub_ref}(), so this
    # is the same as the previous statement (albeit, less
    # readable):
    my $writer2 = &{$foo}($arg);

    # Finally, print what the result is so you can see for
    # yourself. It isn't the original array reference passed to
    # the dereferenced subroutine, but whatever that subroutine
    # decided to return. In my case, I transformed the array
    # reference into a hash reference.
    print Dumper $writer2;
}

sub bar2
{
    # Now that we understand how the code works, let's see it in
    # action as it was originally written. Again, we'll replace
    # $s with a random scalar since we still don't care what it
    # actually is.
    my $s = 5;

    my $writer = shift->(
            [200, ['Content-Type' => 'text/plain'], $s]);

    print Dumper $writer;
}

print "bar:\n\n";

# We've seen that bar expects a subroutine reference as the first
# argument, so that's what we're going to give it.
bar \&foo;

print "\nbar2:\n\n";

# Again, we pass in a reference too our subroutine, foo.
bar2 \&foo;

__END__
bar:

$VAR1 = {
          'blah' => 5,
          'headers' => [
                         'Content-Type',
                         'text/plain'
                       ],
          'status' => 200
        };

bar2:

$VAR1 = {
          'blah' => 5,
          'headers' => [
                         'Content-Type',
                         'text/plain'
                       ],
          'status' => 200
        };
bamccaig@castopulence:~$


HTH.


-- 
Brandon McCaig <http://www.bamccaig.com/> <bamcc...@gmail.com>
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software <http://www.castopulence.org/> <bamcc...@castopulence.org>

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to