in reply to declare and init a scalar

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

Replies are listed 'Best First'.
Re^2: declare and init a scalar
by johngg (Canon) on Jan 22, 2015 at 22:35 UTC

    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
        but then the noobers who get the oneliners don't understand '' "" of their own shell ... easiest just to change one pair of quotes rather than many
        Too bad that perl -Eq{...} doesn't work - though probably for different reasons on different OSes :-)
Re^2: declare and init a scalar
by Not_a_Number (Prior) on Jan 22, 2015 at 21:39 UTC
    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