Re: Cool/useful short examples of Perl?

2011-06-09 Thread Graeme Hewson
On Thursday 09 June 2011 15:49:22 Joel Bernstein wrote:

> Sometimes you really do need complex conditions like this.
> I favour at minimum naming and commenting them properly, something like:
>
> sub should_permit_frobnication {
>   my $self = shift;
>   return 1 if ; # we permit this case
>   return 1 if ... and not ; # special case
>   return 0; # not permitted by default
> }
>
> (later)
> unless ($self->should_permit_frobnication) {
>  ... # the condition above should make sense in the semantic of the domain
> }

With more than two or three decisions (theoretically four or eight outcomes) I 
would write a decision table into comments around the code. This allows you 
to reason about (and show you've reasoned about) the "impossible" conditions. 
So it turns into a ternary decision: true, false or die.


Re: Cool/useful short examples of Perl?

2011-06-09 Thread Chris Jack

Christopher Jones c.jo...@ucl.ac.uk wrote

> Clearly you weren't brought up in the Midlands.. Or did you mean "use 
> them knowingly"?

You're saying you weren't not brought in the Midlands then? 
  


Re: Cool/useful short examples of Perl?

2011-06-09 Thread Abigail
On Thu, Jun 09, 2011 at 03:39:45PM +0100, David Matthewman wrote:
> 
> On 9 Jun 2011, at 15:16, David Landgren wrote:
> 
> > I had all the parts lying around, but didn't realise they were germane to 
> > the problem. So, thanks for the cogent explanation.
> 
> You're welcome. ;-)
> 
> This effect means that:
> 
>   if (!something)
> 
> and
> 
>   unless(something)
> 
> don't do the same thing either, whereas from the 'overloaded operator'
  point-of-view *as far as I can tell* they do the same thing. The 'unless'
  statement runs the overloaded '!' subroutine of the class in question,
  as opposed to doing a 'bool' conversion on the class and doing a logical
  NOT on the result of that, which is the other thing it *might* be expected
  to do.


Yeah. I though 'if (!something)' to call a '!' overload method, and
'unless (something)' to call a 'bool' or '""' overload method. They don't,
in either case a '!' overload method is called. In fact, it seems they
even compile down to the same code:

$ perl -MO=Concise -we 'unless ($x) {1}'
Name "main::x" used only once: possible typo at -e line 1.
5  <@> leave[1 ref] vKP/REFC ->(end)
1 <0> enter ->2
2 <;> nextstate(main 3 -e:1) v:{ ->3
- <1> null vK/1 ->5
4<|> or(other->-) vK/1 ->5
-   <1> ex-not sK*/1 ->4
-  <1> ex-rv2sv sK/1 ->-
3 <$> gvsv(*x) s ->4
-   <@> scope vK ->5
-  <0> ex-nextstate v ->-
-  <0> ex-const v ->-
-e syntax OK

$ perl -MO=Concise -we 'if (!$x) {1}'
Name "main::x" used only once: possible typo at -e line 1.
5  <@> leave[1 ref] vKP/REFC ->(end)
1 <0> enter ->2
2 <;> nextstate(main 3 -e:1) v:{ ->3
- <1> null vK/1 ->5
4<|> or(other->-) vK/1 ->5
-   <1> ex-not sK/1 ->4
-  <1> ex-rv2sv sK/1 ->-
3 <$> gvsv(*x) s ->4
-   <@> scope vK ->5
-  <0> ex-nextstate v ->-
-  <0> ex-const v ->-
-e syntax OK



Abigail


Re: Cool/useful short examples of Perl?

2011-06-09 Thread Joel Bernstein
On 9 June 2011 16:38, Colin Campbell  wrote:
> On 09/06/11 11:52, David Cantrell wrote:
>> On Wed, Jun 08, 2011 at 07:46:52AM +0200, Richard Foley wrote:
>
>>> Even worse is:
>>>      unless ( !something ) { ...
>>>
>>> The brain just into tailspin goes.
>>
>> It's not just Germans' brains that melt.  Double negatives are just
>> confusing.  Normal people hardly ever use them.
>>
> Judging from some of the code I've seen some folks (possibly with a bit
> less programming experience) think its great to pile on the
> unless ( !$something && !$something_else || $yet_something_else )

Sometimes you really do need complex conditions like this.
I favour at minimum naming and commenting them properly, something like:

sub should_permit_frobnication {
  my $self = shift;
  return 1 if ; # we permit this case
  return 1 if ... and not ; # special case
  return 0; # not permitted by default
}

(later)
unless ($self->should_permit_frobnication) {
 ... # the condition above should make sense in the semantic of the domain
}

> Simplicity is hard to learn...
...from other people's mistakes...

/joel



Re: Cool/useful short examples of Perl?

2011-06-09 Thread David Matthewman

On 9 Jun 2011, at 15:16, David Landgren wrote:

> I had all the parts lying around, but didn't realise they were germane to the 
> problem. So, thanks for the cogent explanation.

You're welcome. ;-)

This effect means that:

  if (!something)

and

  unless(something)

don't do the same thing either, whereas from the 'overloaded operator' 
point-of-view *as far as I can tell* they do the same thing. The 'unless' 
statement runs the overloaded '!' subroutine of the class in question, as 
opposed to doing a 'bool' conversion on the class and doing a logical NOT on 
the result of that, which is the other thing it *might* be expected to do.

I just wrote a class to check this. It claims that everything is true 
(especially the lies), and is called 'Garak', ;-)

-- 
David Matthewman


Re: Cool/useful short examples of Perl?

2011-06-09 Thread Colin Campbell
On 09/06/11 11:52, David Cantrell wrote:
> On Wed, Jun 08, 2011 at 07:46:52AM +0200, Richard Foley wrote:

>> Even worse is:
>>  unless ( !something ) { ...
>>
>> The brain just into tailspin goes.
> 
> It's not just Germans' brains that melt.  Double negatives are just
> confusing.  Normal people hardly ever use them.
> 
Judging from some of the code I've seen some folks (possibly with a bit
less programming experience) think its great to pile on the
unless ( !$something && !$something_else || $yet_something_else )

Simplicity is hard to learn...


-- 
Colin Campbell
Chief Software Engineer,
PTFS Europe Limited
Content Management and Library Solutions
+44 (0) 845 557 5634 (phone)
+44 (0) 7759 633626  (mobile)
colin.campb...@ptfs-europe.com
skype: colin_campbell2

http://www.ptfs-europe.com


Re: Cool/useful short examples of Perl?

2011-06-09 Thread Gabor Szabo
On Thu, Jun 9, 2011 at 3:39 PM, Imre Saling  wrote:
> Quoting "Ruud H.G. van Tol" :
>>
>>    # see also perlvar: $BASETIME
>>    { my $t0 = time; sub elapsed { time - $t0 } }
>>
>> --
>> Ruud
>
>
> or to get the elapsed time since the last call to elapsed():
>
>  my $t0 = time;
>  sub elapsed {
>    my $t = time;
>    my $dt = $t - $t0;
>    $t0 = $t;
>    return $dt;
>  }
>


my bikeshed has nicer color:


use 5.010;
use Time::HiRes qw(time);

say elapsed();
say elapsed();

sub elapsed {
   state $t0 = $^T;

   my $t = time;
   my $dt = $t-$t0;
   $t0 = $t;
   return $dt;
}


Gabor



Re: Cool/useful short examples of Perl?

2011-06-09 Thread David Landgren

On 09/06/2011 15:22, David Matthewman wrote:


On 9 Jun 2011, at 12:10, David Landgren wrote:


On 08/06/2011 15:00, David Matthewman wrote:


On 8 Jun 2011, at 13:17, Tom Hukins wrote:


On Wed, Jun 08, 2011 at 02:00:41PM +0200, Abigail wrote:

I'd rather go for sacking people that don't know the difference
between

if (something) { ... }

and

unless (!something) { ... }


[...]


And now I'm wondering whether all this is either blindingly obvious to everyone 
else on the list and you can't believe I'm explaining something so basic, or 
abstruse in the extreme and you can't believe I'm noodling about such an 
unlikely edge case. Or both. ;-)


I had all the parts lying around, but didn't realise they were germane 
to the problem. So, thanks for the cogent explanation.


David
--
There's bum trash in my hall and my place is ripped
I've totalled another amp, I'm calling in sick


Re: Cool/useful short examples of Perl?

2011-06-09 Thread Bill Crawford
On 9 June 2011 13:39, Imre Saling  wrote:

> or to get the elapsed time since the last call to elapsed():
>
>  my $t0 = time;
>  sub elapsed {
...
>  }

sub start {
return $last = time;
}

sub lap {
my $t = time;
push (@laps, $t - $last);
$last = $t;
return $laps[-1];
}

sub reset {
(@laps) = ( );
}



Re: Cool/useful short examples of Perl?

2011-06-09 Thread Dave Cross

On 06/09/2011 01:39 PM, Imre Saling wrote:

Quoting "Ruud H.G. van Tol" :


# see also perlvar: $BASETIME
{ my $t0 = time; sub elapsed { time - $t0 } }


or to get the elapsed time since the last call to elapsed():

my $t0 = time;
sub elapsed {
  my $t = time;
  my $dt = $t - $t0;
  $t0 = $t;
  return $dt;
}


If we're talking about modern Perl, then that looks like a perfect use 
for a state variable.


Dave...


Re: Cool/useful short examples of Perl?

2011-06-09 Thread David Matthewman

On 9 Jun 2011, at 12:10, David Landgren wrote:

> On 08/06/2011 15:00, David Matthewman wrote:
>> 
>> On 8 Jun 2011, at 13:17, Tom Hukins wrote:
>> 
>>> On Wed, Jun 08, 2011 at 02:00:41PM +0200, Abigail wrote:
 I'd rather go for sacking people that don't know the difference
 between
 
if (something) { ... }
 
 and
 
unless (!something) { ... }
>>> 
> 
> [...]
> 
>> Their side effects are different. There may be a simpler way to demonstrate 
>> this, but for instance:
> 
> I still don't get it. Abigail wasn't talking about !!. Your code seemed to be 
> more to the point, and so I fleshed it out. Consider:

OK. Backing up a little, this is really an exercise in 'always explicitly 
return from a subroutine if you're *ever* going to care what the return value 
is.'

If you don't explicitly return from a subroutine in Perl, it returns the value 
of the last statement executed. Now:

if ($value) ...

and:

unless (!$value) ...

are not executing the same statement, and a subroutine returning immediately 
after each test will consequently not return the same thing.

Bearing that in mind:

#! /usr/bin/env perl

print "unless ( !1 ): ";
print use_unless(1) . "\n";
print "if (  1 ): ";
print use_if(1) . "\n";
print "unless ( !0 ): ";
print use_unless(0) . "\n";
print "if (  0 ): ";
print use_if(0) . "\n";

sub use_unless {
my $value = shift;
my $state = 'False!';
unless ( !$value ) {
$state = 'True!';
}
}

sub use_if {
my $value = shift;
my $state = 'False!';
if ( $value ) {
$state = 'True!';
}
}

 End of code 

This prints out:

unless ( !1 ): True!
if (  1 ): True!
unless ( !0 ): 1
if (  0 ): 0

When the subroutines are called with a true value, the 'if' or 'unless' test 
succeeds, the inner block is run, and the subroutine returns 'True!' because 
that was the result of the last statement it executed. If called with a false 
value, the test fails, the inner block isn't run, and the subroutine returns 
the value of the test expression, which is different in the two cases. (The 
case where the subroutines are called with false values are the interesting 
ones from the point of view of the discussion, but I included the ones where 
they were called with true values in the hope that it would show what was going 
on better.)

And now I'm wondering whether all this is either blindingly obvious to everyone 
else on the list and you can't believe I'm explaining something so basic, or 
abstruse in the extreme and you can't believe I'm noodling about such an 
unlikely edge case. Or both. ;-)

-- 
David Matthewman


Re: Cool/useful short examples of Perl?

2011-06-09 Thread Imre Saling

Quoting "Ruud H.G. van Tol" :


# see also perlvar: $BASETIME
{ my $t0 = time; sub elapsed { time - $t0 } }

--
Ruud



or to get the elapsed time since the last call to elapsed():

  my $t0 = time;
  sub elapsed {
my $t = time;
my $dt = $t - $t0;
$t0 = $t;
return $dt;
  }

-pelagic



Re: Cool/useful short examples of Perl?

2011-06-09 Thread David Landgren

On 08/06/2011 15:00, David Matthewman wrote:


On 8 Jun 2011, at 13:17, Tom Hukins wrote:


On Wed, Jun 08, 2011 at 02:00:41PM +0200, Abigail wrote:

I'd rather go for sacking people that don't know the difference
between

if (something) { ... }

and

unless (!something) { ... }




[...]


Their side effects are different. There may be a simpler way to demonstrate 
this, but for instance:


I still don't get it. Abigail wasn't talking about !!. Your code seemed 
to be more to the point, and so I fleshed it out. Consider:


#! /usr/bin/env perl -l

foreach my $var (-1, 0, '0 but true', 1, 2, "0", '', ' ', undef) {
print "\n", defined $var ? $var : 'undef';
is_if($var);
not_unless($var);
not_if($var);
is_unless($var);
}

sub is_unless {
  unless ($_[0]) {
print +(caller 0)[3];
  }
}

sub is_if {
  if ($_[0]) {
print +(caller 0)[3];
  }
}

sub not_unless {
  unless (!$_[0]) {
print +(caller 0)[3];
  }
}

sub not_if {
  if (!$_[0]) {
print +(caller 0)[3];
  }
}

__END__

This produces:

-1
main::is_if
main::not_unless

0
main::not_if
main::is_unless

0 but true
main::is_if
main::not_unless

1
main::is_if
main::not_unless

2
main::is_if
main::not_unless

0
main::not_if
main::is_unless


main::not_if
main::is_unless


main::is_if
main::not_unless

undef
main::not_if
main::is_unless

Which leads me to conclude that you enter a block whether you write if 
($foo) or unless (!$foo) (which I was glad to observe, otherwise my sky 
would be falling down). So I can't see the side effects to which Abigail 
alludes.


David

--
There's bum trash in my hall and my place is ripped
I've totalled another amp, I'm calling in sick


Re: Cool/useful short examples of Perl?

2011-06-09 Thread Christopher Jones
On 9 Jun 2011, at 11:52, David Cantrell wrote:

> Double negatives are just confusing.  Normal people hardly ever use them.

Clearly you weren't brought up in the Midlands.. Or did you mean "use them 
knowingly"?


Chris





Re: Cool/useful short examples of Perl?

2011-06-09 Thread David Cantrell
On Wed, Jun 08, 2011 at 02:00:41PM +0200, Abigail wrote:

> I'd rather go for sacking people that don't know the difference 
> between 
> if (something) { ... }
> 
> and
> unless (!something) { ... }
> 
> Or does everyone think they are always equivalent?

If they're not equivalent, someone's playing silly buggers, and should
be sacked.

Things that immediately spring to mind are overloading of ! and where
something is ++something_else, but that's an incredibly stupid thing to
do and should result in fingernails being pulled out and *then* sacking.

-- 
David Cantrell | Official London Perl Mongers Bad Influence

People from my sort of background needed grammar schools to
compete with children from priveleged homes like ... Tony Benn
 -- Margaret Thatcher


Re: Cool/useful short examples of Perl?

2011-06-09 Thread David Cantrell
On Wed, Jun 08, 2011 at 07:46:52AM +0200, Richard Foley wrote:
> I've found a lot of German programmers very uncomfortable with the "unless" 
> keyword, (or maybe it's just c programmers).  They appear to very often prefer
> to use a construct of the form:
>   if ( !something ) { ...
> 
> Even worse is:
>   unless ( !something ) { ...
> 
> The brain just into tailspin goes.

It's not just Germans' brains that melt.  Double negatives are just
confusing.  Normal people hardly ever use them.

-- 
David Cantrell | top google result for "topless karaoke murders"

  Languages for which ISO-Latin-$n is not necessary, #1 in a series:

Latin


Re: Cool/useful short examples of Perl?

2011-06-09 Thread David Cantrell
On Wed, Jun 08, 2011 at 07:18:33AM +0100, Sue Spence wrote:

> This distaste is not really limited to Germans or C programmers in my
> experience. Anything more complicated than
> 
> holiday.picnic() unless raining;
> 
> will ideally not appear in any code that I am required to
> inspect/support/maintain.

Does your Holiday class support methods for all the possible things you
might do, or does it use AUTOLOAD for most of its methods?

-- 
David Cantrell | Bourgeois reactionary pig

"There's a hole in my bucket, dear Liza, dear Liza."
"WHAT MAKES YOU SAY THERE IS A HOLE IN YOUR BUCKET?"


Re: Cool/useful short examples of Perl?

2011-06-09 Thread Nicholas Clark
On Thu, Jun 09, 2011 at 10:26:22AM +0100, Matt Lawrence wrote:
> On 08/06/11 18:19, Philip Newton wrote:
> >On Wed, Jun 8, 2011 at 15:37, Matt Lawrence  
> >wrote:
> >>Perl's canonical true and false are 1 and '' respectively
> >Is that so? How would one find that out?
> >
> >Dump-ing 4==4 and 4==5 with Devel::Peek implies to me that true and
> >false are PVNVs with integer, floating-point, and string values filled
> >simultaneously, so I'm not sure how any of the three fields could be
> >considered "the" value of those, er, values(?).
> 
> Isn't that true of all scalars, under the hood? All I meant was:
> 
> $ perl -le 'print "\x27$_\x27" for 1 == 1, 1 == 0'
> '1'
> ''

"truth" has a value, and it's consistent with any other "1" that's been used
in an integer, floating point and string context:

$ perl -MDevel::Peek -e '$a = 1; sprintf "$a%g", $a; Dump $a; Dump 1 == 1'
SV = PVNV(0x1008031a0) at 0x100811948
  REFCNT = 1
  FLAGS = (IOK,NOK,POK,pIOK,pNOK,pPOK)
  IV = 1
  NV = 1
  PV = 0x100201790 "1"\0
  CUR = 1
  LEN = 16
SV = PVNV(0x100802c20) at 0x100800828
  REFCNT = 2147483643
  FLAGS = (IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
  IV = 1
  NV = 1
  PV = 0x100200090 "1"\0
  CUR = 1
  LEN = 16

It's falsehood which is special:

$ perl -MDevel::Peek -MScalar::Util=dualvar -e '$a = dualvar(0, ""); sprintf 
"%g", $a; Dump $a; Dump 0 == 1'
SV = PVNV(0x100803a40) at 0x100815418
  REFCNT = 1
  FLAGS = (IOK,NOK,POK,pIOK,pNOK,pPOK)
  IV = 0
  NV = 0
  PV = 0x1002017c0 ""\0
  CUR = 0
  LEN = 16
SV = PVNV(0x100802c00) at 0x100800810
  REFCNT = 2147483647
  FLAGS = (IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
  IV = 0
  NV = 0
  PV = 0x100200080 ""\0
  CUR = 0
  LEN = 16


It's effectively a dualvar with a string value of an empty string, and a
numeric 0.

(Yes, an empty string has the value of 0 when used in a numeric context, but
it warns about the conversion. The dualvar doesn't)

[whilst beer is foamy, and none of the above]

Nicholas Clark


Re: Cool/useful short examples of Perl?

2011-06-09 Thread Matt Lawrence

On 08/06/11 18:19, Philip Newton wrote:

On Wed, Jun 8, 2011 at 15:37, Matt Lawrence  wrote:

Perl's canonical true and false are 1 and '' respectively

Is that so? How would one find that out?

Dump-ing 4==4 and 4==5 with Devel::Peek implies to me that true and
false are PVNVs with integer, floating-point, and string values filled
simultaneously, so I'm not sure how any of the three fields could be
considered "the" value of those, er, values(?).


Isn't that true of all scalars, under the hood? All I meant was:

$ perl -le 'print "\x27$_\x27" for 1 == 1, 1 == 0'
'1'
''

Matt


Re: Cool/useful short examples of Perl?

2011-06-08 Thread Ruud H.G. van Tol


On 30 May 2011 11:40, Leo Lapworth  wrote:


I'm working on http://learn.perl.org/ and I'd like to have a few rotating
example of what can be done with Perl on the home page.



# see also perlvar: $BASETIME
{ my $t0 = time; sub elapsed { time - $t0 } }

--
Ruud


Re: Cool/useful short examples of Perl?

2011-06-08 Thread David Matthewman

On 8 Jun 2011, at 13:17, Tom Hukins wrote:

> On Wed, Jun 08, 2011 at 02:00:41PM +0200, Abigail wrote:
>> I'd rather go for sacking people that don't know the difference 
>> between 
>> 
>>if (something) { ... }
>> 
>> and
>> 
>>unless (!something) { ... }
> 
> It's sunny outside and pubs are open:  I can think of worse times to
> lose my job.
> 
>> Or does everyone think they are always equivalent?
> 
> I'm not everyone, and with a language as flexible as Perl I hesitate
> to make strong statements involving words like "always", but I don't
> recall encountering a situation where they differ.

If 'something' is 'something && something_else' then you *really* want to put 
brackets around it before adding a '!' at the start.

But that's so trivial it barely needs saying, yes?

-- 
David Matthewman




Re: Cool/useful short examples of Perl?

2011-06-08 Thread Philip Newton
On Wed, Jun 8, 2011 at 15:37, Matt Lawrence  wrote:
> Perl's canonical true and false are 1 and '' respectively

Is that so? How would one find that out?

Dump-ing 4==4 and 4==5 with Devel::Peek implies to me that true and
false are PVNVs with integer, floating-point, and string values filled
simultaneously, so I'm not sure how any of the three fields could be
considered "the" value of those, er, values(?).

Cheers,
Philip
-- 
Philip Newton 


RE: Cool/useful short examples of Perl?

2011-06-08 Thread Chris Jack

Apologies for including the top half of the digest.
 
Chris 

Re: Cool/useful short examples of Perl?

2011-06-08 Thread Matt Lawrence

On 08/06/11 13:41, Paul Makepeace wrote:

On Wed, Jun 8, 2011 at 13:17, Tom Hukins  wrote:

On Wed, Jun 08, 2011 at 02:00:41PM +0200, Abigail wrote:

I'd rather go for sacking people that don't know the difference
between

 if (something) { ... }

and

 unless (!something) { ... }

It's sunny outside and pubs are open:  I can think of worse times to
lose my job.


Or does everyone think they are always equivalent?

I'm not everyone, and with a language as flexible as Perl I hesitate
to make strong statements involving words like "always", but I don't
recall encountering a situation where they differ.

$ perl -le 'print "$_ == !!$_ ? ", $_ == !!$_ ? "yes" : "no" for (-1,
0, 1, 2, undef)'
-1 == !!-1 ? no
0 == !!0 ? yes
1 == !!1 ? yes
2 == !!2 ? no
  == !! ? yes



But:

$ perl -le 'print 0 eq !!0 ? "yes" : "no"'
no

This still wouldn't affect the behaviour of the original example. In the 
context of an if or unless statement, the only thing that matters is the 
boolean value of the expression, not exactly which true or false value 
it is. Perl's canonical true and false are 1 and '' respectively, but 
everything has a boolean value.


Matt



Re: Cool/useful short examples of Perl?

2011-06-08 Thread Paul Makepeace
On Wed, Jun 8, 2011 at 14:15, Kaoru  wrote:
> On Wed, Jun 8, 2011 at 1:41 PM, Paul Makepeace  wrote:
>> $ perl -le 'print "$_ == !!$_ ? ", $_ == !!$_ ? "yes" : "no" for (-1,
>> 0, 1, 2, undef)'
>> -1 == !!-1 ? no
>> 0 == !!0 ? yes
>> 1 == !!1 ? yes
>> 2 == !!2 ? no
>>  == !! ? yes
>
> Whether they are equal according to "==" shouldn't matter here, should
> it? What matters between if(something) and unless (!something) is
> whether something and !!something are the same boolean-wise.
>
> I think this code shows they are all the same?

Yes, you're right - perl doesn't use == semantics for that.


$ perl -e 'for ("a", "", -1, 0, 1, 2, undef) {
print "$_: ";
if ($_) {
  unless (!$_) { print "ok" } else { print "not ok" }
} else {
  unless (!$_) { print "not ok" } else { print "ok" }
}
print "\n" }'
a: ok
: ok
-1: ok
0: ok
1: ok
2: ok
: ok

A legitimate use of unless + else! :-)

P



Re: Cool/useful short examples of Perl?

2011-06-08 Thread David Matthewman

On 8 Jun 2011, at 13:41, Paul Makepeace wrote:

> On Wed, Jun 8, 2011 at 13:17, Tom Hukins  wrote:
>> On Wed, Jun 08, 2011 at 02:00:41PM +0200, Abigail wrote:
>>> I'd rather go for sacking people that don't know the difference
>>> between
>>> 
>>> if (something) { ... }
>>> 
>>> and
>>> 
>>> unless (!something) { ... }
>> 
>> It's sunny outside and pubs are open:  I can think of worse times to
>> lose my job.
>> 
>>> Or does everyone think they are always equivalent?
>> 
>> I'm not everyone, and with a language as flexible as Perl I hesitate
>> to make strong statements involving words like "always", but I don't
>> recall encountering a situation where they differ.
> 
> $ perl -le 'print "$_ == !!$_ ? ", $_ == !!$_ ? "yes" : "no" for (-1,
> 0, 1, 2, undef)'
> -1 == !!-1 ? no
> 0 == !!0 ? yes
> 1 == !!1 ? yes
> 2 == !!2 ? no
> == !! ? yes

Well, -1 may not equal !!-1, but truthwise it's the same, so it's the same to 
an if or unless statement.

I realise this is getting a bit silly, but:


foreach my $test_item (-1, 0, 1, 2, undef, '') {
print "$test_item:\t" 
. return_truth_by_if_not($test_item) . ', '
. return_truth_by_unless($test_item) . ', '
. return_truth_by_if_not(!$test_item) . ', '
. return_truth_by_unless(!$test_item) . ', '
. return_truth_by_if_not(!!$test_item) . ', '
. return_truth_by_unless(!!$test_item) . "\n";
}

sub return_truth_by_if_not {
  my $value = shift;
  if (!$value) {
  return 'non';
  }
  else {
  return 'oui';
  }
}

sub return_truth_by_unless {
  my $value = shift;
  unless ($value) {
  return 'non';
  }
  else {
  return 'oui';
  }
}

-- 
David Matthewman




Re: Cool/useful short examples of Perl?

2011-06-08 Thread Chris Jack




> From: london.pm-requ...@london.pm.org
> Subject: london.pm Digest, Vol 68, Issue 13
> To: london.pm@london.pm.org
> Date: Wed, 8 Jun 2011 14:00:37 +0100
> 
> Send london.pm mailing list submissions to
> london.pm@london.pm.org
> 
> To subscribe or unsubscribe via the World Wide Web, visit
> http://london.pm.org/mailman/listinfo/london.pm
> or, via email, send a message with subject or body 'help' to
> london.pm-requ...@london.pm.org
> 
> You can reach the person managing the list at
> london.pm-ow...@london.pm.org
> 
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of london.pm digest..."
> 
> 
> Today's Topics:
> 
> 1. Re: Someone needs to take jwz aside... (Paul Makepeace)
> 2. Re: Someone needs to take jwz aside... (Denny)
> 3. Re: Someone needs to take jwz aside... (Peter Edwards)
> 4. Re: Cool/useful short examples of Perl? (Abigail)
> 5. Re: Cool/useful short examples of Perl? (Bill Crawford)
> 6. Re: Cool/useful short examples of Perl? (Tom Hukins)
> 7. Re: Cool/useful short examples of Perl? (Paul Makepeace)
> 8. Re: Cool/useful short examples of Perl? (Peter Corlett)
> 9. Re: Cool/useful short examples of Perl? (Roger Burton West)
> 10. Re: Cool/useful short examples of Perl? (David Matthewman)
> 
> 
> --
> 
> Message: 1
> Date: Wed, 8 Jun 2011 12:07:01 +0100
> From: Paul Makepeace 
> Subject: Re: Someone needs to take jwz aside...
> To: "London.pm Perl M[ou]ngers" 
> Message-ID: 
> Content-Type: text/plain; charset=ISO-8859-1
> 
> On Wed, Jun 8, 2011 at 11:21, David Cantrell  wrote:
> > Of course, it's possible that the Comprehensive Python Archive Network
> > or similar for ruby/javascript/java/C/whatever does exist but I just
> > can't find it. ?But then, if I can't find it, it's not much use.
> 
> (If you were a python programmer and yet had still somehow managed to
> assiduously avoid all mentions of it, you could search for 'python
> packages' (because that's what they're called in python) and would
> find the top result is http://pypi.python.org/pypi)
> 
> 
> Paul
> 
> 
> 
> --
> 
> Message: 2
> Date: Wed, 08 Jun 2011 12:15:51 +0100
> From: Denny <2...@denny.me>
> Subject: Re: Someone needs to take jwz aside...
> To: "London.pm Perl M[ou]ngers" 
> Message-ID: <1307531751.13033.12.ca...@serenity.denny.me>
> Content-Type: text/plain; charset="utf-8"
> 
> On Wed, 2011-06-08 at 12:07 +0100, Paul Makepeace wrote:
> > On Wed, Jun 8, 2011 at 11:21, David Cantrell  wrote:
> > > Of course, it's possible that the Comprehensive Python Archive Network
> > > or similar for ruby/javascript/java/C/whatever does exist but I just
> > > can't find it. But then, if I can't find it, it's not much use.
> > 
> > (If you were a python programmer and yet had still somehow managed to
> > assiduously avoid all mentions of it, you could search for 'python
> > packages' (because that's what they're called in python) and would
> > find the top result is http://pypi.python.org/pypi)
> 
> As helpfully documented here:
> http://wiki.python.org/moin/MovingToPythonFromOtherLanguages
> 
> -- next part --
> A non-text attachment was scrubbed...
> Name: not available
> Type: application/pgp-signature
> Size: 490 bytes
> Desc: This is a digitally signed message part
> Url : 
> http://london.pm.org/pipermail/london.pm/attachments/20110608/5c15a72b/attachment-0001.pgp
> 
> --
> 
> Message: 3
> Date: Wed, 8 Jun 2011 12:19:15 +0100
> From: Peter Edwards 
> Subject: Re: Someone needs to take jwz aside...
> To: "London.pm Perl M[ou]ngers" 
> Message-ID: 
> Content-Type: text/plain; charset=ISO-8859-1
> 
> >
> > On Wed, 8 Jun 2011, David Cantrell wrote:
> >
> > It's the lack of a CPAN-a-like for any other language that keeps me
> >> coming back to perl.
> >>
> >> Of course, it's possible that the Comprehensive Python Archive Network
> >> or similar for ruby/javascript/java/C/whatever does exist but I just
> >> can't find it. But then, if I can't find it, it's not much use.
> >>
> >>
> >> Python repo
> http://pypi.python.org/pypi
> 
> It was fairly chastening a couple of years back looking for a library
> implementing Role Based Access Control and finding that there was a Python
> one but no Perl one
> 

Re: Cool/useful short examples of Perl?

2011-06-08 Thread Kaoru
On Wed, Jun 8, 2011 at 1:41 PM, Paul Makepeace  wrote:
> $ perl -le 'print "$_ == !!$_ ? ", $_ == !!$_ ? "yes" : "no" for (-1,
> 0, 1, 2, undef)'
> -1 == !!-1 ? no
> 0 == !!0 ? yes
> 1 == !!1 ? yes
> 2 == !!2 ? no
>  == !! ? yes

Whether they are equal according to "==" shouldn't matter here, should
it? What matters between if(something) and unless (!something) is
whether something and !!something are the same boolean-wise.

I think this code shows they are all the same?


$ perl -le 'print "$_\tis the same booleanwise as\t!!$_\t", !($_ xor
!!$_) ? "yes" : "no" for (-1, 0, 1, 2, undef)'
-1  is the same booleanwise as  !!-1yes
0   is the same booleanwise as  !!0 yes
1   is the same booleanwise as  !!1 yes
2   is the same booleanwise as  !!2 yes
is the same booleanwise as  !!  yes

-

As has been pointed out "!" can be overridden etc, but I think the use
of unless is safer than your example suggested.

- Alex



Re: Cool/useful short examples of Perl?

2011-06-08 Thread David Matthewman

On 8 Jun 2011, at 13:17, Tom Hukins wrote:

> On Wed, Jun 08, 2011 at 02:00:41PM +0200, Abigail wrote:
>> I'd rather go for sacking people that don't know the difference 
>> between 
>> 
>>if (something) { ... }
>> 
>> and
>> 
>>unless (!something) { ... }
> 
> It's sunny outside and pubs are open:  I can think of worse times to
> lose my job.
> 
>> Or does everyone think they are always equivalent?
> 
> I'm not everyone, and with a language as flexible as Perl I hesitate
> to make strong statements involving words like "always", but I don't
> recall encountering a situation where they differ.

Their side effects are different. There may be a simpler way to demonstrate 
this, but for instance:

#! /usr/bin/env perl

print "unless: ";
print use_unless() . "\n";
print "if: ";
print use_if() . "\n";

sub use_unless {
  my $value = 1;
  unless ($value) {
# nop  
  }
}

sub use_if {
  my $value = 1;
  if (!$value) {
# nop
  }
}

-- 
David Matthewman


Re: Cool/useful short examples of Perl?

2011-06-08 Thread Roger Burton West
On Wed, Jun 08, 2011 at 01:41:29PM +0100, Paul Makepeace wrote:

>$ perl -le 'print "$_ == !!$_ ? ", $_ == !!$_ ? "yes" : "no" for (-1,
>0, 1, 2, undef)'
>-1 == !!-1 ? no
>0 == !!0 ? yes
>1 == !!1 ? yes
>2 == !!2 ? no
> == !! ? yes

Might as well add lua, since I'm trying to learn it at the moment (and
therefore this is unlikely to be idiomatic). Having explicit boolean
types makes it all a bit more predictable, I feel.

$ lua -e 'function process(x) y=not not x;print(x,"==",y,"?",(y==x)) end for 
x=-1,2 do process(x) end process(nil) process(true) process(false)'
-1  ==  true?   false
0   ==  true?   false
1   ==  true?   false
2   ==  true?   false
nil ==  false   ?   false
true==  true?   true
false   ==  false   ?   true

Roger


Re: Cool/useful short examples of Perl?

2011-06-08 Thread Peter Corlett
On 8 Jun 2011, at 13:00, Abigail wrote:
[...]
> I'd rather go for sacking people that don't know the difference 
> between 
>if (something) { ... }
> and
>unless (!something) { ... }
> Or does everyone think they are always equivalent?

Is this a trick question?

I would expect it to be equivalent if "something" is an expression with higher 
precedence than "!". If not, I'd be fascinated to know what the 
counterintuitive behaviour is and why somebody thought it was a good idea. (And 
also how I've managed to treat them as equivalent for years and not get burned.)

For "something" being arbitrary text, no. if($foo && $bar) and unless(!$foo && 
$bar) are of course not equivalent.

I do like "unless". It's more visible than "!". I like it enough that I'll 
invert the sense of an expression using De Morgan's law and use unless to 
eliminate those oft-missed plings. Avoiding misreading trumps avoiding ugliness.





Re: Cool/useful short examples of Perl?

2011-06-08 Thread Paul Makepeace
On Wed, Jun 8, 2011 at 13:17, Tom Hukins  wrote:
> On Wed, Jun 08, 2011 at 02:00:41PM +0200, Abigail wrote:
>> I'd rather go for sacking people that don't know the difference
>> between
>>
>>     if (something) { ... }
>>
>> and
>>
>>     unless (!something) { ... }
>
> It's sunny outside and pubs are open:  I can think of worse times to
> lose my job.
>
>> Or does everyone think they are always equivalent?
>
> I'm not everyone, and with a language as flexible as Perl I hesitate
> to make strong statements involving words like "always", but I don't
> recall encountering a situation where they differ.

$ perl -le 'print "$_ == !!$_ ? ", $_ == !!$_ ? "yes" : "no" for (-1,
0, 1, 2, undef)'
-1 == !!-1 ? no
0 == !!0 ? yes
1 == !!1 ? yes
2 == !!2 ? no
 == !! ? yes


FWIW,
$ python -c 'for x in -1, 0, 1, 2, None: nnx = not not x; print x,
"==", nnx, "?", "yes" if x == nnx else "no"'
-1 == True ? no
0 == False ? yes
1 == True ? yes
2 == True ? no
None == False ? no

(Wait, what? Command line python? Semicolons? Ternary ops?)

Paul



Re: Cool/useful short examples of Perl?

2011-06-08 Thread Tom Hukins
On Wed, Jun 08, 2011 at 02:00:41PM +0200, Abigail wrote:
> I'd rather go for sacking people that don't know the difference 
> between 
> 
> if (something) { ... }
> 
> and
> 
> unless (!something) { ... }

It's sunny outside and pubs are open:  I can think of worse times to
lose my job.

> Or does everyone think they are always equivalent?

I'm not everyone, and with a language as flexible as Perl I hesitate
to make strong statements involving words like "always", but I don't
recall encountering a situation where they differ.

In response to your question, I started out thinking about "zero but
true" values, but this doesn't matter because double negation of
truthfulness won't care about the value.  So I got stuck.

The only special situation I can think of would be when someone
overloads the "!" operator.  I would console anyone doing this on code
I maintain by mentioning that it's sunny outside and pubs are open.

I suspect I've missed lots of other interesting syntactical
peculiarities, though.  Would you mind sharing them?

Tom


Re: Cool/useful short examples of Perl?

2011-06-08 Thread Bill Crawford
On 8 June 2011 13:00, Abigail  wrote:

> I'd rather go for sacking people that don't know the difference
> between
>
>    if (something) { ... }
>
> and
>
>    unless (!something) { ... }
>
>
>
> Or does everyone think they are always equivalent?

There are those (though I wouldn't be so stern) who would advocate
sacking people who wrote overloaded operators that made them not do
the same thing :)

> Abigail

Will.



Re: Cool/useful short examples of Perl?

2011-06-08 Thread Abigail
On Wed, Jun 08, 2011 at 09:24:50AM +0100, Dirk Koopman wrote:
> On 08/06/11 08:31, Abigail wrote:
>>
>>
>>  unless (!something) {
>> ...
>>  }
>>  else {
>> ... # Unless not something isn't true.
>>  }
>>
>
> An earnest question in the interview test along the lines of "what does  
> this mean and when would you use it" should weed several people out.
>
> Instant sacking (or promotion if in Britain) on sight would seem to be  
> an answer.
>


I'd rather go for sacking people that don't know the difference 
between 

if (something) { ... }

and

unless (!something) { ... }



Or does everyone think they are always equivalent?



Abigail


Re: Cool/useful short examples of Perl?

2011-06-08 Thread Paul Makepeace
On Wed, Jun 8, 2011 at 08:31, Abigail  wrote:
> On Wed, Jun 08, 2011 at 07:46:52AM +0200, Richard Foley wrote:
>> I've found a lot of German programmers very uncomfortable with the "unless"
>> keyword, (or maybe it's just c programmers).  They appear to very often 
>> prefer
>> to use a construct of the form:
>>
>>       if ( !something ) { ...
>>
>> Even worse is:
>>
>>       unless ( !something ) { ...
>>
>> The brain just into tailspin goes.
>
>
>    unless (!something) {
>       ...
>    }
>    else {
>       ...     # Unless not something isn't true.

 dont(...);

>    }

Joking aside, I wonder if part of the confusion with unless is that
it's usually associated with some kind of negative when it's in the
prefix form "Unless  (you'll be in trouble | don't ... | ruh-roh)"

Paul



Re: Cool/useful short examples of Perl?

2011-06-08 Thread Dirk Koopman

On 08/06/11 08:31, Abigail wrote:



 unless (!something) {
...
 }
 else {
... # Unless not something isn't true.
 }



An earnest question in the interview test along the lines of "what does 
this mean and when would you use it" should weed several people out.


Instant sacking (or promotion if in Britain) on sight would seem to be 
an answer.


Dirk


Re: Cool/useful short examples of Perl?

2011-06-08 Thread Abigail
On Wed, Jun 08, 2011 at 07:46:52AM +0200, Richard Foley wrote:
> I've found a lot of German programmers very uncomfortable with the "unless" 
> keyword, (or maybe it's just c programmers).  They appear to very often 
> prefer 
> to use a construct of the form:
> 
>   if ( !something ) { ...
> 
> Even worse is:
> 
>   unless ( !something ) { ...
> 
> The brain just into tailspin goes.


unless (!something) {
   ...
}
else {
   ... # Unless not something isn't true.
}



Abigail


Re: Cool/useful short examples of Perl?

2011-06-07 Thread Sue Spence
This distaste is not really limited to Germans or C programmers in my
experience. Anything more complicated than

holiday.picnic() unless raining;

will ideally not appear in any code that I am required to
inspect/support/maintain.

On Wednesday, 8 June 2011, Richard Foley  wrote:
> I've found a lot of German programmers very uncomfortable with the "unless"
> keyword, (or maybe it's just c programmers).  They appear to very often prefer
> to use a construct of the form:
>
>         if ( !something ) { ...
>
> Even worse is:
>
>         unless ( !something ) { ...
>
> The brain just into tailspin goes.
>
> Where
>
> Ciao
>
> Richard
> --
> Richard Foley
> Ciao - shorter than AufWiederSehen!
> http://www.rfi.net/books.html
>
>
>> On 31 May 2011, at 15:02, David Cantrell wrote:
>> > On Mon, May 30, 2011 at 04:27:30PM +0100, Denny wrote:
>> >> On Mon, 2011-05-30 at 15:36 +0100, David Precious wrote:
>> >>> if (! Email::Valid->address($email_address) ) {
>> >>
>> >> Something wrong with 'unless'?
>> >
>> > Yes.  Most of the time you'll either have an 'else' or want to add it
>> > later, and unless ... else is Just Wrong.
>>
>> Well, in that case, it should be:
>>
>>   if (Email::Valid->address($email_address) ) {
>>     # Code for if the e-mail address is valid.
>>     ...
>>   }
>>   else {
>>     # Code you were going to write in the block above.
>>     
>>   }
>>
>> Otherwise your 'else' block is basically a double negative, and (IMAO) just
>> as confusing for an 'else' block for an 'unless'.
>>
>> OK, I admit, I've found myself wanting to add an 'else' block to an
>> 'unless' statement. And it's awkward, but only for the short period of
>> time it takes for me to rewrite it as an 'if' with the original code in
>> the new 'else' block.
>



Re: Cool/useful short examples of Perl?

2011-06-07 Thread Richard Foley
I've found a lot of German programmers very uncomfortable with the "unless" 
keyword, (or maybe it's just c programmers).  They appear to very often prefer 
to use a construct of the form:

if ( !something ) { ...

Even worse is:

unless ( !something ) { ...

The brain just into tailspin goes.

Where 

Ciao

Richard
--
Richard Foley
Ciao - shorter than AufWiederSehen! 
http://www.rfi.net/books.html


> On 31 May 2011, at 15:02, David Cantrell wrote:
> > On Mon, May 30, 2011 at 04:27:30PM +0100, Denny wrote:
> >> On Mon, 2011-05-30 at 15:36 +0100, David Precious wrote:
> >>> if (! Email::Valid->address($email_address) ) {
> >> 
> >> Something wrong with 'unless'?
> > 
> > Yes.  Most of the time you'll either have an 'else' or want to add it
> > later, and unless ... else is Just Wrong.
> 
> Well, in that case, it should be:
> 
>   if (Email::Valid->address($email_address) ) {
> # Code for if the e-mail address is valid.
> ...
>   }
>   else {
> # Code you were going to write in the block above.
> 
>   }
> 
> Otherwise your 'else' block is basically a double negative, and (IMAO) just
> as confusing for an 'else' block for an 'unless'.
> 
> OK, I admit, I've found myself wanting to add an 'else' block to an
> 'unless' statement. And it's awkward, but only for the short period of
> time it takes for me to rewrite it as an 'if' with the original code in
> the new 'else' block.


Re: Cool/useful short examples of Perl?

2011-06-07 Thread David Matthewman

On 31 May 2011, at 15:02, David Cantrell wrote:

> On Mon, May 30, 2011 at 04:27:30PM +0100, Denny wrote:
>> On Mon, 2011-05-30 at 15:36 +0100, David Precious wrote:
>>> if (! Email::Valid->address($email_address) ) {
>> Something wrong with 'unless'?
> 
> Yes.  Most of the time you'll either have an 'else' or want to add it
> later, and unless ... else is Just Wrong.

Well, in that case, it should be:

  if (Email::Valid->address($email_address) ) {
# Code for if the e-mail address is valid.
...
  }
  else {
# Code you were going to write in the block above.

  }

Otherwise your 'else' block is basically a double negative, and (IMAO) just as 
confusing for an 'else' block for an 'unless'.

OK, I admit, I've found myself wanting to add an 'else' block to an 'unless' 
statement. And it's awkward, but only for the short period of time it takes for 
me to rewrite it as an 'if' with the original code in the new 'else' block.

-- 
David Matthewman




Re: Cool/useful short examples of Perl?

2011-06-02 Thread Simon Cozens
On 01/06/2011 18:55, Peter Edwards wrote:
> Here's an idiot simple code colorizer for logs / diffs that I use quite a
> lot
> http://blogs.perl.org/users/peter_edwards/2010/08/colorized-perl-code-snippets-on-ansi-terminals.html

Here's my idiot simple log colorizer:

use Config::Auto; use Term::ANSIColor; $|=1;
my %config = %{Config::Auto::parse()};
while (<>) {
for my $re (keys %config) { /$re/i && print color($config{$re}) }
chomp; print $_, color('reset'),"\n";
}


Re: Cool/useful short examples of Perl?

2011-06-01 Thread Peter Edwards
On 30 May 2011 11:40, Leo Lapworth  wrote:

> Hi,
>
> I'm working on http://learn.perl.org/ and I'd like to have a few rotating
> example of what can be done with Perl on the home page.
>
>
Here's an idiot simple code colorizer for logs / diffs that I use quite a
lot
http://blogs.perl.org/users/peter_edwards/2010/08/colorized-perl-code-snippets-on-ansi-terminals.html


Regards, Peter
http://perl.dragonstaff.co.uk


Re: Cool/useful short examples of Perl?

2011-06-01 Thread Kieren Diment
I'll have a side order of diacritics with that please.

On 01/06/2011, at 7:34 PM, Andy Wardley wrote:

> On 31/05/2011 15:08, David Cantrell wrote:
>> And Hungarian notation is good!
> 
> pronounI adjectiveRespectfully verbDisagree.
> 
> initialA
> 



Re: Cool/useful short examples of Perl?

2011-06-01 Thread Andy Wardley

On 31/05/2011 15:08, David Cantrell wrote:

And Hungarian notation is good!


pronounI adjectiveRespectfully verbDisagree.

initialA



Re: Cool/useful short examples of Perl?

2011-05-31 Thread Jason Clifford
On Tue, 2011-05-31 at 15:06 +0100, Mark Fowler wrote:
> Depends if you're looking semi-technical or not.  How about my hostify
> script that puts DNS entries into /etc/hosts?
> 
> http://blog.twoshortplanks.com/2011/01/30/hostify/

Am I alone in finding the code elements of that page very hard to read -
the deeper the grey gets the harder it is to read against a black
background.



Re: Cool/useful short examples of Perl?

2011-05-31 Thread Christopher Jones
On 31 May 2011, at 15:18, Paul Makepeace wrote:

> On Tue, May 31, 2011 at 15:02, David Cantrell  wrote:
>> On Mon, May 30, 2011 at 04:27:30PM +0100, Denny wrote:
>>> On Mon, 2011-05-30 at 15:36 +0100, David Precious wrote:
 if (! Email::Valid->address($email_address) ) {
>>> Something wrong with 'unless'?
>> 
>> Yes.  Most of the time you'll either have an 'else' or want to add it
>> later, and unless ... else is Just Wrong.
> 
> That's the main problem with unless - once you've written your
> conditional that way, it's impossible to change it.
> 
> P

I see that as a feature, not a problem.


Chris





Re: Cool/useful short examples of Perl?

2011-05-31 Thread David Cantrell
On Mon, May 30, 2011 at 05:47:33PM -0400, Uri Guttman wrote:

> if you think that is line noise, then your perl skills need
> improving.

I presume that you have a copy of K&R?  Is your copy missing the first
few chapters?  No?  Oh, so maybe there's some value in starting with
simple examples instead of jumping straight in with things like:

   int strcmp(char *s, char *t)
   {
   for ( ; *s == *t; s++, t++)
   if (*s == '\0')
   return 0;
   return *s - *t;
   }

-- 
David Cantrell | Official London Perl Mongers Bad Influence

Cum catapultae proscriptae erunt tum soli proscript catapultas habebunt


Re: Cool/useful short examples of Perl?

2011-05-31 Thread Paul Makepeace
On Tue, May 31, 2011 at 15:02, David Cantrell  wrote:
> On Mon, May 30, 2011 at 04:27:30PM +0100, Denny wrote:
>> On Mon, 2011-05-30 at 15:36 +0100, David Precious wrote:
>> > if (! Email::Valid->address($email_address) ) {
>> Something wrong with 'unless'?
>
> Yes.  Most of the time you'll either have an 'else' or want to add it
> later, and unless ... else is Just Wrong.

That's the main problem with unless - once you've written your
conditional that way, it's impossible to change it.

P



Re: Cool/useful short examples of Perl?

2011-05-31 Thread Peter Corlett
On 31 May 2011, at 15:02, David Cantrell wrote:
[...]
> Yes.  Most of the time you'll either have an 'else' or want to add it
> later, and unless ... else is Just Wrong.

Nah, just add an "elsunless" keyword to the language :)




Re: Cool/useful short examples of Perl?

2011-05-31 Thread Mark Fowler
On Mon, May 30, 2011 at 11:40 AM, Leo Lapworth  wrote:

> I'm working on http://learn.perl.org/ and I'd like to have a few rotating
> example of what can be done with Perl on the home page.

How about my short script to put DNS entires into /etc/hosts?

http://blog.twoshortplanks.com/2011/01/30/hostify/

Mark.


Re: Cool/useful short examples of Perl?

2011-05-31 Thread David Cantrell
On Mon, May 30, 2011 at 06:51:11PM +0100, Paul Makepeace wrote:
> On Mon, May 30, 2011 at 14:12, Gabor Szabo  wrote:
> > use Spreadsheet::Read;
> > my $ref = ReadData ("test.xls");
> > say $ref->[1]{A3};
> > http://search.cpan.org/dist/Spreadsheet-Read/Read.pm
> Cool module. If you do include it, s/ref/workbook/
> Paul (who is puzzled by this name-your-var-with-only-its-type cargo cult)

The smaller the code, the fewer bugs it can contain!  And Hungarian
notation is good!

-- 
David Cantrell | Reality Engineer, Ministry of Information

  All praise the Sun God
  For He is a Fun God
  Ra Ra Ra!


Re: Cool/useful short examples of Perl?

2011-05-31 Thread Mark Fowler
Depends if you're looking semi-technical or not.  How about my hostify
script that puts DNS entries into /etc/hosts?

http://blog.twoshortplanks.com/2011/01/30/hostify/

Mark.

On Mon, May 30, 2011 at 11:40 AM, Leo Lapworth  wrote:
> Hi,
>
> I'm working on http://learn.perl.org/ and I'd like to have a few rotating
> example of what can be done with Perl on the home page.
>
> The first two I've thought of are below, does anyone have others?
>
> They don't have to use CPAN modules, one liners are fine as long as it
> is simple to see what they do. I'll have a 'more' link which goes on to
> show full example with line by line explanations.
>
> Module preference is anything from http://search.cpan.org/dist/Task-Kensho/
>
> E.g.:
>
> This is probably max sort of size...
>
>  # Send an email
>  use Email::Sender::Simple qw(sendmail);
>  use Email::Simple;
>  use Email::Simple::Creator;
>
>  my $email = Email::Simple->create(
>    header => [
>      To      => '"Xavier Q. Ample" ',
>      From    => '"Bob Fishman" ',
>      Subject => "don't forget to *enjoy the sauce*",
>    ],
>    body => "This message is short, but at least it's cheap.\n",
>  );
>
>  sendmail($email);
>
> or
>
>  # Serve static files from document root with a directory index
>  # app.psgi
>  use Plack::App::Directory;
>  my $app = Plack::App::Directory->new({ root => "/path/to/htdocs" })->to_app;
>
>  > plackup
>  > Accepting connections at http://0:5000/
>
> Thanks in advance!
>
> Leo
>



Re: Cool/useful short examples of Perl?

2011-05-31 Thread David Cantrell
On Mon, May 30, 2011 at 04:27:30PM +0100, Denny wrote:
> On Mon, 2011-05-30 at 15:36 +0100, David Precious wrote:
> > if (! Email::Valid->address($email_address) ) {
> Something wrong with 'unless'?

Yes.  Most of the time you'll either have an 'else' or want to add it
later, and unless ... else is Just Wrong.

-- 
David Cantrell | Reality Engineer, Ministry of Information

The Law of Daves: in any gathering of technical people, the
number of Daves will be greater than the number of women.


Re: Cool/useful short examples of Perl?

2011-05-31 Thread Steve Mynott
On Mon, May 30, 2011 at 11:40:57AM +0100, Leo Lapworth typed:

> I'm working on http://learn.perl.org/ and I'd like to have a few rotating
> example of what can be done with Perl on the home page.

Examples from 

http://pleac.sourceforge.net/pleac_perl/index.html

(an opensource version of the Perl Cookbook)

-- 
Steve Mynott 


Re: Cool/useful short examples of Perl?

2011-05-31 Thread Mark Keating

On 31/05/2011 04:52, Gabor Szabo wrote:

Gabor
ps. My spell checker wants me to write 'learned' there.
I wonder which one of us is right and how many people out there know
the difference.
Gabor, your spell checker is a moron, thankfully you are not, it is 
'learnt', but spell checkers also hate dreamt as being archaic but keep 
slept, wept and meant!

ps2. Your language is confusing. Let's program in Hungarian.
Our language is confusing :) It is a slut who will sleep with anyone and 
steal from everywhere, on that note it is pps etc. (post scriptum, post 
post scriptum, ad infinitum).


Sorry for going OT

-mdk

--
Mark Keating BA (Hons) | Writer, Photographer, Cat-Herder
Managing Director  | Shadowcat Systems Limited
Director/Secretary | Enlightened Perl Organisation
Marketing Chair| The Perl Foundation Marketing Committee
Marketing and PR   | TPF Steering Committee
co-Leader  | North West England Perl Mongers
Organiser  | London Perl Workshop (UKPW)
co-Organiser   | Dynamic Languages Conference
http://www.shadowcat.co.uk | http://linkedin.com/in/markkeating
http://www.enlightenedperl.org | http://www.perlfoundation.org
http://northwestengland.pm.org | http://www.flavors.me/mdk



Re: Cool/useful short examples of Perl?

2011-05-31 Thread Abigail
On Mon, May 30, 2011 at 10:29:10PM +0100, Dave Hodgkinson wrote:
> 
> On 30 May 2011, at 22:17, Uri Guttman wrote:
> > 
> >  DH> No. Real people want to solve real world problems in a simple,
> >  DH> maintainable way, not wave around a canapé sized penis.
> > 
> > you have a strange way of measuring genitals. real world code is also
> > concise and fast and usable.
> 
> And MAINTAINABLE. 
> 


Maintainability is overrated.


Abigail


Re: Cool/useful short examples of Perl?

2011-05-31 Thread Graeme Hewson
On Tuesday 31 May 2011 04:52:24 Gabor Szabo wrote:

> ps. My spell checker wants me to write 'learned' there.
> I wonder which one of us is right and how many people out there know
> the difference.

Many people are bilingual in American English and British English!


Re: Cool/useful short examples of Perl?

2011-05-30 Thread Leo Lapworth
Hi,

Thanks to those who provided me examples.

I'm glad everyone else has had interesting discussions, it's obvious
that lots of people have opinions.. which is nice, though I'm not looking to
get into discussions, I'm focusing on building a website.

For those interested learn.perl.org is going to have a few examples
(just to spice it up / get someone interested in what is possible), but for
this phase is mostly about:

1) Installing Perl
2) What is a command line (hang out on #win32 for an education in what
people don't know!)
3) How do I run a program ("I've got Perl installed, now what?")
4) What are modules / Installing modules
5) Using perldoc.perl.org / reading FAQ's / Books and other resources
(I have created a page that lists ALL Perl Keywords and links
to the relevant perldoc page, so although 'perldoc -f while' does
not work, there will be a 'while' link on the site pointing to 'perlsyn')

Having been doing a lot of reading around this the perldoc's and
FAQ's really could do with more love - so if you have time please
try and get involved with those projects:

Perldoc: http://perldoc.perl.org/perlrepository.html the /pod/ folder
FAQ: http://faq.perl.org/#How_to_contribute_to

Thanks again - I'll let you know when the site is live.

Cheers

Leo


Re: Cool/useful short examples of Perl?

2011-05-30 Thread Gabor Szabo
On Tue, May 31, 2011 at 2:23 AM, Paul Makepeace  wrote:
> On Mon, May 30, 2011 at 23:20, lesleyb  wrote:
>> I had previous programming experience when I started learning Perl
>> and I was quickly introduced to the 'unless' construct.  I confess
>> to finding it both cute and entirely obvious.
>
> Yeah, seriously. You know, if someone can understand a regular
> expression then a word that behaves exactly the same way as they've
> been familiar with since about age 2 shouldn't cause any problems.

Compared to regexes you are probably right. If regexes won't be
the primary show-case then let me point out, not necessarily to you Paul,
that there is a large part of the world that have not started with
English at that age.  They might have learnt English later but for many
'if not' or 'if !' is still much more familiar than 'unless'.
Unless they have a similar world in their mother tongue or mother
programming language.


Gabor
ps. My spell checker wants me to write 'learned' there.
I wonder which one of us is right and how many people out there know
the difference.

ps2. Your language is confusing. Let's program in Hungarian.



Re: Cool/useful short examples of Perl?

2011-05-30 Thread Avleen Vig
On Mon, May 30, 2011 at 7:45 PM, Damian Conway  wrote:
> I'll happily concede that this example isn't nearly as impressive as
> some of the others in this thread, but its real-world, useful,
> conveniently incremental, requires no modules, and ably
> demonstrates the value of many core features of Perl.

^^^ THIS
This is why perl is great, and this is what should be evangelised about perl.
Not that you can do it in 1 line with strange and obscure syntax.
This is what people learn the most from. Not perl golf.


Re: Cool/useful short examples of Perl?

2011-05-30 Thread 'lesleyb'
On Mon, May 30, 2011 at 07:46:08PM -0400, Uri Guttman wrote:
> > "NC" == Nicholas Clark  writes:
> 
> 
>   NC> You've stated that you *don't* expect newbies to know how to read
>   NC> that line.  Leo is currently looking for things that newbies could
>   NC> be expected to read.
> 
> then you will get one liners that don't do much. you can't have it both
> ways. either they are useful or they are so simple that newbies can read
> them. using modules isn't a way around that as they shouldn't use one
> without understanding it. so my way is to show how useful a small amount
> of perl can be. get them intrigued and wanting to learn how to do
> that. i haven't seen anything else posted here that is even close to
> useful and cool and newbie level yet. but everyone can jump on
> mine. wow.

I suspect the important thing here is TMTOWTDI .

To be absolutely honest I couldn't come up with a one-liner like the one
you suggested.  However it was interesting to read it.

I won't pretend I learned how to write good one-liners from it but maybe
by reading and working through more of the same I could improve.

But I am not a Perl newbie.  And I do agree about the maintenance issue.
But a Perl newbie today might have to maintain such code tomorrow.

Perhaps the concept that Leon is working on could be extended to more 
intermediate levels as well?

Kind Regards

Lesley


Re: Cool/useful short examples of Perl?

2011-05-30 Thread Damian Conway
Uri wrote:

> ever taken a class with damian? he doesn't pussy foot around with
> kiddie code. that line is just like stuff he trains with

No it isn't. Not in my beginners classes, nor in PBP. When I'm teaching
newcomers to Perl, I focus my examples entirely on readability and
maintainability.

For example--and these may be relevant to Dave's original
request--here's an improved implementation of uniq in Perl
(improved in the sense that it doesn't require the repeated lines to
be adjacent):

my %have_printed;
while (my $line = readline) {
next if $have_printed{$line};
print $line;
$have_printed{$line} = 1;
}

The nice thing about this example, is that you can then show
small improvements very easily. For instance, to ignore repeated
lines case-insensitively:

my %have_printed;
while (my $line = readline) {
next if $have_printed{lc $line};
print $line;
$have_printed{lc $line} = 1;
}

and then you can use the repeated call to lc to motivate the use
of the slightly more idiomatic (and hence slightly less
obvious-to-newcomers):

my %have_printed;
while (my $line = readline) {
next if $have_printed{lc $line}++;
print $line;
}

And then you can show one of the handy new features of Perl 5.14,
by extending the example to ignore differences in whitespacing
as well:

my %have_printed;
while (my $line = readline) {
my $abstract_line = lc( $line =~ s/\s+/ /rg );
next if $have_printed{$abstract_line}++;
print $line;
}

(Note the injection of an interim variable in that last example,
precisely because it improves the comprehensibility of the code.)

>From there you can keep adding to the example in further small steps:
print only lines that *are* repeated, print each line with a repetition
count, specifying which parts of each line to consider/ignore, etc. etc.

I'll happily concede that this example isn't nearly as impressive as
some of the others in this thread, but its real-world, useful,
conveniently incremental, requires no modules, and ably
demonstrates the value of many core features of Perl.

Damian


Re: Cool/useful short examples of Perl?

2011-05-30 Thread Uri Guttman
> "NC" == Nicholas Clark  writes:


  NC> You've stated that you *don't* expect newbies to know how to read
  NC> that line.  Leo is currently looking for things that newbies could
  NC> be expected to read.

then you will get one liners that don't do much. you can't have it both
ways. either they are useful or they are so simple that newbies can read
them. using modules isn't a way around that as they shouldn't use one
without understanding it. so my way is to show how useful a small amount
of perl can be. get them intrigued and wanting to learn how to do
that. i haven't seen anything else posted here that is even close to
useful and cool and newbie level yet. but everyone can jump on
mine. wow.

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -


Re: Cool/useful short examples of Perl?

2011-05-30 Thread Paul Makepeace
On Mon, May 30, 2011 at 11:40, Leo Lapworth  wrote:
> I'm working on http://learn.perl.org/ and I'd like to have a few rotating
> example of what can be done with Perl on the home page.

I think this is great,

curl -L http://cpanmin.us | perl - --sudo App::cpanminus

An installer that installs itself!

Then to demonstrate it,

cpanm --sudo App::Ack

Then demonstrate how insanely awesome ack is.

Paul


Re: Cool/useful short examples of Perl?

2011-05-30 Thread Paul Makepeace
On Mon, May 30, 2011 at 23:20, lesleyb  wrote:
> I had previous programming experience when I started learning Perl
> and I was quickly introduced to the 'unless' construct.  I confess
> to finding it both cute and entirely obvious.

Yeah, seriously. You know, if someone can understand a regular
expression then a word that behaves exactly the same way as they've
been familiar with since about age 2 shouldn't cause any problems.

I'm with Uri on showing what's going on - what excites programmers is
the stuff you can do with the language. Either that or they're
probably not programmers.

http://www.codinghorror.com/blog/2006/07/separating-programming-sheep-from-non-programming-goats.html

> If this is perl, then we've lost.

Er, Perl "lost" a long time ago.

import ConfigParser

config = ConfigParser.ConfigParser()
config.read(files)

Paul



Re: Cool/useful short examples of Perl?

2011-05-30 Thread 'lesleyb'
On Mon, May 30, 2011 at 04:40:26PM -0500, Avleen Vig wrote:
> On Mon, May 30, 2011 at 1:35 PM, Jason Clifford  wrote:
> > On Mon, 2011-05-30 at 16:27 +0100, Denny wrote:
> >> >     if (! Email::Valid->address($email_address) ) {
> >>
> >> Something wrong with 'unless'?
> >
> > No but lots of people appear to find "if" to be more readable
> >
> > If you're not worried about readability then why bother with either the
> > if or unless. Just do:
> >
> > use Email::Valid;
> > Email::Valid->address($email_address) || print "Not valid";
> 
> When you're trying to teach someone a new language, you have to relate
> it to things they'll understand.
> Almost every other language has if/then/else, and doing that here
> would definitely be the right thing.
> The perl-specific things should be left as an exercise for the reader,
> after they're comfortable with the basics and not frightened away.
> 

I had previous programming experience when I started learning Perl
and I was quickly introduced to the 'unless' construct.  I confess
to finding it both cute and entirely obvious.  It's a shame other
languages don't have it ... shame unless unless perhaps?.

I guess it depends on the intended audience .

Kind Regards

Lesley


Re: Cool/useful short examples of Perl?

2011-05-30 Thread Denny
On Mon, 2011-05-30 at 17:47 -0400, Uri Guttman wrote:
>  my %conf = read_file( $file_name ) =~ /^(\w+)=(.*)$/mg ;
> 
> that is an assigment to hash, scalar context call on read_file and a
> regex getting out key=val lines. all stuff newbies need to know and must
> learn. it happens to use them all in one basic line. it is all a very
> teachable thing to anyone who knows what a hash, sub call and regex
> are. i don't expect newbies to know how to read that line but i do
> expect them to able to learn from it. that is what proper training is
> about. not feeding pablum to kiddies.

Bear in mind the original brief:

On Mon, 2011-05-30 at 11:40 +0100, Leo Lapworth wrote:
> I'm working on http://learn.perl.org/ and I'd like to have a few
> rotating examples of what can be done with Perl on the home page.
> [...]
> one liners are fine as long as it is simple to see what they do.

So these aren't lessons - they're standalone code samples.  If it needs
explaining it's probably too complicated.

The Config::Any version of the read is:

use Config::Any;
my $config = Config::Any->load_files( { files => \@files } );

That's immediately readable and understandable, and it will load a
config file (or files) written in almost any standard config-file
format, rather than just key=val files.  It doesn't teach people about
all the raw Perl stuff your example uses, but as a standalone "ooh, look
how easy it is to get stuff done in Perl" it's probably more suitable
for the target audience and intended use-case.

Regards,
Denny



signature.asc
Description: This is a digitally signed message part


Re: Cool/useful short examples of Perl?

2011-05-30 Thread Nicholas Clark
On Mon, May 30, 2011 at 05:47:33PM -0400, Uri Guttman wrote:

> learn. it happens to use them all in one basic line. it is all a very
> teachable thing to anyone who knows what a hash, sub call and regex
> are. i don't expect newbies to know how to read that line but i do
> expect them to able to learn from it. that is what proper training is
> about. not feeding pablum to kiddies.

I agree that it's a wonderful basis for a lesson. But it's not what Leo is
looking for. He wrote:

They don't have to use CPAN modules, one liners are fine as long as it
is simple to see what they do.

http://london.pm.org/pipermail/london.pm/Week-of-Mon-20110530/020713.html

You've stated that you *don't* expect newbies to know how to read that line.
Leo is currently looking for things that newbies could be expected to read.

Nicholas Clark


Re: Cool/useful short examples of Perl?

2011-05-30 Thread Uri Guttman
> "DH" == Dave Hodgkinson  writes:

  DH> On 30 May 2011, at 22:47, Uri Guttman wrote:

  >>> "DH" == Dave Hodgkinson  writes:
  >> 
  DH> And MAINTAINABLE. 
  >> 
  DH> Not everyone speaks line noise.
  >> 
  >> if you think that is line noise, then your perl skills need
  >> improving.

  DH> And this is EXACTLY the problem. If this is perl, then we've lost.

nope. it is just you that is lost. perl is all of that and more. you
haven't done much training i bet. you don't give out easy problems as
they don't keep the student's interest. you push them as much as you can
to get to the next level. ever taken a class with damian? he doesn't
pussy foot around with kiddie code. that line is just like stuff he
trains with (and i have plenty of experience in his classes as one of
his producers). getting a student to marvel at interesting code gets
them involved and asking questions. that is what training is about.

and you can mix/match all sorts of short code snippets on
learn.perl. just don't make them all kiddie things or no one will come
back.

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -


Re: Cool/useful short examples of Perl?

2011-05-30 Thread Avleen Vig
On Mon, May 30, 2011 at 4:47 PM, Uri Guttman  wrote:
> if you think that is line noise, then your perl skills need
> improving. seriously i have taught the slurp line to total beginners and
> they get it afterwards. this is for learn.perl.org, not golf.
>
>  my %conf = read_file( $file_name ) =~ /^(\w+)=(.*)$/mg ;
>
> that is an assigment to hash, scalar context call on read_file and a
> regex getting out key=val lines. all stuff newbies need to know and must
> learn. it happens to use them all in one basic line. it is all a very
> teachable thing to anyone who knows what a hash, sub call and regex
> are. i don't expect newbies to know how to read that line but i do
> expect them to able to learn from it. that is what proper training is
> about. not feeding pablum to kiddies.

I mostly idle on this list since I moved back to the US, but this is
something that really irks me.
This is exactly why other languages are getting promoted more than perl:
because people insist on promoting the things about perl that make it
unmaintainable and harder to read, rather than the strengths about how
expandable it is, the massive collection of modules, backwards
compatibility between versions (mostly), and speed.



Re: Cool/useful short examples of Perl?

2011-05-30 Thread Dave Hodgkinson

On 30 May 2011, at 22:47, Uri Guttman wrote:

>> "DH" == Dave Hodgkinson  writes:
> 
>  DH> And MAINTAINABLE. 
> 
>  DH> Not everyone speaks line noise.
> 
> if you think that is line noise, then your perl skills need
> improving.

And this is EXACTLY the problem. If this is perl, then we've lost.




Re: Cool/useful short examples of Perl?

2011-05-30 Thread Avleen Vig
On Mon, May 30, 2011 at 3:02 PM, Uri Guttman  wrote:
>> "D" == Denny  <2...@denny.me> writes:
> you need to challenge them a bit with new stuff instead of
> handing them simple code anyone can do.

It depends entirely.
If we're teaching things to people who already know some perl, then
sure you're right.
But I will bet that people who know some perl almost definitely won't
visit learn.perl.org no matter how challenging or interesting you make
it.
The people who will visit it are people who want to *learn* perl when
they don't know (much of it) at all, or people who want a reference.

Keep the clever challenging stuff as examples in the reference.
Keep the really simple stuff on the front page to draw people in who
think perl isn't for them / too difficult / too strange because they
don't understand the complicated challenging things.


I also hope you don't mean that perl should be challenging for
everyone. That is absolutely the worst and most wrong approach ever.
If I ever find a task challenging, there is a very good chance I'm
doing it wrong.
Simple, easy, not complicated or challenging. This is the way good
things are written.



Re: Cool/useful short examples of Perl?

2011-05-30 Thread Uri Guttman
> "DH" == Dave Hodgkinson  writes:

  DH> On 30 May 2011, at 22:17, Uri Guttman wrote:
  >> 
  DH> No. Real people want to solve real world problems in a simple,
  DH> maintainable way, not wave around a canapé sized penis.
  >> 
  >> you have a strange way of measuring genitals. real world code is also
  >> concise and fast and usable.

  DH> And MAINTAINABLE. 

  DH> Not everyone speaks line noise.

if you think that is line noise, then your perl skills need
improving. seriously i have taught the slurp line to total beginners and
they get it afterwards. this is for learn.perl.org, not golf.

 my %conf = read_file( $file_name ) =~ /^(\w+)=(.*)$/mg ;

that is an assigment to hash, scalar context call on read_file and a
regex getting out key=val lines. all stuff newbies need to know and must
learn. it happens to use them all in one basic line. it is all a very
teachable thing to anyone who knows what a hash, sub call and regex
are. i don't expect newbies to know how to read that line but i do
expect them to able to learn from it. that is what proper training is
about. not feeding pablum to kiddies.

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -



Re: Cool/useful short examples of Perl?

2011-05-30 Thread Avleen Vig
On Mon, May 30, 2011 at 1:35 PM, Jason Clifford  wrote:
> On Mon, 2011-05-30 at 16:27 +0100, Denny wrote:
>> >     if (! Email::Valid->address($email_address) ) {
>>
>> Something wrong with 'unless'?
>
> No but lots of people appear to find "if" to be more readable
>
> If you're not worried about readability then why bother with either the
> if or unless. Just do:
>
> use Email::Valid;
> Email::Valid->address($email_address) || print "Not valid";

When you're trying to teach someone a new language, you have to relate
it to things they'll understand.
Almost every other language has if/then/else, and doing that here
would definitely be the right thing.
The perl-specific things should be left as an exercise for the reader,
after they're comfortable with the basics and not frightened away.



Re: Cool/useful short examples of Perl?

2011-05-30 Thread Dave Hodgkinson

On 30 May 2011, at 22:17, Uri Guttman wrote:
> 
>  DH> No. Real people want to solve real world problems in a simple,
>  DH> maintainable way, not wave around a canapé sized penis.
> 
> you have a strange way of measuring genitals. real world code is also
> concise and fast and usable.

And MAINTAINABLE. 

Not everyone speaks line noise.






Re: Cool/useful short examples of Perl?

2011-05-30 Thread Uri Guttman
> "DH" == Dave Hodgkinson  writes:

  DH> On 30 May 2011, at 21:02, Uri Guttman wrote:



  DH>  you need to challenge them a bit with new stuff instead of
  >> handing them simple code anyone can do.

  DH> No. Real people want to solve real world problems in a simple,
  DH> maintainable way, not wave around a canapé sized penis.

you have a strange way of measuring genitals. real world code is also
concise and fast and usable. not every config needs a module to parse
them.

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -



Re: Cool/useful short examples of Perl?

2011-05-30 Thread Dave Hodgkinson

On 30 May 2011, at 21:02, Uri Guttman wrote:

>> "D" == Denny  <2...@denny.me> writes:
> 
>  D> On Mon, 2011-05-30 at 13:10 -0400, Uri Guttman wrote:
>>> if you think those are complex, you haven't seen enough perl! :)
> 
>  D> I'm trying to think about the target audience, rather than show how
>  D> clever I am.
> 
> that isn't clever in my book. the slurp thing is only one simple regex
> in list context.

And there's the problem.




Re: Cool/useful short examples of Perl?

2011-05-30 Thread Dave Hodgkinson

On 30 May 2011, at 21:02, Uri Guttman wrote:



 you need to challenge them a bit with new stuff instead of
> handing them simple code anyone can do.

No. Real people want to solve real world problems in a simple, maintainable
way, not wave around a canapé sized penis.






Re: Cool/useful short examples of Perl?

2011-05-30 Thread Gabor Szabo
Another example you can add maybe after some further tweaking:


  #!/usr/bin/env perl
  use Dancer;

  get '/' => sub {
  "Hello World!"
  };

  dance;


  $ perl bin/app.pl &
  ...
  $ curl http://localhost:3000/
  Hello world!


>From http://perldancer.org/

Gabor
http://szabgab.com/


Re: Cool/useful short examples of Perl?

2011-05-30 Thread Uri Guttman
> "D" == Denny  <2...@denny.me> writes:

  D> On Mon, 2011-05-30 at 13:10 -0400, Uri Guttman wrote:
  >> if you think those are complex, you haven't seen enough perl! :)

  D> I'm trying to think about the target audience, rather than show how
  D> clever I am.

that isn't clever in my book. the slurp thing is only one simple regex
in list context. i have shown it (a working version!) to intermediate
and newbie perlers and they get it when shown how to break it down. that
is a skill they usually need to learn and this is a real world example
that displays it. just like showing map/grep to the same crowd. they all
shy from it until you really explain it. then they add it to their perl
vocabulary. you need to challenge them a bit with new stuff instead of
handing them simple code anyone can do.

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -


Re: Cool/useful short examples of Perl?

2011-05-30 Thread Uri Guttman
> "PN" == Philip Newton  writes:

  PN> On Mon, May 30, 2011 at 17:59, Uri Guttman  wrote:
  >> 
  >> # Here is a simple and fast way to load and save a simple config file
  >> # made of key=value lines.
  >> 
  >>  my %conf = read_file( $file_name ) =~ /^(\w+)=(\.*)$/mg ;
  >>  write_file( $file_name, {atomic => 1}, map "$_=$conf{$_}\n", keys %conf ;

  PN> I wonder what kind of configuration you use, where values can consist
  PN> only of one or more dots

good catch. but i encode the values in the number of dots. unary math
rules!

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -



Re: Cool/useful short examples of Perl?

2011-05-30 Thread Dave Hodgkinson

On 30 May 2011, at 19:47, Philip Newton wrote:

> On Mon, May 30, 2011 at 17:59, Uri Guttman  wrote:
>> 
>> # Here is a simple and fast way to load and save a simple config file
>> # made of key=value lines.
>> 
>>  my %conf = read_file( $file_name ) =~ /^(\w+)=(\.*)$/mg ;
>>  write_file( $file_name, {atomic => 1}, map "$_=$conf{$_}\n", keys %conf ;
> 
> I wonder what kind of configuration you use, where values can consist
> only of one or more dots

I think his modem just had a small fit.



Re: Cool/useful short examples of Perl?

2011-05-30 Thread Denny
On Mon, 2011-05-30 at 19:35 +0100, Jason Clifford wrote:
> On Mon, 2011-05-30 at 16:27 +0100, Denny wrote:
> > > if (! Email::Valid->address($email_address) ) {
> > 
> > Something wrong with 'unless'?
> 
> No but lots of people appear to find "if" to be more readable

It's not a choice between 'if' and 'unless' though, it's a choice
between 'if not' and 'unless'.  I'm honestly very surprised that any
native English speaker would find 'if not' more readable than 'unless'.

Which reminds me, 'not' is also more readable than '!', while we're on
the subject.  :)



signature.asc
Description: This is a digitally signed message part


Re: Cool/useful short examples of Perl?

2011-05-30 Thread Philip Newton
On Mon, May 30, 2011 at 17:59, Uri Guttman  wrote:
>
> # Here is a simple and fast way to load and save a simple config file
> # made of key=value lines.
>
>  my %conf = read_file( $file_name ) =~ /^(\w+)=(\.*)$/mg ;
>  write_file( $file_name, {atomic => 1}, map "$_=$conf{$_}\n", keys %conf ;

I wonder what kind of configuration you use, where values can consist
only of one or more dots

Cheers,
Philip
-- 
Philip Newton 



Re: Cool/useful short examples of Perl?

2011-05-30 Thread Jason Clifford
On Mon, 2011-05-30 at 16:27 +0100, Denny wrote:
> > if (! Email::Valid->address($email_address) ) {
> 
> Something wrong with 'unless'?

No but lots of people appear to find "if" to be more readable

If you're not worried about readability then why bother with either the
if or unless. Just do:

use Email::Valid;
Email::Valid->address($email_address) || print "Not valid";








Re: Cool/useful short examples of Perl?

2011-05-30 Thread Paul Makepeace
On Mon, May 30, 2011 at 14:12, Gabor Szabo  wrote:
> use Spreadsheet::Read;
>
> my $ref = ReadData ("test.xls");
> say $ref->[1]{A3};
>
> http://search.cpan.org/dist/Spreadsheet-Read/Read.pm

Cool module. If you do include it, s/ref/workbook/

Paul (who is puzzled by this name-your-var-with-only-its-type cargo cult)


Re: Cool/useful short examples of Perl?

2011-05-30 Thread Denny
On Mon, 2011-05-30 at 13:10 -0400, Uri Guttman wrote:
> if you think those are complex, you haven't seen enough perl! :)

I'm trying to think about the target audience, rather than show how
clever I am.



signature.asc
Description: This is a digitally signed message part


Re: Cool/useful short examples of Perl?

2011-05-30 Thread Uri Guttman
> "D" == Denny  <2...@denny.me> writes:

  D> On Mon, 2011-05-30 at 11:59 -0400, Uri Guttman wrote:
  >> from the File::Slurp synopsis. can't get much cooler, short or useful
  >> than this. :)
  >> my %conf = read_file( $file_name ) =~ /^(\w+)=(\.*)$/mg ;
  >> write_file( $file_name, {atomic => 1}, map "$_=$conf{$_}\n", keys %conf ;

  D> It's kind of horrific looking though, and potentially fragile.  Is there
  D> a less hideous way of doing it with another module?  Config::Any covers
  D> the read but not the write...

the file format is the easiest to read/write. also that is the fastest
way to load/save a simple config file. those are parts of the usefulness
of those short lines. and if you think those are complex, you haven't
seen enough perl! :)

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -


Re: Cool/useful short examples of Perl?

2011-05-30 Thread Denny
On Mon, 2011-05-30 at 17:30 +0100, David Precious wrote:
> On Monday 30 May 2011 16:27:30 Denny wrote:
> > On Mon, 2011-05-30 at 15:36 +0100, David Precious wrote:
> > > if (! Email::Valid->address($email_address) ) {
> > 
> > Something wrong with 'unless'?
> 
> we're talking bits of code for people who are just 
> learning Perl, and "unless" might be somewhat new to them still; if they're 
> coming from another language, "if" is probably going to be more familiar and 
> easier to mentally parse, I'd imagine.

Hr.  I see your point, but personally I'd be reluctant to teach
people only the bits of Perl that are the same as PHP, C, whatever.
That's not going to show them any reason to start using and enjoying
Perl for its strengths.

I'd use 'unless' as a post-condition - it's almost as easy to parse as
English then.

say "Sorry, that email address is not valid." 
unless Email::Valid->address( $email_address );



signature.asc
Description: This is a digitally signed message part


Re: Cool/useful short examples of Perl?

2011-05-30 Thread Dave Hodgkinson

On 30 May 2011, at 16:59, Uri Guttman wrote:

> 
> from the File::Slurp synopsis. can't get much cooler, short or useful
> than this. :)
> 
> 
> # Here is a simple and fast way to load and save a simple config file
> # made of key=value lines.
> 
>  my %conf = read_file( $file_name ) =~ /^(\w+)=(\.*)$/mg ;
>  write_file( $file_name, {atomic => 1}, map "$_=$conf{$_}\n", keys %conf ;

Jack Nicklaus called.



Re: Cool/useful short examples of Perl?

2011-05-30 Thread Dave Hodgkinson

On 30 May 2011, at 16:27, Denny wrote:

> On Mon, 2011-05-30 at 15:36 +0100, David Precious wrote:
>>if (! Email::Valid->address($email_address) ) {
> 
> Something wrong with 'unless'?
> 

Ask Damian.

I also agree, it's a speedbump for my reading. KISS etc.



Re: Cool/useful short examples of Perl?

2011-05-30 Thread Ben Tisdall
Config::Tiny fits the bill nicely.

On Mon, May 30, 2011 at 5:13 PM, Denny <2...@denny.me> wrote:
> On Mon, 2011-05-30 at 11:59 -0400, Uri Guttman wrote:
>> from the File::Slurp synopsis. can't get much cooler, short or useful
>> than this. :)
>>   my %conf = read_file( $file_name ) =~ /^(\w+)=(\.*)$/mg ;
>>   write_file( $file_name, {atomic => 1}, map "$_=$conf{$_}\n", keys %conf ;
>
> It's kind of horrific looking though, and potentially fragile.  Is there
> a less hideous way of doing it with another module?  Config::Any covers
> the read but not the write...
>
>



Re: Cool/useful short examples of Perl?

2011-05-30 Thread David Precious
On Monday 30 May 2011 16:27:30 Denny wrote:
> On Mon, 2011-05-30 at 15:36 +0100, David Precious wrote:
> > if (! Email::Valid->address($email_address) ) {
> 
> Something wrong with 'unless'?

Depends whether you follow Damian Conway's PBP strictly :)

At $work, we mostly do follow PBP, but with some exceptions - avoiding unless 
is one of the bits we ignore when it makes code clearer.

In this case, however, we're talking bits of code for people who are just 
learning Perl, and "unless" might be somewhat new to them still; if they're 
coming from another language, "if" is probably going to be more familiar and 
easier to mentally parse, I'd imagine.


-- 
David Precious  ("bigpresh")
http://www.preshweb.co.uk/

   "Programming is like sex. One mistake and you have to support
   it for the rest of your life". (Michael Sinz)


Re: Cool/useful short examples of Perl?

2011-05-30 Thread Denny
On Mon, 2011-05-30 at 11:59 -0400, Uri Guttman wrote:
> from the File::Slurp synopsis. can't get much cooler, short or useful
> than this. :)
>   my %conf = read_file( $file_name ) =~ /^(\w+)=(\.*)$/mg ;
>   write_file( $file_name, {atomic => 1}, map "$_=$conf{$_}\n", keys %conf ;

It's kind of horrific looking though, and potentially fragile.  Is there
a less hideous way of doing it with another module?  Config::Any covers
the read but not the write...



signature.asc
Description: This is a digitally signed message part


Re: Cool/useful short examples of Perl?

2011-05-30 Thread Uri Guttman

from the File::Slurp synopsis. can't get much cooler, short or useful
than this. :)


# Here is a simple and fast way to load and save a simple config file
# made of key=value lines.

  my %conf = read_file( $file_name ) =~ /^(\w+)=(\.*)$/mg ;
  write_file( $file_name, {atomic => 1}, map "$_=$conf{$_}\n", keys %conf ;


-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -


Re: Cool/useful short examples of Perl?

2011-05-30 Thread Pedro Figueiredo
Something I use quite a lot when skimming through log files:

perl -le 'print scalar localtime shift'

Ok, probably less than you were looking for...

Cheers,

Pedro


Re: Cool/useful short examples of Perl?

2011-05-30 Thread Denny
On Mon, 2011-05-30 at 15:36 +0100, David Precious wrote:
> if (! Email::Valid->address($email_address) ) {

Something wrong with 'unless'?



signature.asc
Description: This is a digitally signed message part


Re: Cool/useful short examples of Perl?

2011-05-30 Thread 'lesleyb'
On Mon, May 30, 2011 at 03:36:54PM +0100, David Precious wrote:
> On Monday 30 May 2011 14:27:25 'lesleyb' wrote:
> > I am a little fearful people will substitute variables on the
> > RHS in a CGI script without untainting first and then complain
> > when the problems show up.  
> 
> Whilst I agree helping people learn about taint mode and how to untaint is 
> valuable, I'm not sure it has a place in "look how simple this can be" / "how 
> easy it is to do cool stuff" examples.
> 
> Taint mode can be useful, but a user trying out simple examples is unlikely 
> to 
> have enabled taint mode unless they know about it.
> 
> 
> > [...] Even an example of how to untaint a
> > 'basic' RFC822 email address?
> [...]
> > if ($data =~ /^([-\@\w.]+)$/) {
> >   $data = $1; # $data now untainted
> > } else {
> >   die "Bad data in '$data'";  # log this somewhere
> > }
> 
> I'd really not want to see people being encouraged to attempt to validate 
> email addresses with a regex; that's a wheel that should not be re-invented; 
> using e.g. Email::Valid to both untaint and check for validity properly would 
> be a far better approach IMO.
> 
> In fact, a "how to validate an email address properly" example would probably 
> be worthwhile, for instance:
> 
> use Email::Valid;
> 
> if (! Email::Valid->address($email_address) ) {
> print "Sorry, that email address is not valid!";
> }
> 
Miles better :) 

Kind Regards

Lesley


Re: Cool/useful short examples of Perl?

2011-05-30 Thread Dave Hodgkinson

On 30 May 2011, at 11:40, Leo Lapworth wrote:

> Hi,
> 
> I'm working on http://learn.perl.org/ and I'd like to have a few rotating
> example of what can be done with Perl on the home page.

Like this concept. Some idioms based around some of the major modules would be
nice.

Especially, bit not limited to, Zeframware.

LWP with ua hacking, XML::Twig, DBIC passing direct into TT...





  1   2   >