Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Modify a variable within curly braces (ASCII Sequence))

by westrock2000 (Beadle)
on Jun 04, 2010 at 04:13 UTC ( [id://843055]=perlquestion: print w/replies, xml ) Need Help??

westrock2000 has asked for the wisdom of the Perl Monks concerning the following question:

I am using the ASCII Controll Sequences to move the cursor on the screen (my site does not have Term::ANSIScreen installed and so I must make do with what we have). The way it works is like this.
print ("\033[2;5H");
Which would position the cursor to down 2 rows and over 5 columns from top left. I can use variables by doing the following
$columns = 5; print ("\033[2;${columns}H");
But my problem comes from when I want to increment the variable without using an intermediate step. Such as;
$a = 5; print ("\033[3;${a}HStart Here $a"); print ("\033[4;${$a+=10}HEnd Here $a\n");
What prints out is
Start Here 4 End Here 14
So Perl is updating $a, but it doesn't seem to be passing it to the Escape Sequence. Since "End" starts against the wall that means it getting no value. I know I can do $a+=10; inbetween the print statements, but I was wondering if I can shorthand it even more.

Replies are listed 'Best First'.
Re: Modify a variable within curly braces (ASCII Sequence))
by BrowserUk (Patriarch) on Jun 04, 2010 at 04:26 UTC

    Try print ("\033[4;${ \( $a+=10 ) }HEnd Here $a\n");. Enable warnings to see why.

      Ahh! Thats what I tried at first, but I forgot to put the backslash within the curly braces, so it didn't do anything....but logically its what I thought would work.
Re: Modify a variable within curly braces (ASCII Sequence))
by NetWallah (Canon) on Jun 04, 2010 at 04:31 UTC
    The semantics you are attempting will not do what you expect, unless you use the module "Interpolation";

    In your code, ";${$a+=10}" evaluates the $a part, replacing it by it's value, 5, but , by default, string interpolation does not do math.

    The other issue is that you are attempting to de-reference (because you use ${...}) something that is not a reference - it is the string "5+=10".

    update Oops: BrowserUk has presented the "dereferencing a block" loophole, that allows interpolation, which, apparently you were familiar with. Ignore this post.

         Syntactic sugar causes cancer of the semicolon.        --Alan Perlis

Re: Modify a variable within curly braces (ASCII Sequence))
by ambrus (Abbot) on Jun 04, 2010 at 11:18 UTC

    Whatever is the problem with print("\e[4;" . ($a += 10) . "HEnd Here $a\n"); ?

      Whatever is the problem with  print("\e[4;" . ($a += 10) . "HEnd Here $a\n"); ?

      Indeed, and whatever would be the problem with
          $a += 10;
          print("\e[4;${a}HEnd Here $a\n");

      I have to say that, while it will work (and other, similar solutions could be found), BrowserUk's solution falls into the category of Stupid Interpolation Tricks in the context of the problem of the OP. I hope westrock2000's original question was prompted by pure curiosity and not by any inclination to actual use, for if westrock2000 tries to use this or a similar trick in production code, he or she had better be sure none of the maintainers of said code know where he or she lives!

        BrowserUk's solution falls into the category of Stupid Interpolation Tricks in the context of the problem of the OP.

        Your opinion noted and dismissed as irrelevant.

        Personally, I'd do it this way: printf ("\033[4;%dHEnd Here $a\n", $a+=10 );, but that doesn't answer the OPs question.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        Wait, there's also print "\e[4;${a}H\e[10CEnd here\n"; provided you don't need the value of $a later. (I assume you don't, and the OP only increments it in place only because that way he can interpolate simply $a.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2025-02-18 05:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found