[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Evaluating expressions with regexp



Peder, 

> I want to evaluate expressions in an text, the expressions are in the form:
> <#execute:(expression)#>, like <#execute: 933.47+273#>.
>
> I am using "s/<#execute:(.*?)#>/\1/goe" to find and evaluate this and
> it works -- kind of.
> Problem is it evaluates to "SCALAR(0x8b9159c)" instead of just plain
"1206.47".
>
> How do I fix this?
> /Peder

Considering a simple test case:

#! /usr/bin/perl
# This is expr.pl
use strict;

my $expr = '<#execute: 933.47+273#>';

$expr =~ s/<#execute:(.*?)#>/\1/e;
print '#### expr.pl ####', "\n";
print '->', $expr, '<-', "\n";
__END__

then:
perl -w expr.pl
gives:

# Can't use \1 to mean $1 in expression.
File 'expr.pl'; Line 7
#### expr.pl ####
->SCALAR(0x3511000)<-

So we change line 7 to
$expr =~ s/<#execute:(.*?)#>/$1/ge;

Now:
perl -w expr.pl
gives:

#### expr.pl ####
-> 933.47+273<-

Nearly there, now we need:
$expr =~ s/<#execute:(.*?)#>/$1/gee;

so :
perl -w expr.pl

gives:
#### expr.pl ####
->1206.47<-

The moral of the story is : always use the -w flag and 'use strict' when
developing new code.

Cheers,
Steve Cardie
Pindar Systems plc


# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org