[30218] in Perl-Users-Digest
Perl-Users Digest, Issue: 1461 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Apr 22 16:43:00 2008
Date: Tue, 22 Apr 2008 13:42:27 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Tue, 22 Apr 2008 Volume: 11 Number: 1461
Today's topics:
Perl - delayed evaluation of variables? <wardct@gmail.com>
Re: Perl - delayed evaluation of variables? <smallpond@juno.com>
Re: Perl - delayed evaluation of variables? <smallpond@juno.com>
Re: Perl - delayed evaluation of variables? <ben@morrow.me.uk>
Re: Perl - delayed evaluation of variables? <noreply@gunnar.cc>
Re: Perl - delayed evaluation of variables? <syscjm@sumire.gwu.edu>
Re: Perl - delayed evaluation of variables? <wardct@gmail.com>
Re: Perl - delayed evaluation of variables? <wardct@gmail.com>
Re: Perl - delayed evaluation of variables? <skye.shaw@gmail.com>
Perl LWP Issue with JSP's <mark1.thompson45@btinternet.com>
Re: Perl LWP Issue with JSP's <mark1.thompson45@btinternet.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 17 Apr 2008 03:56:27 -0700 (PDT)
From: ditman <wardct@gmail.com>
Subject: Perl - delayed evaluation of variables?
Message-Id: <6dcacaaa-9c5d-4466-84e4-35d8850bc685@m44g2000hsc.googlegroups.com>
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";
}
------------------------------
Date: Thu, 17 Apr 2008 05:00:15 -0700 (PDT)
From: smallpond <smallpond@juno.com>
Subject: Re: Perl - delayed evaluation of variables?
Message-Id: <3ccbb935-5b0c-4085-a91c-0b198c5dc71f@l64g2000hse.googlegroups.com>
On Apr 17, 6:56 am, ditman <war...@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
------------------------------
Date: Thu, 17 Apr 2008 05:04:26 -0700 (PDT)
From: smallpond <smallpond@juno.com>
Subject: Re: Perl - delayed evaluation of variables?
Message-Id: <495f3659-1691-464f-a3ba-2e1c8b4bdc30@c65g2000hsa.googlegroups.com>
On Apr 17, 8:00 am, smallpond <smallp...@juno.com> wrote:
> On Apr 17, 6:56 am, ditman <war...@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.
------------------------------
Date: Thu, 17 Apr 2008 13:26:37 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Perl - delayed evaluation of variables?
Message-Id: <t8sjd5-qki.ln1@osiris.mauzo.dyndns.org>
Quoth ditman <wardct@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
------------------------------
Date: Thu, 17 Apr 2008 16:45:35 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Perl - delayed evaluation of variables?
Message-Id: <66p63oF2l3136U1@mid.individual.net>
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
------------------------------
Date: Thu, 17 Apr 2008 09:46:22 -0500
From: Chris Mattern <syscjm@sumire.gwu.edu>
Subject: Re: Perl - delayed evaluation of variables?
Message-Id: <slrng0eolu.drd.syscjm@sumire.gwu.edu>
On 2008-04-17, ditman <wardct@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?
Sure. Define the string when 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";
> }
Is there a reason you are not defining $LANG_LOCALE until show_message?
How are you attempting to find BuildVer.ini when you don't know where
$LANG_LOCALE is until you call the function that tells you you can't
find BuildVer.ini in a directory defined by $LANG_LOCALE?
--
Christopher Mattern
NOTICE
Thank you for noticing this new notice
Your noticing it has been noted
And will be reported to the authorities
------------------------------
Date: Thu, 17 Apr 2008 08:23:36 -0700 (PDT)
From: technut666 <wardct@gmail.com>
Subject: Re: Perl - delayed evaluation of variables?
Message-Id: <ae761e48-8a39-46ba-a884-800334dbe3df@l42g2000hsc.googlegroups.com>
On Apr 17, 1:04 pm, smallpond <smallp...@juno.com> wrote:
> On Apr 17, 8:00 am, smallpond <smallp...@juno.com> wrote:
>
>
>
> > On Apr 17, 6:56 am, ditman <war...@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.
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 )
------------------------------
Date: Thu, 17 Apr 2008 08:40:52 -0700 (PDT)
From: technut666 <wardct@gmail.com>
Subject: Re: Perl - delayed evaluation of variables?
Message-Id: <88b6d9b0-ff87-48c6-8cbd-65cccfefc929@b64g2000hsa.googlegroups.com>
On Apr 17, 3:46 pm, Chris Mattern <sys...@sumire.gwu.edu> wrote:
> On 2008-04-17, ditman <war...@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?
>
> Sure. Define the string when 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";
> > }
>
> Is there a reason you are not defining $LANG_LOCALE until show_message?
> How are you attempting to find BuildVer.ini when you don't know where
> $LANG_LOCALE is until you call the function that tells you you can't
> find BuildVer.ini in a directory defined by $LANG_LOCALE?
>
> --
> Christopher Mattern
>
> NOTICE
> Thank you for noticing this new notice
> Your noticing it has been noted
> And will be reported to the authorities
Hi,
Thanks to everyone for your responses.
I am defining a bunch of error messages at the start of the script.
Then I read in a bunch of environment settings from an external file,
one of which is $LANG_LOCALE. So I can't know $LANG_LOCALE before I
declare the error message.
Fortunately I have found a solution in the Perl Cookbook of all
places! :D
It's not a delayed expansion, but it does what I need.
Goes something like this:
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";
$ERROR_MSG_BUILDVER_INI_MISSING =~ s/(\$\w+)/$1/eeg;
print "$ERROR_MSG_BUILDVER_INI_MISSING";
}
Description:
Using the backslash before $LANG_LOCALE in the
$ERROR_MSG_BUILDVER_INI_MISSING string means it is not evaluated.
Then in the function, the regexp looks for anything which looks like a
variable (a dollar sign plus text).
This is captured as $1.
The 'ee' means "Evaluate the right side as a string then eval the
result". So if $1 is the string $LANG_LOCALE and $LANG_LOCALE="de-
DE", this is evaluated as "de-DE".
And the 'g' means operate globally obviously - so this will work for
all variables.
Hope this is clearly explained.
Regards,
C.
------------------------------
Date: Fri, 18 Apr 2008 00:38:25 -0700 (PDT)
From: "Skye Shaw!@#$" <skye.shaw@gmail.com>
Subject: Re: Perl - delayed evaluation of variables?
Message-Id: <8f4f190b-a616-483e-8ed4-cb66b0b22324@c58g2000hsc.googlegroups.com>
On Apr 17, 3:56=A0am, ditman <war...@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. =A0As 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?
Yikes
> Code sample:
>
> my $LANG_LOCALE; # defined, but null value
>
> my $ERROR_MSG_BUILDVER_INI_MISSING =3D "BuildVer.ini file for service
> not found at $_TOP/services/imApp/$LANG_LOCALE";
>
> function show_message(){
> =A0 $LANG_LOCALE =3D "de-DE";
> =A0 print "$ERROR_MSG_BUILDVER_INI_MISSING";
>
> }
If you must:
my $LANG_LOCALE; # defined, but null value
my $ERROR_MSG_BUILDVER_INI_MISSING =3D 'print "BuildVer.ini file for
service
not found at $_TOP/services/imApp/$LANG_LOCALE"';
sub show_message {
$LANG_LOCALE =3D "de-DE";
eval $ERROR_MSG_BUILDVER_INI_MISSING";
}
If you really want to go the template route my not use printf?
my $ERROR_XYZ =3D 'Damn it Jim, I'm a %s not a %s';
printf $ERROR_XYZ, 'Aussie', 'Kiwi';
-Skye
------------------------------
Date: Wed, 16 Apr 2008 11:29:04 -0700 (PDT)
From: tommo_blade <mark1.thompson45@btinternet.com>
Subject: Perl LWP Issue with JSP's
Message-Id: <f5b1a411-095d-45d0-94be-d26cf3f10cbe@m73g2000hsh.googlegroups.com>
Hi, I am tring to retrieve some data from a source page and following
that a link within that source page, I am able to get the data from
the source page OK and also extract the link from the source page but
when I try to get the data from that link I get an error, I get most
of the data from the link but the error appears to be something to do
with a JSP but not sure, can anyone help, the snippet of code below is
what I am using to get the data from the link and the info below that
is the error I get.
p.s. if I do a "view source" on the page I can see the data I need so
I would have thought that the LWP call would have retrieved it.
$ua = LWP::UserAgent->new();
$ua->cookie_jar($cookie_jar);
$req = new HTTP::Request GET => $url;
$res = $ua->request($req);
<!-- clientplayerinfo-football.jsp starts -->
Failure trying to include /clientplayerinfo-football-new.jsp:
Exception in JSP: /clientplayerinfo-football-new.jsp:118
115: <td rowspan="3" width="2"></td>
116: <td align="center" valign="middle">
117: <table width="530"
height="36" border="0" cellpadding="0" cellspacing="0" align="center">
118: <ctv:sort var="facts"
items="${player.facts}" property="gameFixture.date" ascending="false" /
>
119: <c:set var="playerScore"
value="0" />
120: <c:set var="uniqueRules"
value="${player.game.uniqueRules}" />
121: <tr align="left" valign="top">
Stacktrace:
<!-- clientplayerinfo-football.jsp ends -->
------------------------------
Date: Wed, 16 Apr 2008 12:00:19 -0700 (PDT)
From: tommo_blade <mark1.thompson45@btinternet.com>
Subject: Re: Perl LWP Issue with JSP's
Message-Id: <c05f55bf-d3ba-42d3-b951-8cdcff101629@e67g2000hsa.googlegroups.com>
Sorry, it was a typo - all working ok now...
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 1461
***************************************