From: ditman on
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";
}
From: smallpond on
On Apr 17, 6:56 am, ditman <war...(a)gmail.com> wrote:
> 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
From: smallpond on
On Apr 17, 8:00 am, smallpond <smallp...(a)juno.com> wrote:
> On Apr 17, 6:56 am, ditman <war...(a)gmail.com> wrote:
>
>
>
> > 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.
From: Ben Morrow on

Quoth ditman <wardct(a)gmail.com>:
>
> 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

From: Gunnar Hjalmarsson on
ditman wrote:
> 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";
> }

Why don't you simply pass the variables to the function?

sub show_message {
my ($top, $lang) = @_;
print 'BuildVer.ini file for service not' .
" found at $top/services/imApp/$lang";
}

show_message( $_TOP, $LANG_LOCALE );

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl