Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
It's not weird its perlish.TM ;-)
But seriously, using "uninitialized" variables is done all the time in perl. The weird part is trying to figure out when doing so will trigger a warning and when it won't. I'll be very impressed if you can tell what warnings the following code will produce:
#!/usr/bin/perl -T use warnings; use strict; my ($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m); $a++; $b += 1; $c = $c + 1; $d->{size} = 'big'; $e->[5] = 35; $_ .= "$f"; $_ .= $g; $_ = 'h=' . $h; $_ = $i . '=i'; $_ = "j=$j"; $_ = "$k=k"; $_ = $l x 5; $_ = "$m" x 5;

ANS: $c,$f,$g,$h,$j, and $m are used it ways that trigger warnings $a++; # this is ok $b += 1; # this is ok $c = $c + 1; # but this is bad??? $d->{size} = 'big'; # both are just fine... treating undef as a hash + ref $e->[5] = 35; # is ok, but treat it like a 0 and I get yelled +at? $_ .= "$f"; # bad $_ .= $g; # still bad $_ = 'h=' . $h; # bad.... ok so you can't treat undef like a string $_ = $i . '=i'; # wait, no warning, maybe you can $_ = "j=$j"; # nope this tosses an error $_ = "$k=k"; # but this doesn't.... huh? $_ = $l x 5; # neither does this $_ = "$m" x 5; # but if I do the stringifing myself I get yelled at

So, how did you do? I contend that you would have scored better on this quiz if I had asked about the values of the expressions rather than about the warnnings they may or may not trigger.

Learning how undef behaves is easy... learning when it tosses warnings is hard. This can lead to gratuitious special-casing (i.e. if ($x && $x > 3) {$y = ($z||0) + 5)) whose sole purpose is to explicitly state that undef should be treated like, well, undef!


In reply to Re: Re: no warnings 'uninitialized' (was:Time to seconds) by blakem
in thread Time to seconds by argus

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (8)
    As of 2018-01-22 15:15 GMT
    Find Nodes?
      Voting Booth?
      How did you see in the new year?

      Results (235 votes). Check out past polls.