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

Re: Javascript to Perl = fail

by eyepopslikeamosquito (Chancellor)
on May 05, 2012 at 00:07 UTC ( #969004=note: print w/replies, xml ) Need Help??

in reply to Javascript to Perl = fail

Let's start by examining just one snippet of your Javascript code:

c = c.charCodeAt(0); c++; c = c.toString(); temp = c; c = String.fromCharCode(temp); line += c;
I hadn't written any Javascript until just now, but it seems to me (and correct me if I'm wrong) that the above code could be equivalently expressed in one line as:
line += String.fromCharCode(c.charCodeAt(0)+1);
which is equivalent to the Perl code:
$line .= chr(ord($c)+1);
assuming that $c contains just one character, as I believe it does from reading your code. Note that Perl uses a dot rather than a plus sign to concatenate strings. The Perl ord function returns the ord value (0-255) of a character while the Perl chr function does the reverse.

Given your lack of Perl experience, rather than attempting to convert the whole script all at once, I suggest you start by writing a number of very short Perl test programs, liberally sprinkled with print statements, so you can see what is going on. That should slowly build your confidence without overwhelming you. For example, you could start by running this little test program:

use strict; use warnings; my $line = "hello"; my $c = "A"; print "1. line='$line' c='$c'\n"; $line .= chr(ord($c)+1); print "2. line='$line' c='$c'\n";
which should print:
1. line='hello' c='A' 2. line='helloB' c='A'
Always start your Perl scripts with "use strict" and "use warnings" as shown above. Doing that will catch a lot of errors for you.

As for learning Perl, take a look at and Perl Tutorial Hub. Also, be sure to refer to the Perl online documentation. Good luck!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://969004]
[Corion]: Discipulus++ # being well is like being earnest
[marto]: hopefully next year I'll be in a position to go to Perly things in Europe :)
[Corion]: Maybe I'll use the Real Jekyll a bit to get a feel for how it actually works, and to get annoyed by it ;)
[Corion]: marto: That would be cool :) There is no kids care at Perly events unfortunately, so it's not entirely family friendly
[Corion]: I think I should define a set of (say) five projects through which I rotate from time to time, just to prevent myself from being bored/annoyed by the problems they get stuck in ;)
[marto]: Corion, not bringing the kids would make it ideal :P
[marto]: a suggestion, have one project in the list that isn't programming/perl related
[Corion]: marto: Yeah, things are more parent-friendly the way they are ;))
[marto]: some hardware/makey thing
[Corion]: marto: Ah, I'm currently drifting to that again, over"painting" (well with Inkscape) one of my photographs and thinking about vectorizing some other images ...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2017-07-27 07:41 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (404 votes). Check out past polls.