fionbarr has asked for the wisdom of the Perl Monks concerning the following question:

somehow I got in the habit of declaring a scalar variable as:
my $var = q{};
Is there any practical value of this rather than:
my $var;

Replies are listed 'Best First'.
Re: declare and init a scalar
by BrowserUk (Patriarch) on Jan 22, 2015 at 20:33 UTC

    There is only one situation when I consciously assign the null string to a scalar:

    { my $vec; vec( $vec, 0, 1 ) = 1; };; Use of uninitialized value $vec in vec at (eval 9) line 1, <STDIN> lin +e 1. Use of uninitialized value $vec in scalar assignment at (eval 9) line +1, <STDIN> line 1. { my $vec = ''; vec( $vec, 0, 1 ) = 1; };;

    A somewhat analogous situation arises with substr:

    { my $s; substr( $s, 0, 1, 'fred' ); };; Use of uninitialized value $s in substr at (eval 13) line 1, <STDIN> l +ine 5. { my $s = ''; substr( $s, 0, 1, 'fred' ); };;

    But that is less useful because it only silences the warning if there is an overlap between the range being overwritten and that that already exists:

    { my $s = ''; substr( $s, 1, 1, 'fred' ); print $s; };; substr outside of string at (eval 16) line 1, <STDIN> line 8. { my $s = ' '; substr( $s, 1, 1, 'fred' ); print $s; };; fred

    But beyond those few special cases, it's pretty much pointless typing.

    (I also think that using q{} instead of '' is a waste of effort; and a mild form of obfuscation. PBP strikes again.)


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

      I've got into the habit of using q{ ... } and qq{ ... } because I work in both Unix/Linux and MS Windows environments. They mean I don't have to think about the quoting conventions of the o/s when writing one-liners, which in practice tend to be several-liners.

      Update: Corrected typo above, s/it //, and to clarify, I use quoting constructs everywhere, scripts and one-liners.

      Cheers,

      JohnGG

        I've got into the habit of using q{ ... } and qq{ ... } because it I work in both Unix/Linux and MS Windows environments. They mean I don't have to think about the quoting conventions of the o/s when writing one-liners,

        That is the most (only) cogent argument I seen for this practice.

        However, given that you have to think about whether to use -E' ... ' or -E" ... " in the first place, within that, choosing to use q[ ... ] or qq[ ... ] when appropriate doesn't seem so hard.

        And limiting that use to just one-liners, less so.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
        In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
      I also think that using q{} instead of '' is a waste of effort; and a mild form of obfuscation. PBP strikes again.

      BrowserUk++

      The sooner we can get rid of this (and associated) memes on PM, the better.

        The ones I really like [NOT!] :) are:

        • my $var = q'';
        • my $var = qq"";

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
        In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
Re: declare and init a scalar
by Your Mother (Archbishop) on Jan 22, 2015 at 23:36 UTC

    It’s a free world so do as you like but seeing q{} and qw() all over when '' and () will do, annoys me. Parsimony! Think of the time you subtract from your life typing the extra characters. Will no one think of the children?! Though annoying me might be amusing to some so… live it up! And before you ask me how much of life I gave up to write this, LanX! Nothing makes me happier than hearing the sound of my keyboard.

    Did I get the part?

      > And before you ask me how much of life I gave up to write this, LanX!

      Moi?

      How did I get involved here?

      Do people already start to hear my voice when they post sub-logical things?

      Really?

      Cool! >D

      Cheers Rolf

      PS: Je suis Charlie!

        Not sub-logical as much as SubGenius. Take that, subspace!

Re: declare and init a scalar
by LanX (Saint) on Jan 22, 2015 at 20:16 UTC
    They are different, the first is an empty string (defined) the other is just undef. (not defined)

    I'd say in general the first one is humbug ...

    Cheers Rolf

    PS: Je suis Charlie!

      I should have said that I use 'q{}' on a string.
Re: declare and init a scalar
by Anonymous Monk on Jan 22, 2015 at 21:30 UTC
    I think it's actually a bad thing to do. It reminds me of people who declare a ton of global variables at the top of their script just to make strict shut up. It completely defeats the purpose of strict 'vars'. Likewise, my $var = '' is basically the same as no warnings 'uninitialized'

      Sometimes, for the sake of readability1, assigning '' to a variable is better that leaving it undefined, or assigning undef.

      That said, it is possible for assigning '' to a variable hides errors that might otherwise be detected by the warning.

      As long as the coder is aware of the trade-offs, the coder can choose the alternative that best fits a given situation.

      ---

      1 Just adding a comment explaining what you are doing and why can actually confuse the reader:

      my $buf; # leave value as undefined so use before a real value is avai +lable will result in a warning message. ...; while (<$input>) { ...; $buf .= $newletter; # not an error because appending to an undef v +alue is treated the same as appending to an empty string }

      While the above is certainly valid Perl code (and doesn't produce a warning), there are a lot of code reviewers who will reject this, citing it as being confusing.

      And, often, the same reviewers will reject:

      if (defined $buf) { $buf .= $newletter; } else { $buf = $newletter; }

      as being too complex.

      But:

      my $buf = ''; ...; while (<$input>) { ...; $buf .= $newletter; }

      is clear about what is being done and comfortable to the reviewers.

      Sometimes debugging a weird bug is easier than arguing with the reviewers.

        Sometimes debugging a weird bug is easier than arguing with the reviewers.

        I agree! But...debugging is about finding errors; if you arbitrarily disable the diagnostics that will tell where the error originates you make life much harder.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
        In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
      so how do you initialize a string variable?
        my $var = 'some string';
Re: declare and init a scalar
by Laurent_R (Canon) on Jan 23, 2015 at 07:25 UTC
    I usually don't initialize variables when I cannot give them a usefull value. But sometimes you have to. For example a procedure to remove duplicates in a file sorted on the duplicate key.
    my $old_key = ""; while (<>) { my $key = (split /;/, $_)[2] next if $key eq $old_key; # ... }
    I need to initialize $old_key to prevent a warning when reading the first line of the file.
    Je suis Charlie.