Re: [Boston.pm] catenation of undef with string, 2 cases

2016-11-09 Thread Mike Small
Conor Walsh  writes:

> I thought Perl 6 was supposed to fix the DWIM bug.

IIRC, I read the other day it narrows it so that only an assignment to a
data structure element autovivifies but an attempted access does
not. That would eliminate some do what I don't mean cases, eh?

-- 
Mike Small
sma...@sdf.org

___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] catenation of undef with string, 2 cases

2016-11-09 Thread Conor Walsh
I thought Perl 6 was supposed to fix the DWIM bug.

On Wed, Nov 9, 2016 at 3:38 PM, Bill Ricker  wrote:
> On Wed, Nov 9, 2016 at 3:24 PM, Mike Small  wrote:
>> I have trouble
>> remembering the handy special cases but when it's somewhat systematic
>> that helps.
>
> yeah, the DWIM magic is really nice when it really is what we meant,
> but is surprising when it's internally consistent in it's own special
> way, not in the way that would seem consistent for this code i'm
> working on today. :-)
>
> --
> Bill Ricker
> bill.n1...@gmail.com
> https://www.linkedin.com/in/n1vux
>
> ___
> Boston-pm mailing list
> Boston-pm@mail.pm.org
> http://mail.pm.org/mailman/listinfo/boston-pm

___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] catenation of undef with string, 2 cases

2016-11-09 Thread Uri Guttman

On 11/09/2016 03:24 PM, Mike Small wrote:

Uri Guttman  writes:


On 11/09/2016 02:04 PM, Bill Ricker wrote:

I think Uri and Ricky have it nailed.

You can
wrap with do{ no warnings; ...  }  or
protect the concatenation with  42 . ($b//q()); or equivalent ?: or or
use $b .= 42 ; if order doesn't matter (it usually does, though)
or initialized $b to '' instead of undef, knowing it will be
concatenated (but in that case be sure it's tested for Truth not
Definedness)



just to add on, i use .= so often. i like to build up strings and then
return the whole string. i never initialize them to '' as i know .=
works without warning. this is true for scalars and data structures as
i said before. any lvalue undef can be used in this way.

uri


Thanks everyone. Especially thanks for relating it to +=. I have trouble
remembering the handy special cases but when it's somewhat systematic
that helps.

another systemic and related thing in perl is autovivification. i have 
seen newbies always check for undef and then assigning a hash/array ref 
before adding elements to the hash/array. this is how some other langs 
require things to be done. perl has always had neat shortcuts like 
autoviv and no undef warnings on +=, .= and ++ because those are very 
popular operations and having perl do some init behind the scenes for 
you is way cool.


uri


___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] catenation of undef with string, 2 cases

2016-11-09 Thread Mike Small
Uri Guttman  writes:

> On 11/09/2016 02:04 PM, Bill Ricker wrote:
>> I think Uri and Ricky have it nailed.
>>
>> You can
>> wrap with do{ no warnings; ...  }  or
>> protect the concatenation with  42 . ($b//q()); or equivalent ?: or or
>> use $b .= 42 ; if order doesn't matter (it usually does, though)
>> or initialized $b to '' instead of undef, knowing it will be
>> concatenated (but in that case be sure it's tested for Truth not
>> Definedness)
>>
>>
> just to add on, i use .= so often. i like to build up strings and then
> return the whole string. i never initialize them to '' as i know .=
> works without warning. this is true for scalars and data structures as
> i said before. any lvalue undef can be used in this way.
>
> uri
>

Thanks everyone. Especially thanks for relating it to +=. I have trouble
remembering the handy special cases but when it's somewhat systematic
that helps.

-- 
Mike Small
sma...@sdf.org

___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] catenation of undef with string, 2 cases

2016-11-09 Thread Uri Guttman

On 11/09/2016 02:04 PM, Bill Ricker wrote:

I think Uri and Ricky have it nailed.

You can
wrap with do{ no warnings; ...  }  or
protect the concatenation with  42 . ($b//q()); or equivalent ?: or or
use $b .= 42 ; if order doesn't matter (it usually does, though)
or initialized $b to '' instead of undef, knowing it will be
concatenated (but in that case be sure it's tested for Truth not
Definedness)


just to add on, i use .= so often. i like to build up strings and then 
return the whole string. i never initialize them to '' as i know .= 
works without warning. this is true for scalars and data structures as i 
said before. any lvalue undef can be used in this way.


uri


___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] catenation of undef with string, 2 cases

2016-11-09 Thread Bill Ricker
I think Uri and Ricky have it nailed.

You can
wrap with do{ no warnings; ...  }  or
protect the concatenation with  42 . ($b//q()); or equivalent ?: or or
use $b .= 42 ; if order doesn't matter (it usually does, though)
or initialized $b to '' instead of undef, knowing it will be
concatenated (but in that case be sure it's tested for Truth not
Definedness)



On Wed, Nov 9, 2016 at 1:46 PM, Uri Guttman  wrote:
> On 11/09/2016 01:40 PM, Morse, Richard E.,MGH wrote:
>>>
>>> On Nov 9, 2016, at 12:49 PM, Mike Small  wrote:
>>>
>>>
>>> #!/usr/pkg/bin/perl
>>> use warnings;
>>>
>>> my $a;
>>> $a .= '70';
>>> my $b;
>>> $b = 42 . $b;
>>> print "$a, $b\n";
>>>
>>>
>>> With the script above I get an uninitialized value warning from perl
>>> 5.24 for the second concatenation but not the first. Is there a story
>>> behind this? Something to do with the first case being something you'd
>>> likely want to do without whinging from the interpreter?
>>
>> Interestingly, if you swap the order of the second concatenation, you
>> don’t get the warning, which makes it consistent.
>>
>> I don’t know if this is the reason, but here’s a place this would be
>> somewhat useful:
>>
>> ...
>> my $out;
>> foreach my $k (sort keys %h) {
>> $out .= “$k: $h{$k}\n”;
>> }
>> ...
>>
>> otherwise, you have to start with:
>>
>> my $out = ‘’;
>>
>> (admittedly, that’s how I always start this, but it’s interesting to note
>> that I could leave out the assignment to ‘’.)
>>
>>
> appending to undef is handled like incrementing (or +=) an undef. it is too
> common and useful to trigger a warning. not requiring initialization to ''
> or 0 saves a lot of code. note that this is especially true with data
> structures. $href->{text} .= $stuff is very useful. if i had to initialize
> it to '' beforehand i would hate it.
>
> this works for .= += and ++ (maybe some other things but those are the
> primary ones).
>
> uri
>
>
>
>
> ___
> Boston-pm mailing list
> Boston-pm@mail.pm.org
> http://mail.pm.org/mailman/listinfo/boston-pm



-- 
Bill Ricker
bill.n1...@gmail.com
https://www.linkedin.com/in/n1vux

___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm

Re: [Boston.pm] catenation of undef with string, 2 cases

2016-11-09 Thread Uri Guttman

On 11/09/2016 01:40 PM, Morse, Richard E.,MGH wrote:

On Nov 9, 2016, at 12:49 PM, Mike Small  wrote:


#!/usr/pkg/bin/perl
use warnings;

my $a;
$a .= '70';
my $b;
$b = 42 . $b;
print "$a, $b\n";


With the script above I get an uninitialized value warning from perl
5.24 for the second concatenation but not the first. Is there a story
behind this? Something to do with the first case being something you'd
likely want to do without whinging from the interpreter?

Interestingly, if you swap the order of the second concatenation, you don’t get 
the warning, which makes it consistent.

I don’t know if this is the reason, but here’s a place this would be somewhat 
useful:

...
my $out;
foreach my $k (sort keys %h) {
$out .= “$k: $h{$k}\n”;
}
...

otherwise, you have to start with:

my $out = ‘’;

(admittedly, that’s how I always start this, but it’s interesting to note that 
I could leave out the assignment to ‘’.)


appending to undef is handled like incrementing (or +=) an undef. it is 
too common and useful to trigger a warning. not requiring initialization 
to '' or 0 saves a lot of code. note that this is especially true with 
data structures. $href->{text} .= $stuff is very useful. if i had to 
initialize it to '' beforehand i would hate it.


this works for .= += and ++ (maybe some other things but those are the 
primary ones).


uri



___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm

Re: [Boston.pm] catenation of undef with string, 2 cases

2016-11-09 Thread Morse, Richard E.,MGH
> On Nov 9, 2016, at 12:49 PM, Mike Small  wrote:
> 
> 
> #!/usr/pkg/bin/perl
> use warnings;
> 
> my $a;
> $a .= '70';
> my $b;
> $b = 42 . $b;
> print "$a, $b\n";
> 
> 
> With the script above I get an uninitialized value warning from perl
> 5.24 for the second concatenation but not the first. Is there a story
> behind this? Something to do with the first case being something you'd
> likely want to do without whinging from the interpreter?

Interestingly, if you swap the order of the second concatenation, you don’t get 
the warning, which makes it consistent.

I don’t know if this is the reason, but here’s a place this would be somewhat 
useful:

...
my $out;
foreach my $k (sort keys %h) {
$out .= “$k: $h{$k}\n”;
}
...

otherwise, you have to start with:

my $out = ‘’;

(admittedly, that’s how I always start this, but it’s interesting to note that 
I could leave out the assignment to ‘’.)

Ricky


The information in this e-mail is intended only for the person to whom it is
addressed. If you believe this e-mail was sent to you in error and the e-mail
contains patient information, please contact the Partners Compliance HelpLine at
http://www.partners.org/complianceline . If the e-mail was sent to you in error
but does not contain patient information, please contact the sender and properly
dispose of the e-mail.

___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm

[Boston.pm] catenation of undef with string, 2 cases

2016-11-09 Thread Mike Small

#!/usr/pkg/bin/perl
use warnings;

my $a;
$a .= '70';
my $b;
$b = 42 . $b;
print "$a, $b\n";


With the script above I get an uninitialized value warning from perl
5.24 for the second concatenation but not the first. Is there a story
behind this? Something to do with the first case being something you'd
likely want to do without whinging from the interpreter?

- Mike

___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm