Re: code line of the day

2006-09-13 Thread Bart Lateur
On Fri, 8 Sep 2006 08:19:21 -0700 (PDT), John Douglas Porter wrote:

If you really want to test the ref type, do so
robustly using the methods in Scalar::Util.

I'm not convinced that is indeed the best  approach. So Uri expects
either a plain string, or a scalar ref to a string.

What if instead you get an object? Do you want to treat it as a scalar
ref, or as a plain string? IMO you *don't* want to peer inside the
intestines of an object, instead, you ought to treat the object as a
black box, *not* look at what kind of blessed ref it is, and thus, you
should be wanting to use the stringified object.

Thus: treat only an unblessed scalar ref as a scalar ref.

-- 
Bart.


Re: local vs. my (was Re: code line of the day)

2006-09-10 Thread A. Pagaltzis
Hi Chris,

* Chris Dolan [EMAIL PROTECTED] [2006-09-09 21:00]:
 I didn't mean that to be insulting -- I was just teasing. […]
 That was for my curiosity and to convince me, […] I apologize
 for my less-than-clear motives.

no problem, not your fault. I’m a bit impatient sometimes, which
can lead to unwarranted grumpiness.

Regards,
-- 
Aristotle Pagaltzis // http://plasmasturm.org/


Re: local vs. my (was Re: code line of the day)

2006-09-09 Thread Chris Dolan

On Sep 9, 2006, at 3:12 AM, A. Pagaltzis wrote:


* Chris Dolan [EMAIL PROTECTED] [2006-09-09 03:55]:

Works the same. I often use `local $_` in tiny functions that
mangle just a single value. Matter of taste/style.


Ahh, I see -- cargo cult.  ;-)


Err, what? I chose that style for myself. I didn’t pick it up
from anyone else and certainly not unthinkingly.


I didn't mean that to be insulting -- I was just teasing.  In fact I  
usually respect your contributions a lot, so I was curious why your  
style was different from mine and if there was a reason why yours  
might be better.



I just benchmarked the  two versions, and my wins by a wide
margin:

% perl test.pl
  Rate localmy
local 467290/s--  -33%
my699301/s   50%--


I dunno if that was supposed to be an argument, but I couldn’t
care less. I hope I don’t have to explain why as well.


That was for my curiosity and to convince me, not to convince you.   
That's why I changed the subject line in the message.  I apologize  
for my less-than-clear motives.


Chris

--
Chris Dolan, Software Developer, Clotho Advanced Media Inc.
608-294-7900, fax 294-7025, 1435 E Main St, Madison WI 53703
vCard: http://www.chrisdolan.net/ChrisDolan.vcf

Clotho Advanced Media, Inc. - Creators of MediaLandscape Software  
(http://www.media-landscape.com/) and partners in the revolutionary  
Croquet project (http://www.opencroquet.org/)





Re: local vs. my (was Re: code line of the day)

2006-09-09 Thread Philippe BooK Bruhat
Le samedi 09 septembre 2006 à 10:12, A. Pagaltzis écrivait:
 
 * Uri Guttman [EMAIL PROTECTED] [2006-09-09 05:40]:
  use local only when you MUST use it. mjd has a good article on
  the 7 valid uses of local. just declaring vars in a sub is not
  one of them.
 
 If you use $_ in any way within a sub, you better localise it or
 someone who calls your code is going to be very surprised at some
 point (that includes yourself).

Why is why you can write « my $_ » in recent Perls, I think.

-- 
 Philippe BooK Bruhat

 Honesty is its own reward. Dishonesty is its own punishment.
(Moral from Groo The Wanderer #30 (Epic))


Re: code line of the day

2006-09-08 Thread John Douglas Porter

 * Uri Guttman [EMAIL PROTECTED] [2006-09-07 09:30]:
  
  @{$self-{templates}}{ keys %{$tmpls} } =
  map ref $_ eq 'SCALAR' ? \${$_} : \$_, values %{$tmpls} ;


If we're looking for ways to do it differently, possibly better:

my %copy = %$tmpls;
$_ = ref $_ ? \$$_ : \$_ for values %copy;
@{$self-{templates}}{ keys %copy } = values %copy;

Three statements vs 1, and a temporary variable...
I'm not sure that's better either.  Maybe clearer.

-- 
John Douglas Porter


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


Re: code line of the day

2006-09-08 Thread John Douglas Porter

A. Pagaltzis [EMAIL PROTECTED] wrote:
 sub flatten_copy {
 local $_ = shift;
 ref $_ eq 'SCALAR' ? $$_ : $_;
 }
 
 my %copy = %$tmpls;
 $_ = \( flatten_copy $_ ) for values %copy;
 @{$self-{templates}}{ keys %copy } = values %copy;

Excellent!

I believe it's better to do

  ref $_ ? $$_ : $_

than

  ref $_ eq 'SCALAR' ? $$_ : $_

because, in the first case, you get an actual error
in the exceptional case (a ref of the wrong type),
whereas in the latter case you get a useless and
wrong string in the value, which you might not catch
until later, if ever.

If you really want to test the ref type, do so
robustly using the methods in Scalar::Util.

-- 
John Douglas Porter


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


Re: code line of the day

2006-09-08 Thread A. Pagaltzis
* Chris Dolan [EMAIL PROTECTED] [2006-09-08 17:10]:
 Why did you use local?  Shouldn't the following work?
 
 sub flatten_copy {
 my $s = shift;
 ref $s eq 'SCALAR' ? $$s : $s;
 }

Works the same. I often use `local $_` in tiny functions that
mangle just a single value. Matter of taste/style.

Regards,
-- 
Aristotle Pagaltzis // http://plasmasturm.org/


local vs. my (was Re: code line of the day)

2006-09-08 Thread Chris Dolan

On Sep 8, 2006, at 6:48 PM, A. Pagaltzis wrote:


* Chris Dolan [EMAIL PROTECTED] [2006-09-08 17:10]:

Why did you use local?  Shouldn't the following work?

sub flatten_copy {
my $s = shift;
ref $s eq 'SCALAR' ? $$s : $s;
}


Works the same. I often use `local $_` in tiny functions that
mangle just a single value. Matter of taste/style.


Ahh, I see -- cargo cult.  ;-)

Changing the topic a little bit...  I've always suspected there was a  
speed difference between local and my.  So, I just benchmarked the  
two versions, and my wins by a wide margin:


% perl test.pl
  Rate localmy
local 467290/s--  -33%
my699301/s   50%--

% perl -v

This is perl, v5.8.6 built for darwin-thread-multi-2level
(with 2 registered patches, see perl -V for more detail)


=  test.pl ==
#!/usr/bin/perl -w

use strict;
use Benchmark qw(cmpthese);

my $s = 'foo';
$_ = 'foo';

cmpthese(1_000_000, {
   'local' = sub {
  flatten_copy_local('bar');
   },
   'my' = sub {
  flatten_copy_my('bar');
   },
});

sub flatten_copy_local {
   local $_ = shift;
   ref $_ eq 'SCALAR' ? $$_ : $_;
}
sub flatten_copy_my {
   my $s = shift;
   ref $s eq 'SCALAR' ? $$s : $s;
}


--
Chris Dolan, Software Developer, Clotho Advanced Media Inc.
608-294-7900, fax 294-7025, 1435 E Main St, Madison WI 53703
vCard: http://www.chrisdolan.net/ChrisDolan.vcf

Clotho Advanced Media, Inc. - Creators of MediaLandscape Software  
(http://www.media-landscape.com/) and partners in the revolutionary  
Croquet project (http://www.opencroquet.org/)





Re: code line of the day

2006-09-07 Thread Randal L. Schwartz
 Uri == Uri Guttman [EMAIL PROTECTED] writes:

Uri@{$self-{templates}}{ keys %{$tmpls} } =
Urimap ref $_ eq 'SCALAR' ? \${$_} : \$_, values %{$tmpls} ;

Uri discuss amongst yourselves. topics include: what does it do?

Uh, it throws a lot of warnings when values(%$tmpls) has non-references?

What do I win?

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
merlyn@stonehenge.com URL:http://www.stonehenge.com/merlyn/
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!


Re: code line of the day

2006-09-07 Thread Ronald J Kimball
On Thu, Sep 07, 2006 at 07:21:07AM -0700, Randal L. Schwartz wrote:
  Uri == Uri Guttman [EMAIL PROTECTED] writes:
 
 Uri  @{$self-{templates}}{ keys %{$tmpls} } =
 Uri  map ref $_ eq 'SCALAR' ? \${$_} : \$_, values %{$tmpls} ;
 
 Uri discuss amongst yourselves. topics include: what does it do?
 
 Uh, it throws a lot of warnings when values(%$tmpls) has non-references?
 
 What do I win?

Why would it throw warnings?  ref() returns empty string if the argument
isn't a reference.

Ronald


Re: code line of the day

2006-09-07 Thread Bart Lateur
On Thu, 07 Sep 2006 03:29:02 -0400, Uri Guttman wrote:

this line of my code grew to its present form which i find amusing. 

   @{$self-{templates}}{ keys %{$tmpls} } =
   map ref $_ eq 'SCALAR' ? \${$_} : \$_, values %{$tmpls} ;

discuss amongst yourselves. topics include: what does it do? wtf am i
doing that? and general esthetics.

It makes a ref to a (stringified) copy of every scalar, or dereferenced
scalar ref.

Why would anyone take a copy instead of using an alias? Well, duh! To
make it safe to modify without touching the original, of course. Why you
are storing references instead of the string values, I don't know. It
won't save space.

BTW I assume this module is the subject of the slides at
http://www.sysarch.com/tiny_template/slides/index.html, no?

-- 
Bart.


Re: code line of the day

2006-09-07 Thread A. Pagaltzis
* Uri Guttman [EMAIL PROTECTED] [2006-09-07 09:30]:
 this line of my code grew to its present form which i find amusing. 
 
   @{$self-{templates}}{ keys %{$tmpls} } =
   map ref $_ eq 'SCALAR' ? \${$_} : \$_, values %{$tmpls} ;

my ( $k, $v );
$self-templates-{ $k } = ref $v eq 'SCALAR' ? \${$v} : \$v
while ( $k, $v ) = each %$tmpls;

Hmm. Don’t think I like that better.

[ delete 3 different attempts to make it better ]

Ugh. I think the core problem is that Perl actually does very
little to help you operate on hashes as a whole.

Regards,
-- 
Aristotle Pagaltzis // http://plasmasturm.org/