Click here to get back home

Perl - delayed evaluation of variables?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Perl - delayed evaluation of variables? ditman 04-17-2008
Get Chitika Premium
Posted by ditman on April 17, 2008, 6:56 am
Please log in for more thread options
Hi,

I have a script where I define a string at the top of the file.
The string includes a variable which is not set until later in the
script. As a result the variable is evaluated as null and does not
appear in the string.
Is there a way to say to the Perl interpreter - do not evaluate the
string defined at the start, but wait until it is used?

Code sample:

my $LANG_LOCALE; # defined, but null value

my $ERROR_MSG_BUILDVER_INI_MISSING = "BuildVer.ini file for service
not found at $_TOP/services/imApp/$LANG_LOCALE";

function show_message(){
$LANG_LOCALE = "de-DE";
print "$ERROR_MSG_BUILDVER_INI_MISSING";
}

Posted by smallpond on April 17, 2008, 8:00 am
Please log in for more thread options
> Hi,
>
> I have a script where I define a string at the top of the file.
> The string includes a variable which is not set until later in the
> script. As a result the variable is evaluated as null and does not
> appear in the string.
> Is there a way to say to the Perl interpreter - do not evaluate the
> string defined at the start, but wait until it is used?
>
> Code sample:
>
> my $LANG_LOCALE; # defined, but null value
>
> my $ERROR_MSG_BUILDVER_INI_MISSING = "BuildVer.ini file for service
> not found at $_TOP/services/imApp/$LANG_LOCALE";
>
> function show_message(){
> $LANG_LOCALE = "de-DE";
> print "$ERROR_MSG_BUILDVER_INI_MISSING";
>
> }


To do your function, just remove the quotes
so the variable is evaluated inside the function:

function show_message(){
$LANG_LOCALE = "de-DE";
print $ERROR_MSG_BUILDVER_INI_MISSING;
}

To answer your more general question, there is
no such thing as "defining" a string. You were
making an assignment, which does the evaluation
of the expression. If you really want to evaluate
an expression later, look at the eval function.
--S

Posted by smallpond on April 17, 2008, 8:04 am
Please log in for more thread options
>
>
>
> > Hi,
>
> > I have a script where I define a string at the top of the file.
> > The string includes a variable which is not set until later in the
> > script. As a result the variable is evaluated as null and does not
> > appear in the string.
> > Is there a way to say to the Perl interpreter - do not evaluate the
> > string defined at the start, but wait until it is used?
>
> > Code sample:
>
> > my $LANG_LOCALE; # defined, but null value
>
> > my $ERROR_MSG_BUILDVER_INI_MISSING = "BuildVer.ini file for service
> > not found at $_TOP/services/imApp/$LANG_LOCALE";
>
> > function show_message(){
> > $LANG_LOCALE = "de-DE";
> > print "$ERROR_MSG_BUILDVER_INI_MISSING";
>
> > }
>
> To do your function, just remove the quotes
> so the variable is evaluated inside the function:
>
> function show_message(){
> $LANG_LOCALE = "de-DE";
> print $ERROR_MSG_BUILDVER_INI_MISSING;
>
> }
>
> To answer your more general question, there is
> no such thing as "defining" a string. You were
> making an assignment, which does the evaluation
> of the expression. If you really want to evaluate
> an expression later, look at the eval function.
> --S


Sorry. Meant to move the expression inside the
function, of course, not just the variable.

Posted by technut666 on April 17, 2008, 11:23 am
Please log in for more thread options
>
>
>
>
> > > Hi,
>
> > > I have a script where I define a string at the top of the file.
> > > The string includes a variable which is not set until later in the
> > > script. As a result the variable is evaluated as null and does not
> > > appear in the string.
> > > Is there a way to say to the Perl interpreter - do not evaluate the
> > > string defined at the start, but wait until it is used?
>
> > > Code sample:
>
> > > my $LANG_LOCALE; # defined, but null value
>
> > > my $ERROR_MSG_BUILDVER_INI_MISSING = "BuildVer.ini file for service
> > > not found at $_TOP/services/imApp/$LANG_LOCALE";
>
> > > function show_message(){
> > > $LANG_LOCALE = "de-DE";
> > > print "$ERROR_MSG_BUILDVER_INI_MISSING";
>
> > > }
>
> > To do your function, just remove the quotes
> > so the variable is evaluated inside the function:
>
> > function show_message(){
> > $LANG_LOCALE = "de-DE";
> > print $ERROR_MSG_BUILDVER_INI_MISSING;
>
> > }
>
> > To answer your more general question, there is
> > no such thing as "defining" a string. You were
> > making an assignment, which does the evaluation
> > of the expression. If you really want to evaluate
> > an expression later, look at the eval function.
> > --S
>
> Sorry. Meant to move the expression inside the
> function, of course, not just the variable.

Hi,

Thanks for you response! :D
The code sample was to just illustrate the issue - there is more going
on here.
Basically I have to declare a bunch of error messages at the start of
the script.
I then read in a file with a bunch of environment settings - one of
which is $lang_locale. So I can't know $lang_locale until after I
have declared the error messages. (Again a simplified description of
what is going on in the script :D )

Posted by Ben Morrow on April 17, 2008, 8:26 am
Please log in for more thread options

>
> I have a script where I define a string at the top of the file.
> The string includes a variable which is not set until later in the
> script. As a result the variable is evaluated as null and does not
> appear in the string.
> Is there a way to say to the Perl interpreter - do not evaluate the
> string defined at the start, but wait until it is used?
>
> Code sample:
>
> my $LANG_LOCALE; # defined, but null value
>
> my $ERROR_MSG_BUILDVER_INI_MISSING = "BuildVer.ini file for service
> not found at $_TOP/services/imApp/$LANG_LOCALE";
>
> function show_message(){
> $LANG_LOCALE = "de-DE";
> print "$ERROR_MSG_BUILDVER_INI_MISSING";
> }

You can use Scalar::Defer for this. Note that all variables used
(whether lexical or global) will need to be declared first.

#!/usr/bin/perl

use strict;
use warnings;

use Scalar::Defer qw/lazy/;

my $LANG_LOCALE;
our $_TOP;

# This can be as complicated an expression as you like, including
# multiple statements.

my $MSG = lazy { "Not found at $_TOP/services/imApp/$LANG_LOCALE" };

$LANG_LOCALE = 'en_GB';
$_TOP = '/foo/bar';

print $MSG;

This will only evaluate the interpolation once, then cache the result;
if you want it re-evaluated every time, use 'defer' instead of 'lazy'.

Ben


Similar ThreadsPosted
quick help message; or delayed 'use ' March 10, 2006, 2:00 pm
Lazy evaluation? February 13, 2007, 9:14 pm
order of evaluation March 26, 2007, 6:41 am
$@ Evaluation Error Oddity July 8, 2004, 8:07 pm
SWITCH: question on evaluation November 10, 2004, 12:42 pm
Hashes, flattening, evaluation March 16, 2005, 11:36 pm
Unintuitive expression evaluation October 27, 2005, 8:30 pm
variable evaluation and printing March 21, 2007, 7:20 pm
Arithmetic Expression Evaluation in print November 22, 2005, 12:31 am
Evaluation context of C-style Logical And/Or January 14, 2007, 8:45 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap