in reply to Convert time string to seconds

What do you think how the code could be rewritten to avoid these warnings?

Use no warnings 'uninitialized';

in the inner scope where you do the arithmetics.

That tells the reader of your code knows exactly that the undef being interpreted as 0 is intentional. The effect of the "no warnings" is limited to the scope it is in, so there's no harm for the program as a whole.

Perl 6 - links to (nearly) everything that is Perl 6.

Replies are listed 'Best First'.
Re^2: Convert time string to seconds
by SuicideJunkie (Vicar) on Aug 16, 2010 at 13:35 UTC

    For less typing, tighter "scope" and to avoid suppressing other warnings at the same time, you can simply say $1||0, or in this case ($+{'min'}||0).

      I knew somebody would suggest this method :-)

      It's just that when I add code to surpress a warning, "switch off the warning" feels more direct to me than "avoid the warnings".

      Since it's not an error, but a warning, using undef as 0 is legitime, and I find avoiding the feature at all rather backwards. After all warnings should help me write better code, not force me to write different application logic.

      Perl 6 - links to (nearly) everything that is Perl 6.

        I'm sure there is a place for both.

        In the case of a multiline block, usually there are other variables that you really do want to hear about being undeffed, regardless of the fact that you don't want to hear about a couple specific ones... On the other hand, in a tiny block, typing out the whole no warnings 'uninitialized'; seems excessive.

        For me, at least, the middle ground that calls for "no warnings" is fairly narrow but does exist.