Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: Small examples of string eval

by graff (Chancellor)
on May 13, 2006 at 07:00 UTC ( #549192=note: print w/replies, xml ) Need Help??

in reply to Small examples of string eval

Teaching newbies to use eval? What a concept... (like playing with knives) -- well, it's sure to be valuable as a learning experience.

The one thing where eval is basically unavoidable is the use of a string variable within the "tr///" operator. It's simple, basic, and has a bunch of obvious uses in "real-life".

(update: forgot to include an example...)

my %charmap = ( a => 'B', c => 'D', ) # ... could come from a config f +ile my $old = join '', keys %charmap; my $new = join '', values %charmap; while (<>) { eval "tr{$old}{$new}"; if ( $@ ) { die "tr{$old}{$new} failed at line $.: $_"; } print; }

Replies are listed 'Best First'.
Re^2: Small examples of string eval
by ikegami (Pope) on May 13, 2006 at 07:29 UTC

    In my opinion, eval EXPR (as opposed to eval BLOCK) should be used only as a last resort. Your code is a prime example. It breaks as soon as someone places -, } or \ in %charmap. The following does not, and avoids eval EXPR entirely:

    while (<>) { s/(.)/exists $charmap{$1} ? $charmap{$1} : $1/seg; print; }

    (No, the /e is not the same as eval EXPR. There's no dynamic code generation. /ee, on the other hand, would be the same as eval EXPR.)

Re^2: Small examples of string eval
by spurperl (Priest) on May 13, 2006 at 07:28 UTC
    By newbies I don't mean people who've learned to program Pascal yesterday, but rather programmers from other languages who don't yet grok the concepts of runtime evaluation and code generation.

    Thanks for the example, but it's not what I'm looking for. Your example is very idiomatic to Perl, I'd say it's a patch on a deficiency, because I have no trouble imagining tr/// implemented in such a way that allows the trick without eval.

    What I'm looking for is more in the code-generation league. Generate some code with eval, on the fly, and call it when necessary.

      Major code generation is usually used when a language is inept (or not particularly adept) at doing something. Off the top of my head, I can think of three reasons why a language would be inept at doing something

      • 1) A language could be inept at doing something because it lacks the specialization that would make it adept. Querying databases (facilatitated by SQL), generating parsers (facilitated by BNF or similar) and generating object accessors would fall in this category.

      • 2) A language could be inept at doing something because it lacks the generalization that would make it adept. For example, working with Java Bytecode is more tedious than working in Java, so we do the work in Java and generate the Java Bytecode.

      • 3) A language could be inept at doing something because it is primarily text based and/or non-interactive, and a GUI and/or interactive solution would be more adept. For example, GUI builders are often used to build GUI code.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://549192]
[marto]: holli perfect
[ambrus]: Corion: four days a week? hmm
[Corion]: ambrus: I did that in 2012 and 2013, to try it out, and I found the available time to be worth more to me than the money
[Corion]: I've started that again this month (now forever instead of a limited time), and it already destresses me a lot
[holli]: There are Anti Vaxxer weeks at IKEA.
[Corion]: Of course, it's a 20% cut in the money I receive, while the work doesn't necessarily reduce at all, but my approach is to make the work take longer instead of fitting 5 days worth of work into 4
[holli]: 50% off all children coffins.
[Corion]: holli: Ooof :)
[hippo]: Cut should beless than 20% after tax, though. :-)

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2017-09-21 15:04 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (249 votes). Check out past polls.