Re: [perl #127353] CGI documentation, start_html missing

2016-01-25 Thread frederik
Hi Kent,

Thanks for your time, seems everyone works late (or early) around
here.

> This is verging into "user support", which is typically better targeted at 
> the "beginners@perl.org"[1] mailing list or any of the Web::Simple support 
> avenues[2], not RT/P5P, however, I don't like to leave something hanging.

I don't know if just Cc'ing beginners@perl.org will work? (I'm not
subscribed)

> On Fri Jan 22 22:58:28 2016, frede...@ofb.net wrote:
> > Thanks for the reply.
> > 
> > I hope that a compatible version of CGI continues to be available from
> > the standard repositories,
> 
> As far as I'm aware, CGI.pm will continue to be maintained and its bugs fixed 
> where possible, and will be available from CPAN mirrors. ( And even in the 
> event it vanished from CPAN, there's always BackPAN :) )

Got it, thank you.

> > because after reading your explanation, and
> > the synopsis of CGI::HTML::Functions, I still don't understand what
> > the problem with the existing interface is, or how I'm supposed to get
> > start_html using HTML::Tiny... 
> 
> There's no need to use start_html with HTML::Tiny.
> 
>   my $content = start_html( child_nodes()  );
> 
> vs
>  
>   my $g = HTML::Tiny->new();
>   my $html = $g->html( 
>$g->... 
>   );
> 
> Are basically the difference you're looking for.
> 
> The difference that matters is instead of *printing* it to STDOUT with 
> Web::Simple, you pass the HTML string inside the last array ( this is how the 
> PSGI protocol works )
> 
> return [ CODE, [ HEADING => VALUE ], [ $html ] ];

That sounds like a big difference. I find it very intuitive to write
code that goes

print start_table(...)
for(...) {
print tr(td(...))
}
print end_table(...)

I don't see a way to do this with HTML::Tiny. Furthermore, all the
documentation for functions like start_table and start_html has been
not only removed from CGI but also fails to appear in
CGI::HTML::Functions. This just seems like a big regression to me,
documentation-wise.

> > or what is "template driven page
> > generation"... and I just noticed that Web::Simple pulled in a ton of
> > dependencies! Oh, the irony. :)
> 
> Its still simple to *use*, and it is optimised for low-overhead startup time 
> and fat-packability irrc :)
> 
> > Wait, templates are just what I feared. ... sigh...
> 
> Templates aren't all that bad. Sometimes its just nicer to write pure HTML 
> than fuss with writing HTML with hundreds of perl function calls =)

I totally agree with that statement, and I could see myself using
Web::Simple/PSGI for certain projects.

But I would also assert that sometimes it's nicer to write Perl
function calls, without having to worry about HTML entities and
matching all those start and end tags with the little angle brackets
around each one... so why should that choice be deprecated, even
"softly"?

> But I'll leave it up to you if you want to send any follow up questions to 
> other channels. :) 
> 
> HTH. 
> 
> 1: http://lists.perl.org/list/beginners.html , 
> http://www.nntp.perl.org/group/perl.beginners/
> 2: https://metacpan.org/pod/Web::Simple#COMMUNITY-AND-SUPPORT

I submitted an issue with the CGI module 

https://github.com/leejo/CGI.pm/issues/195

Sorry I missed that 'perlbug' is only for core modules.

Take care,

Frederick

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




Re: returning arrays

2016-01-25 Thread Paul Johnson
On Mon, Jan 25, 2016 at 12:24:04AM +0100, lee wrote:
> Paul Johnson  writes:
> 
> > On Sun, Jan 24, 2016 at 05:44:14PM +0200, Shlomi Fish wrote:
> >> Hi lee,
> >> 
> >> On Sun, 24 Jan 2016 13:11:37 +0100
> >> lee  wrote:
> >> 
> >> > Paul Johnson  writes:
> >> > >
> >> > > In scalar context the comma operator evaluates its left-hand side,
> >> > > throws it away and returns the right-hand side.  
> >> > 
> >> > What is the useful use for this operator?
> >> > 
> >> 
> >> Well, I believe its use was originally inherited from
> >> https://en.wikipedia.org/wiki/C_%28programming_language%29 where one can do
> >> something like:
> >> 
> >>x = (y++, y+2);
> >> 
> >> In Perl 5 though it is preferable to use do { ... } instead:
> >> 
> >>$x = do { $y++; $y+2; };
> >
> > In both Perl and C the comma operator is probably most usually 
> > (deliberately)
> > seen in for statements:
> >
> > #!/usr/bin/env perl
> >
> > use strict;
> > use warnings;
> >
> > for (my ($x, $y) = (1, 7); $x < 5; $x++, $y--) {
> > print "$x $y\n";
> > }
> >
> > and
> >
> > #include 
> >
> > int main() {
> > int x, y;
> > for (x = 1, y = 7; x < 5; x++, y--)
> > printf("%d %d\n", x, y);
> > return 0;
> > }
> >
> > both of which produce the output:
> >
> > 1 7
> > 2 6
> > 3 5
> > 4 4
> 
> Ok and how is the comma operator usefully useful?

Beyond what I have written above, I have had little use for it in C and
less in Perl.  But I have no doubt that were you to ask in the
appropriate forum then someone could provide a compelling argument.

>Obviously, I could
> use it to create convoluted code, which is usually not my intention.
> 
> Consider with these examples that an expression like (1, 3) might
> unexpectedly evaluate to 3, and you start to think that you don't like
> things like
> 
> 
> sub s {
> my $a = 1;
> my $b = 3;
> 
>return ($a, $b);
> }
> 
> 
> anymore because you could, by mistake (or intentionally), write
> 
> 
> my $x = s;
> 
> 
> .  So let me re-phrase my original question to: How do you /safely/
> return arrays?
> 
> That means I need an error message for '$x = s;' because I'd write that
> only by mistake.

You could add to your subroutine:

  die "Only call in list context" unless wantarray;

-- 
Paul Johnson - p...@pjcj.net
http://www.pjcj.net

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




Re: returning arrays

2016-01-25 Thread Nathan Hilterbrand



On 01/25/2016 05:13 PM, lee wrote:

Paul Johnson  writes:


On Mon, Jan 25, 2016 at 12:24:04AM +0100, lee wrote:

Paul Johnson  writes:

[...]

Consider with these examples that an expression like (1, 3) might
unexpectedly evaluate to 3, and you start to think that you don't like
things like


sub s {
 my $a = 1;
 my $b = 3;

return ($a, $b);
}


anymore because you could, by mistake (or intentionally), write


my $x = s;


.  So let me re-phrase my original question to: How do you /safely/
return arrays?

That means I need an error message for '$x = s;' because I'd write that
only by mistake.

You could add to your subroutine:

   die "Only call in list context" unless wantarray;

Oh, that's really cool :)


IMHO, this is pretty cool too:

return wantarray() ? ($a, $b) : [$a, $b];

In a list context, you get back a list..  otherwise you get back a 
reference to a list.  Might not be what you want, though.


Works with arrays, too..

my @anarray = ()
return wantarray() ? (@anarray) : [@anarray];

Nathan

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




Re: returning arrays

2016-01-25 Thread lee
Shlomi Fish  writes:

> Hi lee,
>
> I should note that it is my impression that you are far too needy in your
> enquiring, and I'm starting to lose patience.
>
> On Mon, 25 Jan 2016 00:11:43 +0100
> lee  wrote:
>
>> Shlomi Fish  writes:
>> 
>> >> >
>> >> > In scalar context the comma operator evaluates its left-hand side,
>> >> > throws it away and returns the right-hand side.
>> >> 
>> >> What is the useful use for this operator?
>> >>   
>> >
>> > Well, I believe its use was originally inherited from
>> > https://en.wikipedia.org/wiki/C_%28programming_language%29 where one can do
>> > something like:
>> >
>> >x = (y++, y+2);
>> >
>> > In Perl 5 though it is preferable to use do { ... } instead:
>> >
>> >$x = do { $y++; $y+2; };
>> >
>> > See http://perldoc.perl.org/functions/do.html . GCC and compatible 
>> > compilers
>> > have a similar feature to Perl 5's do {...} called statement expressions:
>> > https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html .  
>> 
>> How's that useful?  Isn't that equivalent to
>> 
>> $x = $y + 3;
>> 
>> ?
>
> Well, it also changes the value of $y. You can use more complicated examples,
> and this was just for the sake of demonstration.

$x = $y++ + 2;

All versions are convoluted code.

> One common pattern I used is to do:
>
> my %cache;
> sub f
> {
>
>   return $cache{$result} //= do {
>   # Calculate the cached result here.
>   };
> }

What does that do?

>> >> How do you convert an array into a list?
>> >>   
>> >
>> > You just put it in list context. For example (untested):
>> >
>> > sub f
>> > {
>> >print (@_);
>> > }
>> >
>> > my @input = (3, 44, 505, 6.6);
>> >
>> > f(@input);
>> >
>> > my @other_list = (5,@input,24);  
>> 
>> I'm not sure where the conversion of an array (non-static) into a list
>> (static) would take place in this example.
>> 
>
> (5,@input,24) becomes (5,3,44,505,6.6,24).

splice (@other_list, 3, 1, 4);

Are you saying that would give you an error in this case for you created
a (partly) static array?

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




Re: returning arrays

2016-01-25 Thread lee
Paul Johnson  writes:

> On Mon, Jan 25, 2016 at 12:24:04AM +0100, lee wrote:
>> Paul Johnson  writes:

> [...]
>> Consider with these examples that an expression like (1, 3) might
>> unexpectedly evaluate to 3, and you start to think that you don't like
>> things like
>> 
>> 
>> sub s {
>> my $a = 1;
>> my $b = 3;
>> 
>>return ($a, $b);
>> }
>> 
>> 
>> anymore because you could, by mistake (or intentionally), write
>> 
>> 
>> my $x = s;
>> 
>> 
>> .  So let me re-phrase my original question to: How do you /safely/
>> return arrays?
>> 
>> That means I need an error message for '$x = s;' because I'd write that
>> only by mistake.
>
> You could add to your subroutine:
>
>   die "Only call in list context" unless wantarray;

Oh, that's really cool :)

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




Re: returning arrays

2016-01-25 Thread Kent Fredric
On 26 January 2016 at 12:02, Nathan Hilterbrand  wrote:
> return wantarray() ? ($a, $b) : [$a, $b];
>
> In a list context, you get back a list..  otherwise you get back a reference
> to a list.  Might not be what you want, though.
>
> Works with arrays, too..
>
> my @anarray = ()
> return wantarray() ? (@anarray) : [@anarray];


I've found pretty much everywhere you'd want that its simpler to just
return either [@anarray] or just \@anarray

Returning lists is cute and all, and returning lists only when in list
context is cute ...


But in practice I find it mostly painful, because you find yourself
needing to "trick" perl into thinking its in list context from time to
time.

And even occasional use of

return (()= function() )

Or whatever the right magic is to force the called function into list
context is just more pain than its worth.

It just seems more practical to always return scalar return values of
some description.

Now, if you want a more useful application of the comma operator, consider:

while ( condition ) {
$x = function(), next if $second_condition
}

Which is equivalent to

while ( condition ) {
$second_condition and $x = function(), next;
}

Which is similar to

while( condition ) {
if ( $second_condition ) {
$x = function();
next;
}
}

But allows a more compact representation ( which matters if you have a
dozen or so such conditions ),
and avoids adding a lexical scope. ( Fun fact, replace  "$x =
function(), next;" with "do { $x = function(); next }" and watch
deparse turn it back into an "if" statement :) )

But you can't use "&&" or "and" in the expression, because
"function()" could return a false value, which would prevent the
'next' occurring.


-- 
Kent

KENTNL - https://metacpan.org/author/KENTNL

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




Re: Regex to match "bad" characters in a parameter

2016-01-25 Thread Shawn H Corey
On Mon, 25 Jan 2016 16:16:40 -0800
SSC_perl  wrote:

>   I'm trying to find a way to trap bad item numbers.  I want to
> parse the parameter "itemid=" and then everything up to either an "&"
> or end-of-string.  A good item number will contain only ASCII
> letters, numbers, dashes, and underscores and may terminate with a
> "&" or it may not (see samples below).   The following string should
> test negative in the regex below:
> 
> my $QUERY_STRING = 'itemid=AT18C_AT18C=1';
> 
> but a string containing "itemid=AT18/C" should test positive, since
> it has a slash.
> 
>   I can catch a single bad character and get it to work, e.g.
> 
> if ( $QUERY_STRING =~ m| itemid= .*? [/]+? .*? &? |x ) {
> 
> but I'd like to do something like this instead to catch others:
> 
> if ( $QUERY_STRING =~ m| itemid= (?: .*? [^a-zA-Z0_-]+ .*? ) &? |x )
> { ...
> 
>   Unfortunately, I can't get it to work.  I've read perlretut,
> but can't see the answer.  What am I doing wrong?
> 
> Thanks,
> Frank
> 
> Here are a couple of test strings:
> 
> 'itemid=AT18C_AT18C=1=main.htm=1=1=detail.htm=asc'
> 
> 'c=detail.htm=AT18C'
> 
> 
> 
> 

Use the negative match operator !~

  if( $QUERY_STRING !~ m{ itemid = [-0-9A-Za-z_]+? (?: \& | \z ) }msx ){
print "bad: $QUERY_STRING\n";
  }


-- 
Don't stop where the ink does.
Shawn

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




Regex to match "bad" characters in a parameter

2016-01-25 Thread SSC_perl
I'm trying to find a way to trap bad item numbers.  I want to parse the 
parameter "itemid=" and then everything up to either an "&" or end-of-string.  
A good item number will contain only ASCII letters, numbers, dashes, and 
underscores and may terminate with a "&" or it may not (see samples below).   
The following string should test negative in the regex below:

my $QUERY_STRING = 'itemid=AT18C_AT18C=1';

but a string containing "itemid=AT18/C" should test positive, since it has a 
slash.

I can catch a single bad character and get it to work, e.g.

if ( $QUERY_STRING =~ m| itemid= .*? [/]+? .*? &? |x ) {

but I'd like to do something like this instead to catch others:

if ( $QUERY_STRING =~ m| itemid= (?: .*? [^a-zA-Z0_-]+ .*? ) &? |x ) { ...

Unfortunately, I can't get it to work.  I've read perlretut, but can't 
see the answer.  What am I doing wrong?

Thanks,
Frank

Here are a couple of test strings:

'itemid=AT18C_AT18C=1=main.htm=1=1=detail.htm=asc'

'c=detail.htm=AT18C'




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




Re: returning arrays

2016-01-25 Thread Shlomi Fish
Hi lee,

I should note that it is my impression that you are far too needy in your
enquiring, and I'm starting to lose patience.

On Mon, 25 Jan 2016 00:11:43 +0100
lee  wrote:

> Shlomi Fish  writes:
> 
> >> >
> >> > In scalar context the comma operator evaluates its left-hand side,
> >> > throws it away and returns the right-hand side.
> >> 
> >> What is the useful use for this operator?
> >>   
> >
> > Well, I believe its use was originally inherited from
> > https://en.wikipedia.org/wiki/C_%28programming_language%29 where one can do
> > something like:
> >
> > x = (y++, y+2);
> >
> > In Perl 5 though it is preferable to use do { ... } instead:
> >
> > $x = do { $y++; $y+2; };
> >
> > See http://perldoc.perl.org/functions/do.html . GCC and compatible compilers
> > have a similar feature to Perl 5's do {...} called statement expressions:
> > https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html .  
> 
> How's that useful?  Isn't that equivalent to
> 
> $x = $y + 3;
> 
> ?

Well, it also changes the value of $y. You can use more complicated examples,
and this was just for the sake of demonstration.

One common pattern I used is to do:

my %cache;
sub f
{

return $cache{$result} //= do {
# Calculate the cached result here.
};
}

> 
> I'm surprised that this apparently is supposed to evaluate to something,
> though.  I wouldn't expect that from a control structure.
> 
> >> > This means that the value of (1, 2, 3) in scalar context is 3, and this
> >> > is what gets assigned to $list.
> >> >
> >> > What is not happening at all is the creation of a list of numbers and a
> >> > calculation of its length.
> >> >
> >> > See also perldoc -q 'difference between a list and an array'
> >> 
> >> How do you convert an array into a list?
> >>   
> >
> > You just put it in list context. For example (untested):
> >
> > sub f
> > {
> > print (@_);
> > }
> >
> > my @input = (3, 44, 505, 6.6);
> >
> > f(@input);
> >
> > my @other_list = (5,@input,24);  
> 
> I'm not sure where the conversion of an array (non-static) into a list
> (static) would take place in this example.
> 

(5,@input,24) becomes (5,3,44,505,6.6,24).

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
List of Text Processing Tools - http://shlom.in/text-proc

Inigo Montoya: You seem a decent fellow. I hate to kill you.
The Man in Black: You seem a decent fellow. I hate to die.
— http://en.wikiquote.org/wiki/The_Princess_Bride_%28film%29

Please reply to list if it's a mailing list post - http://shlom.in/reply .

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