Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: I don't use printf enough

by BrowserUk (Pope)
on Oct 22, 2003 at 17:40 UTC ( #301295=note: print w/ replies, xml ) Need Help??


in reply to I don't use printf enough

Funnily enough, I think for many cases I would write that as

print 'text text ', $obj->method1(), ' more text ', $obj->method2(), ' blah blah ', $obj->method3();

I would only use printf if I needed to apply special formatting to the method returns.

My main reasons for this choice are that

  1. It is easy to extend.

    Adding 'and more text', $obj->method2.5 is easy, and commenting out one part likewise.

  2. As the length of the format string increases, it becomes unweildy and need to be split across 2 or 3 lines anyway.
  3. I find this formatting easier to see what text applies to which method.

The absence of a newline, is because i always have -l enabled.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Hooray!


Comment on Re: I don't use printf enough
Select or Download Code
Re2 (-l): I don't use printf enough
by bbfu (Curate) on Oct 23, 2003 at 04:03 UTC

    The absence of a newline, is because i always have -l enabled.

    I like -l but wish that there was a way to selectively counter-act it, as I occasionally (but not often) want to print a newline-less line. How do you handle these situations?

    bbfu
    Black flowers blossom
    Fearless on my breath

      That's when I use printf:)

      With -L enabled, print 'Prompt: '; adds the newline but printf 'Prompt: '; $resp = <STDIN>; doesn't. The distinction is very useful.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      Hooray!


      I like -l but wish that there was a way to selectively counter-act it

      Here is one way:

      #!/usr/bin/perl -wl print "aaa"; { local $\; print "bbb"; } print "bbb"; print "ccc"; __END__ Prints: aaa bbbbbb ccc

      --
      John.

      I like -l but wish that there was a way to selectively counter-act it, as I occasionally (but not often) want to print a newline-less line. How do you handle these situations?
      Localize $\.
      $\ = "\n"; # as perl -l print "before"; { local $\; print "continued"; } print "after"; print "heh!";
      which prints
      before
      continuedafter
      heh!
      
      In fact, you can set $\ to anything, and with local, it's safe — or at least, as safe as using $\.

      IMO, every module that uses print in a hidden fashion, should use

      local $\;
      and not rely on $\ being empty at the time its functions are called.

      Note that printf ignores the settings of $\.

        every module that uses print in a hidden fashion, should use local $\;

        No way. Anybody who sets $\ should set it in a localized block when they do so. -l is there for one liners not real code. I _refuse_ to try to work around other people setting $\ stupidly. And for that matter simply localizing $\ in the module will not work. You'd have to do it in every single subroutine. And that means that every single subroutine that uses print is going to do an unecessary piece of work just to workaround somebody setting $\ stupidly. In essence I refuse to pay the price for somebody elses foolishness. And I dont see it as being reasonable to expect people to do so. Going down that road means that eventually somebody is going to argue that every single subroutine should localize all the global variables because of similar reasons.

        Blech.


        ---
        demerphq

          First they ignore you, then they laugh at you, then they fight you, then you win.
          -- Gandhi


Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://301295]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2014-10-21 02:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (95 votes), past polls