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

Bill Ricker <[email protected]>
Newsgroups gmane.comp.lang.perl.perl-mongers.boston
Message-ID <CAAbKA3WA-S4z+1t+e0i84dAfy13r75Y4wojf7S8996BJVOkb6Q@mail.gmail.com>
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 <[email protected]> wrote:
> On 11/09/2016 01:40 PM, Morse, Richard E.,MGH wrote:
>>>
>>> On Nov 9, 2016, at 12:49 PM, Mike Small <[email protected]> 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
> [email protected]
> http://mail.pm.org/mailman/listinfo/boston-pm



-- 
Bill Ricker
[email protected]
https://www.linkedin.com/in/n1vux

_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.