Re: use strict; use warnings;

Discussion in 'Perl Misc' started by Rainer Weikusat, Feb 27, 2014.

  1. Martijn Lievaart <> writes:
    > On Mon, 24 Feb 2014 11:50:32 -0600, Marek Novotny wrote:
    >> On Mon, 24 Feb 2014 12:45:34 +0100, Janek Schleicher wrote:
    >>> PS: You problably don't know yet what this qw/../ operator means,
    >>> here it's just a short cut for qw/H D C S/ is the same as ("H", "D",
    >>> "C", "S").

    >> That they did cover. For whatever reason I like @array = qw( elem1 elem2
    >> ...) but I see everyone here has adopted qw/elem1 elem2/. I could easily
    >> adapt to this instead. For whatever reason the first is easier for to
    >> remember and think of.

    > qw/.../ is idiomatic, but it is not a very strong idiom afaik. Use
    > whatever you like best.
    > With qq/.../, m/.../, s/.../.../ etc sometimes choosing a different
    > character may be helpful, f.i. compare
    > s/\/.*\/([^\/])+\/.*/$1/;
    > with
    > s!/.*/([^/])+/.*!$1!;
    > Look up "leaning toothstick syndrome".


    Other considerations: This feature is specific to Perl. OTOH, the
    /-using syntax is used by other tools to, meaning, one has to be able to
    read and write it, anyway. Also, no particular 'separation character'
    is universally useful without quoting. 'Hand-optimizing'
    regex-constructs in order to avoid occasional quoting can be considered
    a waste of time. Lastly, individualized seperators introduce noise into
    source code (in the sense that the set of 'special characters' is
    potentially larger and its members aren't known until all of the code
    has been read).
    Rainer Weikusat, Feb 27, 2014
    1. Advertisements

  2. Tim McDaniel

    Tim McDaniel Guest

    In article <>,
    Ben Morrow <> wrote:
    >[Please start a new thread for a new topic.]
    >Quoth Marek Novotny <>:
    >> my @startingdeck = ("A H","2 H","3 H","4 H","5 H","6 H","7 H","8 H",
    >> "9 H","10 H","J H","Q H","K H",
    >> "A D","2 D","3 D","4 D","5 D","6 D","7 D","8 D",
    >> "9 D","10 D","J D","Q D","K D",
    >> "A C","2 C","3 C","4 C","5 C","6 C","7 C","8 C",
    >> "9 C","10 C","J C","Q C","K C",
    >> "A S","2 S","3 S","4 S","5 S","6 S","7 S","8 S",
    >> "9 S","10 S","J S","Q S","K S");
    >> my $i = 0; my @randomdeck;
    >> while ($i < 51){
    >> $randomdeck[$i] = shift(@startingdeck); $i++;
    >> $randomdeck[$i] = pop(@startingdeck); $i++;
    >> }

    >This loop continues until @startingdeck is empty. There happen to be 52
    >entries at the moment, but in general it's not a good idea to rely on
    >'magic numbers' like that:
    > while (@startingdeck) {
    > push @randomdeck, shift @startingdeck;
    > push @randomdeck, pop @startingdeck;
    > }

    A more general programming note:

    If, due to error, @startingdeck starts with an odd number of elements,
    then the pop will return undef on the last iteration. That may be
    sufficient -- maybe the undef will cause a visible error in later

    But I don't like to do overruns (I had to look up what pop does for an
    empty array).

    while (@startingdeck >= 2) {
    push @randomdeck, shift @startingdeck;
    push @randomdeck, pop @startingdeck;

    avoids an overrun, but it has the disadvantage of silently ignoring
    an odd element.

    I like bullet-resistant code, so I might do an assertion check of some
    sort beforehand. Or generate @startingdeck programmatically with a
    two-level foreach.

    Tim McDaniel,
    Tim McDaniel, Mar 1, 2014
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marius Gavrilescu

    Re: use strict; use warnings;

    Marius Gavrilescu, Feb 24, 2014, in forum: Perl Misc
    Mar 11, 2014
  2. Kaz Kylheku

    Re: use strict; use warnings;

    Kaz Kylheku, Feb 24, 2014, in forum: Perl Misc
    Kaz Kylheku
    Feb 24, 2014
  3. George Mpouras

    Re: use strict; use warnings;

    George Mpouras, Feb 24, 2014, in forum: Perl Misc
    George Mpouras
    Feb 24, 2014
  4. Mart van de Wege

    Re: use strict; use warnings;

    Mart van de Wege, Feb 24, 2014, in forum: Perl Misc
    Mart van de Wege
    Feb 25, 2014
  5. Justin C

    Re: use strict; use warnings;

    Justin C, Feb 24, 2014, in forum: Perl Misc
    Justin C
    Feb 25, 2014

Share This Page