Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^3: using map

by BrowserUk (Pope)
on May 06, 2012 at 05:56 UTC ( #969115=note: print w/ replies, xml ) Need Help??


in reply to Re^2: using map
in thread using map

The correct format for printf is printf FORMAT_STRING, LIST so that should be:

Any ordinary (non-specifier) characters in the format string are output verbatim.

If the format contains no %specifiers, then just what is in the template gets output.

As -l doesn't affect printf, it makes it a convenient way of printing output that does not get an automatic \n appended for those rare occasions when that is required.

It is convenient, safe and a lot easier than having to add \n to every darn print statement.

Of course, say ought to be a simple alternative to -l, but a) they made it so darn inconvenient to use; b) its a pain in the neck having to switch back to print any time you need to test something under a pre-5.10 version.


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".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?


Comment on Re^3: using map
Re^4: using map
by tobyink (Abbot) on May 06, 2012 at 06:37 UTC

    If the format contains no %specifiers, then just what is in the template gets output.

    And so printf $_ acts exactly like print $_.

    Very clever... until $_ happens to contain something that looks like a printf specifier and it comes back to bite you.

    Of course, say ought to be a simple alternative to -l, but a) they made it so darn inconvenient to use

    For one-liners, say is actually very convenient to use. Just replace "-e" with "-E".

    perl -E"say q(Hello world)"
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      And so printf $_ acts exactly like print $_.

      Not exactly.    printf still has to parse $_ looking for format specifiers.    And print is affected by the $\ and $, variables while printf is not.

        That's what I just said, isn't it?

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      until $_ happens to contain something that looks like a printf specifier

      And the millions of microwave ovens in family kitchens across the globe can act exactly like small bombs with nothing more than the addition of some plain water! But nobody suggests we should ban the use of either.

      The answer is quite simple. Don't use it where that is a possibility.

      For one-liners, say is actually very convenient to use.

      a) Yes, I know. b) Nobody was talking about one-liners.


      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".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

Re^4: using map
by Anonymous Monk on May 06, 2012 at 08:17 UTC

    You're correct in this case (with the warnings you added afterwards), yes, but leaving out the "%s" was the very reason of thousands of format string vulnerabilities everywhere. And even though that class of vulnerabilities don't affect Perl, it would be correct and a good practice to include it anyway, which is what jwkrahn was trying to point out.

    I do not think that you pointing out that the extra care is unnecessary in this very case was very beneficial to the whole discussion.

      Do you not see the contradiction in your first acknowledging this:

      And even though that class of vulnerabilities don't affect Perl,

      And then going on to add this: it would be correct and a good practice to include it anyway, ?

      In C and similar languages where that vulnerability exists, such advise is warranted and good practice.

      But blindly transferring that to Perl is like trying to transfer the good practice of wearing seat belts in cars, to motorcycles. It simply doesn't work the same way.

      I do not think that you pointing out that the extra care is unnecessary in this very case was very beneficial to the whole discussion.

      Far better to know (and point out) when such practices are applicable -- which AnomalousMonk has more than demonstrated that he does -- than to blindly apply it when it is inapplicable.


      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".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2014-09-30 22:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (385 votes), past polls