Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
hello, yes i'm still a beginner, and yesterday i come up with something never occured to me: the interpolation of a string was not done as expected (sub class of '0 type problem': you expect something wrong).

Why this is not working?
#!perl use strict; use warnings; $|++; my ($comp,$first,@sec); $comp = "descr is ".(defined $first ? q{$first} : 'null').' and list i +s '.( @sec ? q{join ' ', @sec} : 'empty'); print "Oringinal:$comp\n"; &modify_first('UNO'); &modify_second (1);&modify_second (2); &modify_first ('DUE'); &reset_second; sub modify_first{$first = shift; print "$comp\n";} sub modify_second {push @sec, shift; print "$comp\n";} sub reset_second {@sec=qw(); print "$comp\n";} __OUTPUT__ Oringinal:descr is null and list is empty descr is null and list is empty descr is null and list is empty descr is null and list is empty descr is null and list is empty descr is null and list is empty
I have found two approach at this problem: Re: Dreaming of Post Interpolation that propose an quoting-evaluating workaround (not suitable for thinks like  @sec ? @sec : 'empty')
my $text = q{ Dear $person, I know that this text is $adjective. But I wish it could be... }; my $person = 'Mom'; my $adjective = 'not interpolated'; print eval "qq{$text}"; __OUTPUT__ Dear Mom, I know that this text is not interpolated. But I wish it could be...

Another monks come out with a sub solution:
my $n = sub {1}; my $m = sub {&$n*2}; my $o = sub {&$m*2}; my $s = sub {&$n." ".&$m." ".&$o."\n"}; print &$s; $n = sub {2}; print &$s; __OUTPUT__ 1 2 4 2 4 8
The Perl faq propose:
How can I expand variables in text strings? Let's assume that you have a string that contains placeholder vari +ables. $text = 'this has a $foo in it and a $bar'; You can use a substitution with a double evaluation. The first /e +turns $1 into $foo, and the second /e turns $foo into its value. You may + want to wrap this in an "eval": if you try to get the value of an undec +lared variable while running under "use strict", you get a fatal error. eval { $text =~ s/(\$\w+)/$1/eeg }; die if $@; It's probably better in the general case to treat those variables +as entries in some special hash. For example: %user_defs = ( foo => 23, bar => 19, ); $text =~ s/\$(\w+)/$user_defs{$1}/g;

And finally chromatic propose a real magic to create a red apple:
my $color = "red"; my $fruit = "apple"; my $name = "chromatic"; my $string = 'Hi, my name is $name. Please hand me a $color $fruit.'; print ">>$string<<\n"; # demonstrate what we have my $s2; eval "\$s2 = qq/$string/"; # the real magic print "->$s2<-\n"; # demonstrate the result __OUTPUT__ >>Hi, my name is $name. Please hand me a $color $fruit.<< ->Hi, my name is chromatic. Please hand me a red apple.<-

OK. now i know. But there is something newer then years 2000 can I profit? A cleaner solution?

L*
there are no rules, there are no thumbs..

In reply to Interpolation: when it occurs? another beginner question.. by Discipulus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-03-28 19:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found