in reply to restoring special variable defaults

You would usually say
{ local $#; ... do some stuff }
to have e.g. $# restored at the end of a block.

To force something to stringize as a number, first convert it with the 0+ operator and then use it in a string context:$x = "0.";  $x = 0+ $x;  print $x; Just kidding, there is no 0+ operator; but simply adding 0 as above will produce something with a numeric value and no string value; then using it in string context will cause the numeric value to be stringized, invoking $# (unless the newish preserve-integers-where possible logic kicks in).

By the way, $# is deprecated; use printf/sprintf directly instead; that will save you having to do the conversion: $x = "0."; printf "%.15e", $x and will avoid the aforementioned problem with perl bypassing floating point.

Replies are listed 'Best First'.
Re: Re: restoring special variable defaults
by Anonymous Monk on Feb 09, 2004 at 17:09 UTC
    In that case, can I do something like:

    printf "%5.3f",@yummy;

    to print all the elements of yummy in that format? My problem is that I have an array of variable length that I want to print out nicely, which is why $# seemed the only reasonable solution.

    BTW, the scope thing should work for me, I was just curious.

      No, you would need to do printf "%5.3f " x @yummy, @yummy or print join " ", map sprintf("%5.3f",$_), @yummy (both untested).