Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Uer Perl Variable in file

by choroba (Chancellor)
on Aug 06, 2012 at 13:57 UTC ( #985726=note: print w/replies, xml ) Need Help??


in reply to Uer Perl Variable in txt file

Using a hash instead of variables is safer (no eval needed). Something like this:
#!/usr/bin/perl use warnings; use strict; my %replace = (name => 'Joe', fun => 'a game'); while (<DATA>) { for my $key (keys %replace) { s/\$$key/$replace{$key}/g; } print; } __DATA__ Hullo $name, are you ready for $fun?
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: Uer Perl Variable in file
by moritz (Cardinal) on Aug 06, 2012 at 13:59 UTC
      my $search_for = join '|', map quotemeta, keys %replace; ... s/\$($search_for)/$replace{$1}/g;
      Additionally:
      when doing things like this and you have two hash keys "foo" and "foobar", either use \b in the regex if possible, or sort the hash keys for length before joining. otherwise $foobar could be replaced with the value of $replace{foo}.
      s/\$($search_for)\b/$replace{$1}/g;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://985726]
help
Chatterbox?
[Discipulus]: ah! so you mean if i need a static image i can use Twig, if on the fly better css selector and mody attrs (jquery?), rigth?
[LanX]: not sure what you guys are taking about, but a DOM manipulation in JS shouldn't be difficult
[Corion]: Discipulus: Yeah, that's what I would try (but so far, I can't even change the colors manually in the SVG, which is why I gave up ;) )
Discipulus bad sign..
[Discipulus]: italian doc says to modify somehow the initial SVG before using it with D3.js
[Discipulus]: LanX the fact is that my js ability is even poorer than my Perl one..
[LanX]: oh its negative? ;P
[Discipulus]: my warmest comprehension karlgoethebier ;=(

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (13)
As of 2017-03-23 09:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (285 votes). Check out past polls.