Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Interpolation: when it occurs? another beginner question..

by tobyink (Abbot)
on Feb 25, 2013 at 13:33 UTC ( #1020521=note: print w/ replies, xml ) Need Help??


in reply to Interpolation: when it occurs? another beginner question..

These interpolate:

  • "$var"
  • qq{$var}

These do not:

  • '$var'
  • q{$var}

TL;DR: if you want interpolation, don't use single quotes!

OK, I see what you're asking. You want delayed evaluation - I'll update with a solution to that momentarily!

Update: OK, how about this...

use v5.10; use strict; use warnings; sub delayed (&) { package delayed; use overload q[""] => sub { $_[0]->() }, fallback => 1; bless shift; } my $string = delayed {"Look $::person, no $::thing!"}; $::person = "Ma"; $::thing = "stringy eval"; say $string;

Update II (about a week later): I've released String::Interpolate::Delayed using some of the ideas explored in this thread.

package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name


Comment on Re: Interpolation: when it occurs? another beginner question..
Select or Download Code
Re^2: Interpolation: when it occurs? another beginner question..
by karlgoethebier (Deacon) on Feb 25, 2013 at 15:26 UTC

    This is really cool! I think i'll put it on the wall in my office - to remember. Best regards, Karl

    P.S.: I just bothered myself about this with eval and function interpolation :-(

    «The Crux of the Biscuit is the Apostrophe»

      For what it's worth, it does also work with lexical variables, but you need to declare them up-front...

      use v5.10; use strict; use warnings; sub delayed (&) { package delayed; use overload q[""] => sub { $_[0]->() }, fallback => 1; bless shift; } my ($person, $thing); my $string = delayed {"Look $person, no $thing!"}; $person = "Ma"; $thing = "stringy eval"; say $string;

      Here's a version that eliminates the need to declare variables up-front, but does the interpolation manually using s///eg. It's pretty dodgy, and I wouldn't put it anywhere near production code, but it's quite cute as an example...

      use v5.10; use strict; use warnings; use PerlX::QuoteOperator delayed => { -emulate => 'q', -with => sub ($) { package delayed; my $str = shift; use PadWalker; use overload q[""] => sub { $_[0]->() }, fallback => 1; my $get = sub { my $var = shift; my $my = PadWalker::peek_my(3); return $my->{$var} if exists $my->{$var}; my $our = PadWalker::peek_our(3); return $our->{$var} if exists $our->{$var}; die "No such variable: $var\n"; }; bless sub { my $_ = $str; s/(\$\w+)/${$get->($1)}/eg; s/(\@\w+)/join $", @{$get->($1)}/eg; return $_; }; }, }; my $string = delayed "Look $person, no @thing!"; my $person = "Ma"; my @thing = qw( stringy eval ); say $string;

      It's cute, but the code that does the interpolation is pretty dodgy.

      package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
        "...but you need to declare them up-front...eliminates the need to declare variables up-front...pretty dodgy"

        You've got that crystal ball, yes ;-) or how else did you know about my next question?

        Best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1020521]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2014-04-19 14:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (481 votes), past polls