Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

I find that I often end up converting simple concatenation or interpolating print statements into (s)printfs. Especially as they get larger and more cumbersome I find the template-arg list approach more convenient and easier to manage. A useful trick is things like this:

printf "Some string %s in the file %s for %s", map { defined $_ ? $_ : 'undef' } $arg1,$arg2,$arg3;

Or if you are using ternaries in the output or whatever. Unlike BrowserUK and others I find printf a lot easier to manage. Also heres a cute trick (that I will eventually release as a CPAN module with more features)

package _XSprintf; use overload '""' => sub { shift->() }; package main; sub xsprintf { my $fmt =shift; my $bind=\@_; return bless sub { sprintf $fmt,@_ ? @_ : @$bind },'_XSprintf'; } my ($char,$ord,$bits)=('A'); my $diag=xsprintf("%s : %3d => '%8s'\n",$char,$ord,$bits); for ($char='A';length($char)==1;$char++) { $ord=ord $char; $bits=unpack 'B*',$char; print $diag; } __END__ A : 65 => '01000001' B : 66 => '01000010' C : 67 => '01000011' D : 68 => '01000100' E : 69 => '01000101' ...

Possibly not the most persuasive example, but on a number of occasions ive used this to greatly simplify diagnostics output inside of loops. Unfortunately it doesnt play nicely with for(LIST) because the iterator var is aliased, and accordingly it isn't in the bound argument array.


---
demerphq

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



In reply to Re: I don't use printf enough by demerphq
in thread I don't use printf enough by dreadpiratepeter

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (4)
    As of 2014-11-24 00:26 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (134 votes), past polls