Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Data::Dumper and printf

by Trihedralguy (Pilgrim)
on Nov 20, 2012 at 22:41 UTC ( #1004812=perlquestion: print w/replies, xml ) Need Help??
Trihedralguy has asked for the wisdom of the Perl Monks concerning the following question:

I was wondering why the interaction between Data::Dumper and printf is different between an array and an array as ref. See Example:

#!/usr/local/bin/perl use strict; use Data::Dumper; my @array = (1, 2, 3, 4); printf "As Array:\n"; printf Dumper(@array); printf "As Reference to Array:\n"; printf Dumper(\@array);

"print" doesnt seem to have any problems, "printf" doesnt seem to return the proper results...But I want to know why!

Replies are listed 'Best First'.
Re: Data::Dumper and printf
by kennethk (Abbot) on Nov 20, 2012 at 22:52 UTC
    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.

      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.

      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

Re: Data::Dumper and printf
by ww (Archbishop) on Nov 20, 2012 at 22:51 UTC
    For starters, printf is NOT a slide-in substitute for print.

    C:\>perldoc -f printf printf FILEHANDLE FORMAT, LIST printf FILEHANDLE printf FORMAT, LIST printf Equivalent to "print FILEHANDLE sprintf(FORMAT, LIST)", ex +cept that $\ (the output record separator) is not appended. The + first argument of the list will be interpreted as the "printf" f +ormat. See "sprintf" for an explanation of the format argument. I +f you omit the LIST, $_ is used; to use FILEHANDLE without a LIS +T, you must use a real filehandle like "FH", not an indirect one +like $fh. If "use locale" is in effect and POSIX::setlocale() h +as been called, the character used for the decimal separator +in formatted floating-point numbers is affected by the LC_NUM +ERIC locale setting. See perllocale and POSIX. Don't fall into the trap of using a "printf" when a simple "print" would do. The "print" is more efficient and less e +rror prone.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1004812]
Approved by ww
Front-paged by 2teez
LanX needs a smartwatch for his boss ...
[LanX]: darn, I lost a post in preview

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (12)
As of 2017-10-20 14:19 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (263 votes). Check out past polls.