[Omaha.pm] Passing arguments to "sprintf"...

Daniel Linder dan at linder.org
Wed Feb 9 11:27:45 PST 2005


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Here is a quick one...hopefully.

I have the following program:
- --- begin code ---
#!/usr/bin/perl
use strict;
my $testcase;
sub LogIt {
  my @LogString = @_;
  open (LOGFILE, ">>/tmp/mylogfile");
  printf (LOGFILE "This is test case %d\n", $testcase);
  if (1 == $testcase) {
    # This section works just fine!
    printf (LOGFILE @LogString);         #line in question
  } else {
    # This section prints "2" for the
    my $myline = sprintf (@LogString);
    print (LOGFILE $myline);
  }
  printf (LOGFILE "\n");
  close (LOGFILE);
}

$testcase = 1;
LogIt ("one: %s\n", "This works!");
my $string = "This works too!";
LogIt ("two: %s\n", $string);

$testcase = 2;
LogIt ("one: %s\n", "This fails!");
my $string = "This fails too!";
LogIt ("two: %s\n", $string);
- --- end code ---

The contents of the log file are here:
- --- begin logfile ---
This is test case 1
one: This works!

This is test case 1
two: This works too!

This is test case 2
2
This is test case 2
2
- --- end logfile ---

Why does the changing from "sprintf" to "printf" seem to make such a huge
difference?  Or am I missing something?

Dan

- - - - -
"I do not fear computer,
I fear the lack of them."
 -- Isaac Asimov
GPG fingerprint:9EE8 ABAE 10D3 0B55 C536  E17A 3620 4DCA A533 19BF

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFCCmQxNiBNyqUzGb8RAm2hAJ4hKT9HsUXG77Jpxvg/15EzmmwR7gCdFj5T
JGHFY5+XJ9tpUT9XScVBdWo=
=J6bD
-----END PGP SIGNATURE-----


More information about the Omaha-pm mailing list