in reply to Re^4: printing a scalar as an array
in thread printing a scalar as an array

It's not the overhead. It's a bad habit. And eventually, it will bity you. People that tend to pointlessly quote scalars (and you see it happening not just with print, but with other function calls as well), will also quote them when the scalars happen to be refs. Oops. That's to reason to "nag" about use of quotes.

As for not using $\ = "\n", to do it right, you'd have to do it each time before a print, and do it localized, or some other part of the program might act unexpectedly.

If you don't want to type the "\n", use Perl6::Say (or whatever it is called), or use something like:

sub printnl {print @_, "\n"}
(although you won't be able to print to a filehandle that way).

Replies are listed 'Best First'.
Re^6: printing a scalar as an array
by RazorbladeBidet (Friar) on Feb 23, 2005 at 14:33 UTC
    Hmm...

    use strict; my $a = 1; my $b = \$a; print $b, "\n"; print "$b\n";


    both print the same thing - why is that bad? I'm really just curious.

    as for $\="\n", I agree, localized is the way to go, but you don't have to do it before each print (unless, of course, your localized version goes out of scope).

    but I still stand by "use strict" being more important than removing unnecessary double-quotes

    I hope this argument isn't getting too petty :)
    --------------
    It's sad that a family can be torn apart by such a such a simple thing as a pack of wild dogs
      As I said, people tend to quote scalar not just with print. Consider this:
      #!/usr/bin/perl use strict; use warnings; sub gimme_first ($) {$_[0]->[0]} my $a = [1, 2, 3]; print gimme_first $a, "\n"; print gimme_first "$a", "\n";

      as for $\="\n", I agree, localized is the way to go, but you don't have to do it before each print (unless, of course, your localized version goes out of scope).
      Well, that would be wrong. In the following code:
      { local $\ = "\n"; print "whatever"; imported_subroutine (); print "something else"; }
      the problem is that $\ doesn't go out of scope. Even when imported_subroutine is run, the modified $\ is still in scope.

      And I still stand by that if you're going to nag about a "use strict" missing from a code fragment, I will nag about anything you write.

      (Trust me to start a row)

      IMHO you're both right. Using strict solves so many problems that many questions would never be asked if the poster was using strict in the first place. Witness the number of SOPWs that get half a dozen answers saying nothing but

      use strict; use warnings

      Its probably always worth putting it on code fragments that perform a complete task such as this.

      As to the quoted eval, on this occasion that was left over from a previous attempt to solve the problem, but generally speaking its a bad habit of mine that I need to get out of. Its a bit like one of those words you habitually mispell even though if you stop to think, you know perfectly well what it should be.

      VGhpcyBtZXNzYWdlIGludGVudGlvbmFsbHkgcG9pbnRsZXNz
      Next, you two are going to start arguing cuddling elses or vi vs. emacs or any number of other equally pointless things. I think you two are in violent agreement.

      Being right, does not endow the right to be rude; politeness costs nothing.
      Being unknowing, is not the same as being stupid.
      Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
      Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

        I could have sworn that said cudling elves.

        Well that's what I first read. I have been staring at this VDU far too long today. I got a 1000 lines of spagetti code to debug. Has it's own home grown DB interface, no warnings, no strict and bits of SQL splattered all over the shop. A nice block at the top says ## Global Constants ## half of them are then redefined later at random points

        I have learnt a valuable lesson today: Never say you have too little work to do.

        Cheers,
        R.

        Pereant, qui ante nos nostra dixerunt!