Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: How to use sprintf %n formatting pattern

by GrandFather (Saint)
on Jun 09, 2022 at 12:50 UTC ( [id://11144553]=note: print w/replies, xml ) Need Help??


in reply to Re: How to use sprintf %n formatting pattern
in thread How to use sprintf %n formatting pattern

When I run your code I get:

Use of uninitialized value $max_width in subtraction (-) at delme.pl l +ine 26. foo0123456789

which is about what I'd have expected due to the arguments to printf being evaluated before printf is called. What am I missing here?

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Replies are listed 'Best First'.
Re^3: How to use sprintf %n formatting pattern
by hv (Prior) on Jun 09, 2022 at 13:54 UTC

    which is about what I'd have expected due to the arguments to printf being evaluated before printf is called

    I'm still not sure what you mean by that.

    If you call foo($a + $b), perl will calculate $a + $b putting the result in a temporary scalar variable, and put that temporary scalar variable on the stack to call foo(). If you call foo($a), perl will put the scalar variable $a on the stack, and call foo().

    In the case of printf("%s%n%.*s\n", $prefix, $stored_width, $limit, $postfix), we get (effectively) references to each of those variables on the stack (at the C level, they are just SV* pointers) - you know they have to be references, since printf needs to be able to write to $stored_width. At the point the printf implementation needs the next argument as a number, it'll ask the relevant scalar what its numeric value is; if it needs a string, it'll ask for that instead, etc.

    Is that making it any clearer?

      Yes, that too came clear in the shower this morning. I think my issue may be related to usually copying arguments into local variables as a first thing so I tend to forget about the aliasing that goes on. You might say I'm 'closing' in on understanding now. :-)

      Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Re^3: How to use sprintf %n formatting pattern
by hv (Prior) on Jun 09, 2022 at 13:41 UTC

    If $max_width is undefined, you probably ran it without providing a command-line argument. Try setting $max_width to something.

      Yup, figured that out in the shower this morning. I understand egg on the face is good for the complexion. Not sure why a shower is at least as effective as two coffees. Unfortunately the effect seems only to last for the duration of the shower.

      Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Re^3: How to use sprintf %n formatting pattern
by hippo (Bishop) on Jun 09, 2022 at 13:24 UTC

    It works fine for me: This is perl 5, version 34, subversion 0 (v5.34.0) built for x86_64-linux-thread-multi.


    🦛

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-26 05:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found