Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Data::Dumper and printf

by kennethk (Abbot)
on Nov 20, 2012 at 22:52 UTC ( #1004814=note: print w/replies, xml ) Need Help??

in reply to Data::Dumper and printf

The issue is that printf expects a format string as the first argument (thus the 'f'). When you pass the array reference to Dumper, it returns a single string, which printf interprets as its format string. When you pass Dumper the array, it returns an array of strings and so printf thinks the first argument is the format, and thus only prints that.

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^2: Data::Dumper and printf
by Anonymous Monk on Nov 21, 2012 at 05:50 UTC

    And using printf like that forms a class of vulnerabilities known as format string vulnerabilities, in C and other languages that pass to the libc function directly. The interpreted languages generally don't and are safe from this. It causes a denial of service (program crash) most of the time.

Re^2: Data::Dumper and printf
by Trihedralguy (Pilgrim) on Nov 21, 2012 at 00:05 UTC
    That is what I was thinking, but I wasn't completely sure that this was the answer to the question. One of my peers asked me why this was working the way it was and I wasn't 100% sure what the correct answer was. Thank you for your explanation.
      one confusing thing is that printf and sprintf have different prototypes!

      DB<108> prototype "CORE::sprintf" => "\$\@" DB<109> prototype "CORE::printf" => undef

      so sprintf @array will evaluate the array in scalar context, such that now the number of elements will be interpreted as format string...

      Cheers Rolf

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1004814]
[1nickt]: thezip use Path::Tiny. @paths = path("/foo")-> children; (Excludes "." and ".." automatically.)
[1nickt]: (Also takes a compiled regexp as an arg to filter the list)
[1nickt]: For multiple dirs use iterator(): $it = path("/foo")-> iterator; while ( my $path = $it->() ) {...}
[1nickt]: (also skips . and .. automatically, and follows subdirs if you set <c\>recurse => 1</c>
[1nickt]: $it = path("/foo")-> iterator(recurse => 1 );

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (12)
As of 2017-03-28 18:45 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (339 votes). Check out past polls.