shift oo
--0022159751aa174c1b049ec7a99b
Content-Type: text/plain; charset=ISO-8859-1
i ran across a peace of interesting code:
my $writer = shift->(
[ 200, [ "Content-type" => "text/plain" ], $s ]
);
so, if i understand this correctly, this would be the same as
my $writer = sub {
my $a = shift;
my $thing = sub {
my $subthing = $writer->{ $a };
return [ 200, [ "Content-type" => "text/plain" ], $subthing ];
}
}
.... or something like that. what is happening there, or how should i think
about it? it seems like shift is returning $writer->[ $array ] maybe? i
think using shift as a method like that is messing me up.
--0022159751aa174c1b049ec7a99b--
Re: shift oo
>>>>> "sw" == shawn wilson <ag4ve.us [at] gmail.com> writes:
sw> i ran across a peace of interesting code:
sw> my $writer = shift->(
sw> [ 200, [ "Content-type" => "text/plain" ], $s ]
sw> );
first off, there is no OO anywhere in that code. all it is is a
dereference of a code reference passed in to a sub.
sw> so, if i understand this correctly, this would be the same as
sw> my $writer = sub {
sw> my $a = shift;
sw> my $thing = sub {
sw> my $subthing = $writer->{ $a };
sw> return [ 200, [ "Content-type" => "text/plain" ], $subthing ];
sw> }
sw> }
not even close. there is no anon sub being created in the original code,
rather one is being passed in to the sub.
sw> ... or something like that. what is happening there, or how should i think
sw> about it? it seems like shift is returning $writer->[ $array ] maybe? i
sw> think using shift as a method like that is messing me up.
there is NO method there so it isn't a method call. if you have a code
reference (to a regular sub) in $code then you dereference it like this:
$code->( args ... ) ;
all the code does is skip assigning the code ref from [at] _ into $code. i
generally avoid that style of directly using the shift in an
expression. it is better style to store it in a variable so you have
some extra names to describe what that value is.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: shift oo
On 11-03-18 04:31 PM, shawn wilson wrote:
> my $writer = shift->(
> [ 200, [ "Content-type" => "text/plain" ], $s ]
> );
shift will shift [at] _ in a sub and [at] ARGV outside of one. So the first
question is this inside a sub or not?
The first item in the array ( [at] _ or [at] ARGV) has to be a reference to a
sub, which is given one argument: [ 200, [ "Content-type" =>
"text/plain" ], $s ] a reference to an array of three elements, the
second one being a reference to another array which has two elements.
Its returned value is placed in $writer.
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: shift oo
--0016e6db2cc8cb8144049ec8220a
Content-Type: text/plain; charset=ISO-8859-1
On Fri, Mar 18, 2011 at 4:50 PM, Uri Guttman <uri [at] stemsystems.com> wrote:
> >>>>> "sw" == shawn wilson <ag4ve.us [at] gmail.com> writes:
>
> sw> i ran across a peace of interesting code:
> sw> my $writer = shift->(
> sw> [ 200, [ "Content-type" => "text/plain" ], $s ]
> sw> );
>
> first off, there is no OO anywhere in that code. all it is is a
> dereference of a code reference passed in to a sub.
>
>
[snip]
there is NO method there so it isn't a method call. if you have a code
> reference (to a regular sub) in $code then you dereference it like this:
>
> $code->( args ... ) ;
>
> all the code does is skip assigning the code ref from [at] _ into $code. i
> generally avoid that style of directly using the shift in an
> expression. it is better style to store it in a variable so you have
> some extra names to describe what that value is.
>
>
> so, what your saying is:
my $writer = sub {
my $a = shift;
return [ 200, [ "Content-type" => "text/plain" ], $s ];
}
???
--0016e6db2cc8cb8144049ec8220a--
Re: shift oo
--00248c0ef198061c97049ec82eb4
Content-Type: text/plain; charset=ISO-8859-1
On Fri, Mar 18, 2011 at 4:51 PM, Shawn H Corey <shawnhcorey [at] gmail.com>wrote:
> On 11-03-18 04:31 PM, shawn wilson wrote:
>
>> my $writer = shift->(
>> [ 200, [ "Content-type" => "text/plain" ], $s ]
>> );
>>
>
> shift will shift [at] _ in a sub and [at] ARGV outside of one. So the first
> question is this inside a sub or not?
>
>
> this is where this comes from:
http://www.samuelkaufman.com/blog/2011/03/16/streaming-with- plack-take-2/
--00248c0ef198061c97049ec82eb4--
Re: shift oo
>>>>> "sw" == shawn wilson <ag4ve.us [at] gmail.com> writes:
please learn how to quote emails properly. it is hard to tell here what
i replied and what you wrote.
sw> On Fri, Mar 18, 2011 at 4:50 PM, Uri Guttman <uri [at] stemsystems.com> wrote:
>> >>>>> "sw" == shawn wilson <ag4ve.us [at] gmail.com> writes:
>>
sw> i ran across a peace of interesting code:
sw> my $writer = shift->(
sw> [ 200, [ "Content-type" => "text/plain" ], $s ]
sw> );
>>
>> first off, there is no OO anywhere in that code. all it is is a
>> dereference of a code reference passed in to a sub.
>>
>>
sw> [snip]
sw> there is NO method there so it isn't a method call. if you have a code
>> reference (to a regular sub) in $code then you dereference it like this:
>>
>> $code->( args ... ) ;
>>
>> all the code does is skip assigning the code ref from [at] _ into $code. i
>> generally avoid that style of directly using the shift in an
>> expression. it is better style to store it in a variable so you have
>> some extra names to describe what that value is.
>>
>>
>> so, what your saying is:
sw> my $writer = sub {
sw> my $a = shift;
sw> return [ 200, [ "Content-type" => "text/plain" ], $s ];
sw> }
no. where is the use of $a? where is the code call that dereferences the
ref?
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: shift oo
On 11-03-18 05:05 PM, shawn wilson wrote:
> my $writer = sub {
> my $a = shift;
> return [ 200, [ "Content-type" => "text/plain" ], $s ];
> }
Try:
$sub_ref = shift [at] _;
my $writer = $subref->( [ 200, [ "Content-type" => "text/plain" ], $s ] );
The shift is shifting [at] _; it should be written as:
my $writer = (shift [at] _)->( ...
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: shift oo
>>>>> "SHC" == Shawn H Corey <shawnhcorey [at] gmail.com> writes:
SHC> On 11-03-18 04:31 PM, shawn wilson wrote:
>> my $writer = shift->(
>> [ 200, [ "Content-type" => "text/plain" ], $s ]
>> );
SHC> shift will shift [at] _ in a sub and [at] ARGV outside of one. So the first
SHC> question is this inside a sub or not?
no, that cannot be a valid question. [at] ARGV can't hold a code reference
(unless some nutcase pushed one in there!). it can only hold strings
from the command line. so this has to be inside a sub.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: shift oo
--00248c0ef198414db3049ec889e3
Content-Type: text/plain; charset=ISO-8859-1
On Fri, Mar 18, 2011 at 5:23 PM, Uri Guttman <uri [at] stemsystems.com> wrote:
> >>>>> "sw" == shawn wilson <ag4ve.us [at] gmail.com> writes:
>
> please learn how to quote emails properly. it is hard to tell here what
> i replied and what you wrote.
>
> sorry, i don't know how to do any better in gmail (and does it different on
the gmail app on my android too - sorta messed up). should i color replies
differently or something?
> >>
> >> so, what your saying is:
>
sw> my $writer = sub {
> sw> my $a = shift;
> sw> return [ 200, [ "Content-type" => "text/plain" ], $s ];
> sw> }
>
> no. where is the use of $a? where is the code call that dereferences the
> ref?
>
>
here's the whole function (i didn't think i needed to post it because i get
the rest of this code):
my $s = Streamer->new;
my $app = sub {
return sub {
$s->open_fh;
my $writer = shift->(
[ 200, [ "Content-type" => "text/plain" ], $s ]
);
};
};
--00248c0ef198414db3049ec889e3--
Re: shift oo
>>>>> "sw" == shawn wilson <ag4ve.us [at] gmail.com> writes:
sw> On Fri, Mar 18, 2011 at 5:23 PM, Uri Guttman <uri [at] stemsystems.com> wrote:
>> >>>>> "sw" == shawn wilson <ag4ve.us [at] gmail.com> writes:
>>
>> please learn how to quote emails properly. it is hard to tell here what
>> i replied and what you wrote.
>>
>> sorry, i don't know how to do any better in gmail (and does it different on
sw> the gmail app on my android too - sorta messed up). should i color replies
sw> differently or something?
don't use colors. use indents with markers like email has always
had. use a better emailer as well.
>> >>
>> >> so, what your saying is:
>>
sw> my $writer = sub {
sw> my $a = shift;
sw> return [ 200, [ "Content-type" => "text/plain" ], $s ];
sw> }
>>
>> no. where is the use of $a? where is the code call that dereferences the
>> ref?
>>
>>
i don't need to see the original code. my question was about your
rewrite so you could understand it. notice i mentioned $a explicitly as
you used it but it wasn't in the original code. so answer my questions
in that context. you need to show you understand the code
dereference. hint: the shift and code deref are two separate things.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: shift oo
--20cf300258cc76cd13049ec93306
Content-Type: text/plain; charset=ISO-8859-1
On Fri, Mar 18, 2011 at 5:45 PM, Uri Guttman <uri [at] stemsystems.com> wrote:
> >>>>> "sw" == shawn wilson <ag4ve.us [at] gmail.com> writes:
>
>
> sw> On Fri, Mar 18, 2011 at 5:23 PM, Uri Guttman <uri [at] stemsystems.com>
> wrote:
> >> >>>>> "sw" == shawn wilson <ag4ve.us [at] gmail.com> writes:
> >> >>
> >> >> so, what your saying is:
> >>
> sw> my $writer = sub {
> sw> my $a = shift;
> sw> return [ 200, [ "Content-type" => "text/plain" ], $s ];
> sw> }
> >>
> >> no. where is the use of $a? where is the code call that dereferences
> the
> >> ref?
> >>
> >>
>
> > i don't need to see the original code. my question was about your
> > rewrite so you could understand it. notice i mentioned $a explicitly as
> > you used it but it wasn't in the original code. so answer my questions
> > in that context. you need to show you understand the code
> > dereference. hint: the shift and code deref are two separate things.
>
>
> ok, taking another crack at it (from the top this time):
my $a = Streamer->new;
my $app = sub {
return [ 200, [ "Content-type" => "text/plain" ], $a->open_fh ];
}
.... i think, but a part of me is thinking that if it were that simple, it
would have written like that (bad reasoning for thinking i'm wrong, but...)
--20cf300258cc76cd13049ec93306--
Re: shift oo
--002215975af2916ec0049ec93d47
Content-Type: text/plain; charset=ISO-8859-1
On Fri, Mar 18, 2011 at 6:21 PM, shawn wilson <ag4ve.us [at] gmail.com> wrote:
>
>
> On Fri, Mar 18, 2011 at 5:45 PM, Uri Guttman <uri [at] stemsystems.com> wrote:
>
>> >>>>> "sw" == shawn wilson <ag4ve.us [at] gmail.com> writes:
>>
>>
>> sw> On Fri, Mar 18, 2011 at 5:23 PM, Uri Guttman <uri [at] stemsystems.com>
>> wrote:
>> >> >>>>> "sw" == shawn wilson <ag4ve.us [at] gmail.com> writes:
>>
>>
> ok, taking another crack at it (from the top this time):
>
> my $a = Streamer->new;
> my $app = sub {
> return [ 200, [ "Content-type" => "text/plain" ], $a->open_fh ];
> }
>
> ... i think, but a part of me is thinking that if it were that simple, it
> would have written like that (bad reasoning for thinking i'm wrong, but...)
>
oh, crap....
my $a = Streamer->new;
my $app = sub {
my $a = shift; # <---- i think...
return [ 200, [ "Content-type" => "text/plain" ], $a->open_fh ];
}
--002215975af2916ec0049ec93d47--
Re: shift oo
On 11-03-18 06:21 PM, shawn wilson wrote:
> my $a = Streamer->new;
> my $app = sub {
> return [ 200, [ "Content-type" => "text/plain" ], $a->open_fh ];
> }
>
> ... i think, but a part of me is thinking that if it were that simple, it
> would have written like that (bad reasoning for thinking i'm wrong, but...)
No, the [ 200, [ "Content-type" => "text/plain" ], $a->open_fh ] is an
argument to the sub, not the return value.
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: shift oo
--20cf30025ba2cf16d9049ec97b46
Content-Type: text/plain; charset=ISO-8859-1
On Fri, Mar 18, 2011 at 6:36 PM, Shawn H Corey <shawnhcorey [at] gmail.com>wrote:
> On 11-03-18 06:21 PM, shawn wilson wrote:
>
>> my $a = Streamer->new;
>> my $app = sub {
>> return [ 200, [ "Content-type" => "text/plain" ], $a->open_fh ];
>> }
>>
>> ... i think, but a part of me is thinking that if it were that simple, it
>> would have written like that (bad reasoning for thinking i'm wrong,
>> but...)
>>
>
> No, the [ 200, [ "Content-type" => "text/plain" ], $a->open_fh ] is an
> argument to the sub, not the return value.
>
>
>
an argument to what sub?
(it's obvious that i've missed the boat on this concept)
--20cf30025ba2cf16d9049ec97b46--
Re: shift oo
On 11-03-18 06:41 PM, shawn wilson wrote:
> an argument to what sub?
> (it's obvious that i've missed the boat on this concept)
my $writer = shift->(
[ 200, [ "Content-type" => "text/plain" ], $s ]
);
The array [at] _ contains a sub ref as its first argument. It is this sub
that gets [ 200, [ "Content-type" => "text/plain" ], $s ] as its argument.
It can be rewritten as:
my sub_ref = shift [at] _;
my $writer = $sub_ref->(
[ 200, [ "Content-type" => "text/plain" ], $s ]
);
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: shift oo
--000e0cd2374e61c1cc049eca1d5f
Content-Type: text/plain; charset=ISO-8859-1
On Fri, Mar 18, 2011 at 7:02 PM, Shawn H Corey <shawnhcorey [at] gmail.com>wrote:
> On 11-03-18 06:41 PM, shawn wilson wrote:
>
>> an argument to what sub?
>> (it's obvious that i've missed the boat on this concept)
>>
>
> my $writer = shift->(
> [ 200, [ "Content-type" => "text/plain" ], $s ]
> );
>
> The array [at] _ contains a sub ref as its first argument. It is this sub that
> gets [ 200, [ "Content-type" => "text/plain" ], $s ] as its argument.
>
> It can be rewritten as:
>
> my sub_ref = shift [at] _;
> my $writer = $sub_ref->(
> [ 200, [ "Content-type" => "text/plain" ], $s ]
> );
>
oh, that's right, i forgot the general oo use of:
my( $self, [at] etc ) = [at] _;
that almost makes sense. the only part i'm still confused about is why he
defined $writer when you're not going to do anything with it? that just gets
returned, right?
so,
my( $self, [at] more ) = [at] _;
my [at] arr = [ 200, [ "Content-type" => "text/plain" ], $s ];
$self->( [at] arr );
right? though, i've never actually used $self, i've always just defined it
because i knew that was the first thing that was passed to a sub in [at] _ (or,
at some point i knew this anyway :) )
maybe i should reread perlboot or perlsub, eh?
--000e0cd2374e61c1cc049eca1d5f--
Re: shift oo
>>>>> "sw" == shawn wilson <ag4ve.us [at] gmail.com> writes:
sw> On Fri, Mar 18, 2011 at 7:02 PM, Shawn H Corey <shawnhcorey [at] gmail.com>wrote:
>> my sub_ref = shift [at] _;
>> my $writer = $sub_ref->(
>> [ 200, [ "Content-type" => "text/plain" ], $s ]
>> );
sw> oh, that's right, i forgot the general oo use of:
sw> my( $self, [at] etc ) = [at] _;
again, that IS NOT OO. nothing in this thread was OO. why do you keep
saying it is OO? there is not one bit of OO here. this is simple perl
references and dereferencing stuff. it just happens to be a code
ref. here is a clue:
$foo->{bar} ;
$foo->[10] ;
$foo->( $bar ) ;
see? all have the same basic syntax but the paired stuff after the ->
tells you what kind of dereference you are doing. the symmetry is there
for a good reason - it makes it easier to learn the same technique for 3
different reference types.
sw> that almost makes sense. the only part i'm still confused about is
sw> why he defined $writer when you're not going to do anything with
sw> it? that just gets returned, right?
who defined what writer? please show code when you make comments like
that. we have had lots of snippets here and i can't locate which code
piece you are commenting about. as i have told you, communication is
key. don't let pronouns dangle just like don't use $_ unless you have
too. same concept. be as clear and unambiguous as you can when writing
english or code.
sw> so,
sw> my( $self, [at] more ) = [at] _;
why $self? this is NOT OO. it is a simple sub call. it should be called
$code or $sub or something similar - preferably a name saying what the
sub is used for.
sw> my [at] arr = [ 200, [ "Content-type" => "text/plain" ], $s ];
why are you assigning that to an array? the value is an array ref and
can be stored in a scalar.
sw> $self->( [at] arr );
now you pass an array but it has only that one element. there is no need
for that temp array. also calling it [at] arr is poor as it says nothing
about the data in the array.
sw> right? though, i've never actually used $self, i've always just
sw> defined it because i knew that was the first thing that was passed
sw> to a sub in [at] _ (or, at some point i knew this anyway :) )
well, until you actually learn OO (which this is not), you avoid
$self. it is just the default common name for objects passed to method.
sw> maybe i should reread perlboot or perlsub, eh?
no, those are very different topics. read perlsub so you can learn more
about subs. read perlref (and perlreftut) so you can learn more about
refs and how to dereference them. read perlboot ONLY when you are about
to learn OO perl. as i keep saying (and you keep ignoring) this thread
had nothing to do with OO perl.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: shift oo
On Fri, 18 Mar 2011 17:34:00 -0400, shawn wilson wrote:
> here's the whole function (i didn't think i needed to post it because i
> get the rest of this code):
Your problem is less to do with Perl than it is with explaining what you
need clearly. Clear thinking and clear communication lead to clear code
so this is an important thing to work on. This thread resembled a bad
rewrite of the Abbott & Costello "Who's on First?" skit.
> my $s =3D Streamer->new;
> my $app =3D sub {
> return sub {
> $s->open_fh;
> my $writer =3D shift->(
> [ 200, [ "Content-type" =3D> "text/plain" ], $s ]
> );
> };
> };
That is a fancy piece of code somewhat beyond the usual definition of
"beginner". Context is important; you haven't provided all of the
context we need to understand this so there is going to be some guesswork=
in the explanation.
This code creates a "Streamer" object in $s. Then it populates $app with=
a reference to a subroutine that when called, will return a reference to
a subroutine that first calls the open_fh() method of $s, then creates in=
$writer the result of calling the first argument (to the inner
subroutine) as a code reference, passing it a reference to an array
containing 200, another array ref, and the $s object.
As it stands, this doesn't make sense because nothing happens to $writer;=
so why create it? The code certainly does something but either there is
code that you haven't shown us or the writer of this code wasn't thinking=
clearly in putting $writer in there.
There are two code refs created and another one passed in these few
lines; it is very compact. Use of this code would go something like this=
:
$other_ref =3D sub {
my $ar =3D shift;
my ($status, $header_ref, $streamer) =3D [at] $ar;
my [at] headers =3D [at] $header_ref;
# Do something with those variables...
};
$coderef =3D $app->();
$coderef->( $other_ref );
$other_ref must be a reference to a subroutine for this code to work. It=
is not therefore an object (technically, it could be - it could be
blessed - but that is beside the point, nothing in this code expects it
to be an object).
You need to be very conversant with code references and functional
programming to be comfortable with this code.
--
Peter Scott
http://www.perlmedic.com/ http://www.perldebugged.com/
http://www.informit.com/store/product.aspx?isbn=3D0137001274
http://www.oreillyschool.com/courses/perl3/
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: shift oo
--0015173fe52a23489e049edab281
Content-Type: text/plain; charset=ISO-8859-1
thank you all. i have a much better grasp on what this means now. at least i
know why i had trouble with it - i didn't (don't) understand closures. and,
i am not used to event driven programming. the example comes from psgi which
(generally) gets an event and gives you data. i've also been looking into
anyevent (following the rabbit hole) to try to figure out event programming
better. however, i see now that i need to take a few steps back and
understand closures first.
if i understand what everyone here has said and what i've been reading, he
needs to keep $s in scope with the first closure (i suppose to keep the
place of the file handle), and then uses $writer to get that string.
so, i'd access that by doing
my $appfunc = $app;
my $fhsub = $appfunc;
my $writer = fhsub( $thing_to_pass_shift );
print $writer;
now, i'm not too far to reading (so maybe this is a stupid question, but) he
shifts $_[ 0 ]; into that last closure. what happens to that value? why not
just:
return sub{ [ 200, [ "Content-type" => "text/plain" ], $s ] };
ps - i still feel like a beginner :)
reference:
my $s = Streamer->new;
my $app = sub {
return sub {
$s->open_fh;
my $writer = shift->(
[ 200, [ "Content-type" => "text/plain" ], $s ]
);
};
};
--0015173fe52a23489e049edab281--
Re: shift oo
>>>>> "sw" == shawn wilson <ag4ve.us [at] gmail.com> writes:
sw> thank you all. i have a much better grasp on what this means
sw> now. at least i know why i had trouble with it - i didn't (don't)
sw> understand closures. and, i am not used to event driven
sw> programming. the example comes from psgi which (generally) gets an
sw> event and gives you data. i've also been looking into anyevent
sw> (following the rabbit hole) to try to figure out event programming
sw> better. however, i see now that i need to take a few steps back
sw> and understand closures first.
please keep the various things clear. the original code you posted
(without the surrounding sub) was not OO, nor a closure, nor an event
handler. your original question was about shift->() and only that. don't
keep wandering around all these other things until you fully get what
that was. i still am not sure if you got it as each of your rewrites was
off target. the outer context and closure stuff came in but i never saw
you grasp the code dereferenced concept or syntax. you kept saying OO
when it wasn't OO.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: shift oo
--001485f6c82215c221049ee17e47
Content-Type: text/plain; charset=ISO-8859-1
On Sat, Mar 19, 2011 at 10:46 PM, Uri Guttman <uri [at] stemsystems.com> wrote:
> >>>>> "sw" == shawn wilson <ag4ve.us [at] gmail.com> writes:
>
> sw> thank you all. i have a much better grasp on what this means
> sw> now. at least i know why i had trouble with it - i didn't (don't)
> sw> understand closures. and, i am not used to event driven
> sw> programming. the example comes from psgi which (generally) gets an
> sw> event and gives you data. i've also been looking into anyevent
> sw> (following the rabbit hole) to try to figure out event programming
> sw> better. however, i see now that i need to take a few steps back
> sw> and understand closures first.
>
> uri> please keep the various things clear. the original code you posted
> uri> (without the surrounding sub) was not OO, nor a closure, nor an event
> uri> handler. your original question was about shift->() and only that.
> don't
> uri> keep wandering around all these other things until you fully get what
> uri> that was. i still am not sure if you got it as each of your rewrites
> was
> uri> off target. the outer context and closure stuff came in but i never
> saw
> uri> you grasp the code dereferenced concept or syntax. you kept saying OO
> uri> when it wasn't OO.
>
>
>
yes, it's not oo anything - i get that. from my last response:
he shifts $_[ 0 ]; into that last closure. what happens to that value? why
not just:
return sub{ [ 200, [ "Content-type" => "text/plain" ], $s ] };
so, i'm still curious why:
my $ref = shift;
my $writer = $ref->{ [ 200, [ "Content-type" => "text/plain" ], $s ] };
why is writer defined to that $ref with those variables? why not just define
$writer? or better yet, just 'return' the array? as it is, you'd have to
dereference $writer in order to access anything in that index. i just don't
get that.
--001485f6c82215c221049ee17e47--
Re: shift oo
>>>>> "sw" == shawn wilson <ag4ve.us [at] gmail.com> writes:
uri> please keep the various things clear. the original code you
uri> posted (without the surrounding sub) was not OO, nor a closure,
uri> nor an event handler. your original question was about shift->()
uri> and only that.
>> don't
uri> keep wandering around all these other things until you fully get what
uri> that was. i still am not sure if you got it as each of your rewrites
>> was
uri> off target. the outer context and closure stuff came in but i never
>> saw
uri> you grasp the code dereferenced concept or syntax. you kept saying OO
uri> when it wasn't OO.
sw> yes, it's not oo anything - i get that. from my last response:
sw> he shifts $_[ 0 ]; into that last closure. what happens to that value? why
sw> not just:
sw> return sub{ [ 200, [ "Content-type" => "text/plain" ], $s ] };
again, you are confusing two different issues. please stop that as it
is slowing down your learning perl. the shift->() is a pure syntax
issue. the closure wasn't even IN THE CODE you first posted. they are
separate things and should be addressed separately.
here is your original post to refresh your memory:
***********************
i ran across a peace of interesting code:
my $writer = shift->(
[ 200, [ "Content-type" => "text/plain" ], $s ]
);
so, if i understand this correctly, this would be the same as
my $writer = sub {
my $a = shift;
my $thing = sub {
my $subthing = $writer->{ $a };
return [ 200, [ "Content-type" => "text/plain" ], $subthing ];
}
}
***********************
the first part has no closure, no OO, nothing but a simple code
dereference with a shift making it more obscure. you claimed to rewrite
it (but it seems you quoted more code context) and that has a closure
and much more complex code. that is why you are always jumping
around. did you ask about the dereference? why bring up the closure but
you never asked about that in the beginning. stick to one topic at a
time.
sw> so, i'm still curious why:
sw> my $ref = shift;
sw> my $writer = $ref->{ [ 200, [ "Content-type" => "text/plain" ], $s ] };
sw> why is writer defined to that $ref with those variables? why not
sw> just define $writer? or better yet, just 'return' the array? as it
sw> is, you'd have to dereference $writer in order to access anything
sw> in that index. i just don't get that.
oy. look at the code i pasted from you. look carefully at the char
following the shift->. what is that char? what is the char following
$ref-> in your above code? are they the same? if not, what is the
difference?
as i keep saying, you haven't learned the basics of code references and
dereferencing. stick to that topic BEFORE you get into closures. why?
because closures are independent of that and they are more
complex. master one concept before you jump to another. you are not
learning here, you are just splashing around in a kiddie pool.
now address the code dereference issue. drop all the others for
now. learn this first. learn it well. then move on.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: shift oo
>>>>> "Peter" == Peter Scott <Peter [at] PSDT.com> writes:
>> my $s = Streamer->new;
>> my $app = sub {
>> return sub {
>> $s->open_fh;
>> my $writer = shift->(
>> [ 200, [ "Content-type" => "text/plain" ], $s ]
>> );
>> };
>> };
Peter> As it stands, this doesn't make sense because nothing happens to $writer;
Peter> so why create it?
I presume you're objecting to the explicit $writer. Certainly, the
value of $writer is also the return value of the inner subroutine, so
that *is* something that could be noted:
my $whatever_writer_was = $app->(somearg_for_that_inner_shift);
This looks like the PSGI interface. Would have been nice to spell that
out too, since there's a wealth of info on that already.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn [at] stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: shift oo
--001636b2b72ad8049c049f1900c1
Content-Type: text/plain; charset=ISO-8859-1
On Mar 22, 2011 4:43 PM, "Randal L. Schwartz" <merlyn [at] stonehenge.com> wrote:
>
> >>>>> "Peter" == Peter Scott <Peter [at] PSDT.com> writes:
>
> >> my $s = Streamer->new;
> >> my $app = sub {
> >> return sub {
> >> $s->open_fh;
> >> my $writer = shift->(
> >> [ 200, [ "Content-type" => "text/plain" ], $s ]
> >> );
> >> };
> >> };
>
> Peter> As it stands, this doesn't make sense because nothing happens to
$writer;
> Peter> so why create it?
>
> I presume you're objecting to the explicit $writer. Certainly, the
> value of $writer is also the return value of the inner subroutine, so
> that *is* something that could be noted:
>
> my $whatever_writer_was = $app->(somearg_for_that_inner_shift);
>
> This looks like the PSGI interface. Would have been nice to spell that
> out too, since there's a wealth of info on that already.
>
> --
Yes, it would appear that there was much more to this than I originally
suspected. It would seek that there is probably plack::builder and:
builder {
mount "/whatever" => $app;
}
And maybe more. Either way this was a good for a reference (sorta, kinda, ah
maybe not even that :) ). But, I did learn some cool stuff here (I think). I
got working what I wanted to get working and am now looking at ae::redis to
stream when I want.
I should probably go back at some point and make sure I know the basics.
However, for now, getting this stuff to work is more funner.
I learned a new way to use shift here (or probably any function that uses
$_) and I have (sorta learned about closures.
Now, I'm pretty sure that what was being shifted was a method probably $req
= Plack::Request->($env); and $req was being or gets passed from builder.
I might be new here but I think this is getting ot and I've been told that
plack is a month or so from 'supported' so I'll go now. Thank yall for the
help.
--001636b2b72ad8049c049f1900c1--
Re: shift oo
>>>>> "sw" == shawn wilson <ag4ve.us [at] gmail.com> writes:
sw> I learned a new way to use shift here (or probably any function
sw> that uses $_) and I have (sorta learned about closures.
you are doing it again. $_ and [at] _ have nothing to do with each
other. and shift never touches $_. shift works on its array argument or
[at] _ inside a sub or [at] ARGV outside a sub if no argument is passed. you
haven't learned diddly about shift IMNSHO if you keep saying wrong
things like this.
sw> Now, I'm pretty sure that what was being shifted was a method probably $req
sw> = Plack::Request->($env); and $req was being or gets passed from builder.
it doesn't matter what was shifted. what matters is that you understand
shift independently from the surrounding code. you haven't demonstrated
that to me yet.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: shift oo
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.us [at] gmail.com> wrote:
> i ran across a peace of interesting code:
>
> my $writer =3D shift->(
> =C2=A0[ 200, [ "Content-type" =3D> "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 [at] castopulence:~$ cat | perl
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
sub foo
{
my ($array_ref) =3D [at] _;
my ($status, $headers, $blah) =3D [at] {$array_ref};
return {
status =3D> $status,
headers =3D> $headers,
blah =3D> $blah
};
}
sub bar
{
# The original code was this (wrapped for readability):
# my writer =3D shift->(
# [200, ['Content-Type' =3D> 'text/plain'], $s]);
#
# Lets create an arbitrary $s (understanding this snippet
# doesn't require knowing what $s actually is).
my $s =3D 5;
# As seen in previous posts, shift->(...) is shifting [at] _
# and dereferencing the first argument as a subroutine.
# Let's explicitly shift into a variable for clarity.
my $foo =3D shift [at] _;
# 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 =3D [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 =3D $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 =3D &{$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 =3D 5;
my $writer =3D shift->(
[200, ['Content-Type' =3D> '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 =3D {
'blah' =3D> 5,
'headers' =3D> [
'Content-Type',
'text/plain'
],
'status' =3D> 200
};
bar2:
$VAR1 =3D {
'blah' =3D> 5,
'headers' =3D> [
'Content-Type',
'text/plain'
],
'status' =3D> 200
};
bamccaig [at] castopulence:~$
HTH.
--
Brandon McCaig <http://www.bamccaig.com/> <bamccaig [at] gmail.com>
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software <http://www.castopulence.org/> <bamccaig [at] castopulence=
..org>
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: shift oo
On Fri, Mar 18, 2011 at 5:34 PM, shawn wilson <ag4ve.us [at] gmail.com> wrote:
> sorry, i don't know how to do any better in gmail (and does it different on
> the gmail app on my android too - sorta messed up). should i color replies
> differently or something?
Quoting is typically done by prefixing each line of the quoted text
with "> ". Most mailers (including Gmail's Web interface) will do this
for you. Since you even suggested sending colored text I'm going to
assume that you are sending G-mail's default, which will probably end
up being whichever format is capable of sending your message (i.e.,
HTML, if necessary). Looking at the original raw message that I'm
responding to (and quoting) it appears that Gmail sent the E-mail as a
multipart/alternative, which is apparently used to send multiple
copies of the same E-mail in different formats. The only format that I
received appears to be text so I don't really understand why it would
do that... In any case, I can only assume that your quoting problems
are related to this. Either that, or you're screwing it up from your
phone. ;)
I personally use Gmail (most of the time) too, but to my knowledge my
E-mails are sent as I expected them to be (with the occasional
unintended wrapping of code if I forget to count my columns). You can
configure Gmail to send plain-text E-mails in its configuration
settings. It actually works out because as it turns out alternative
formats are evil. ;D
--
Brandon McCaig <http://www.bamccaig.com/> <bamccaig [at] gmail.com>
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software <http://www.castopulence.org/> <bamccaig [at] castopulence.org>
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: shift oo
On Tue, 22 Mar 2011 13:41:59 -0700, Randal L. Schwartz wrote:
>>>>>> "Peter" =3D=3D Peter Scott <Peter [at] PSDT.com> writes:
>
>>> my $s =3D Streamer->new;
>>> my $app =3D sub {
>>> return sub {
>>> $s->open_fh;
>>> my $writer =3D shift->(
>>> [ 200, [ "Content-type" =3D> "text/plain" ], $s ] );
>>> };
>>> };
>
> Peter> As it stands, this doesn't make sense because nothing happens to
> $writer; Peter> so why create it?
>
> I presume you're objecting to the explicit $writer. Certainly, the
> value of $writer is also the return value of the inner subroutine, so
> that *is* something that could be noted:
Right, the superfluous naming of $writer leads to a lot of head-
scratching. Best guess (if this is an accurate post of the code), that
there was originally some debugging/logging code in there before a
"return $writer" that got taken out. Although I supposed that the paste
was incomplete. Either way, a giant red herring.
--
Peter Scott
http://www.perlmedic.com/ http://www.perldebugged.com/
http://www.informit.com/store/product.aspx?isbn=3D0137001274
http://www.oreillyschool.com/courses/perl3/
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: shift oo
On 24/03/2011 02:39, Peter Scott wrote:
> On Tue, 22 Mar 2011 13:41:59 -0700, Randal L. Schwartz wrote:
>
>> "Peter" == Peter Scott<Peter [at] PSDT.com> writes:
>>>
>>> my $s = Streamer->new;
>>> my $app = sub {
>>> return sub {
>>> $s->open_fh;
>>> my $writer = shift->(
>>> [ 200, [ "Content-type" => "text/plain" ], $s ] );
>>> };
>>> };
>>
>> Peter> As it stands, this doesn't make sense because nothing happens to
>> $writer; Peter> so why create it?
>>
>> I presume you're objecting to the explicit $writer. Certainly, the
>> value of $writer is also the return value of the inner subroutine, so
>> that *is* something that could be noted:
>
> Right, the superfluous naming of $writer leads to a lot of head-
> scratching. Best guess (if this is an accurate post of the code), that
> there was originally some debugging/logging code in there before a
> "return $writer" that got taken out. Although I supposed that the paste
> was incomplete. Either way, a giant red herring.
For those that overlooked the post, the OP did attribute the source:
On 18/03/2011 21:08, shawn wilson wrote:
>
> this is where this comes from:
>
> http://www.samuelkaufman.com/blog/2011/03/16/streaming-with- plack-take-2/
Which is take 2 of a blog entry by Samuel Kaufman. The original is here:
<http://www.samuelkaufman.com/blog/2011/03/15/simple-streaming-test-server-with-plack/>
which is pretty poor code (although the author does say "The previous
code sample sucked for numerous reasons") not least because of the
wrapping and lack of indents. But it does explain the purpose of $writer.
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: shift oo
On Thu, Mar 24, 2011 at 4:42 PM, Rob Dixon <rob.dixon [at] gmx.com> wrote:
>
> On 24/03/2011 02:39, Peter Scott wrote:
>>
>> On Tue, 22 Mar 2011 13:41:59 -0700, Randal L. Schwartz wrote:
>>
>>> "Peter" =3D=3D Peter Scott<Peter [at] PSDT.com> =A0writes:
>>>>
>>>> my $s =3D Streamer->new;
>>>> my $app =3D sub {
>>>> return sub {
>>>> $s->open_fh;
>>>> my $writer =3D shift->(
>>>> [ 200, [ "Content-type" =3D> =A0"text/plain" ], $s ] );
>>>> };
>>>> };
>>>
>>> Peter> =A0As it stands, this doesn't make sense because nothing happens=
to
>>> $writer; Peter> =A0so why create it?
>>>
>>> I presume you're objecting to the explicit $writer. =A0Certainly, the
>>> value of $writer is also the return value of the inner subroutine, so
>>> that *is* something that could be noted:
>>
>> Right, the superfluous naming of $writer leads to a lot of head-
>> scratching. =A0Best guess (if this is an accurate post of the code), tha=
t
>> there was originally some debugging/logging code in there before a
>> "return $writer" that got taken out. =A0Although I supposed that the pas=
te
>> was incomplete. =A0Either way, a giant red herring.
>
> For those that overlooked the post, the OP did attribute the source:
>
> On 18/03/2011 21:08, shawn wilson wrote:
>>
>> =A0this is where this comes from:
>>
>> =A0http://www.samuelkaufman.com/blog/2011/03/16/streaming-wi th-plack-tak=
e-2/
>
> Which is take 2 of a blog entry by Samuel Kaufman. The original is here:
>
> <http://www.samuelkaufman.com/blog/2011/03/15/simple-streaming-test-serve=
r-with-plack/>
>
> which is pretty poor code (although the author does say "The previous
> code sample sucked for numerous reasons") not least because of the
> wrapping and lack of indents. But it does explain the purpose of $writer.
>
humm, i missed that. the rewrite looked much better (i couldn't hardly
stand to look at the original). i've actually learned a bunch of stuff
i should have learned - the difference between $_ and [at] _ (which just
seemed too obvious but maybe not :) ), closures (sorta), and and that
cool shift (which i don't think i'll ever use because what it does
isn't obvious).
for the record, i've pretty much figured out how to do what i want.
and *my* code is pretty simple. i create a data stream, keep it in a
Gearman worker, print out the uuid which js can use when posting data
in catalyst that catalyst can then use to call the worker stream and
push the data to the client. this was way more difficult than it
needed to be.
i went through a bunch of interesting things to figure out what i
needed to use (almost thought AnyEvent::Memcached or redis would work
- and it might have with some limitations) but gearman seems to be the
right man for the job :)
anyone who wants the code can just ask, but then again, it's pretty
straight forward once you know what to use (Catalyst, Plack::Builder,
Data::UUID, JSON, Gearman - that's about it). and the documentation on
all of these are pretty solid. And basic javascript. i might
eventually write something up on this (since it took me so damn long -
a bit over a week).
ps - brandon, it was gmail's 'rich formatting' that was messing things
up. it's good i've got that solved too, it seemed to be an irritation
for uri et al.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/