[29011] in Perl-Users-Digest
Perl-Users Digest, Issue: 255 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Mar 22 21:10:10 2007
Date: Thu, 22 Mar 2007 18:09:08 -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 Thu, 22 Mar 2007 Volume: 11 Number: 255
Today's topics:
CGI URL Redirect <uctraing@ultranet.com>
Re: CGI URL Redirect <spamtrap@dot-app.org>
How can I make my prime number generator better? nikolas.britton@gmail.com
how to use perl to redirect web page? <paul.pettus@gmail.com>
Re: is mod_perl better for CGI application? <spamtrap@dot-app.org>
Net::SNMP and Non-Blocking Calls <news.20070322.nospam@27b-6.de>
Re: parsing a tab delimited or CSV, but keep the delimi <christian.bongiorno@gmail.com>
Re: parsing a tab delimited or CSV, but keep the delimi <chris.uppal@metagnostic.REMOVE-THIS.org>
Re: parsing a tab delimited or CSV, but keep the delimi <googlegroupie@coloraura.com>
Re: parsing a tab delimited or CSV, but keep the delimi <jurgenex@hotmail.com>
Re: Perl Equivelent of C++ program <johnwilkinson@clara.co.uk>
Re: Perl Equivelent of C++ program (Jens Thoms Toerring)
Re: Perl Equivelent of C++ program <tadmc@augustmail.com>
Re: Perl Equivelent of C++ program (Jens Thoms Toerring)
Re: Problem with indirect variables <mdudley@king-cart.com>
Re: Problem with indirect variables <uri@stemsystems.com>
Re: Problem with indirect variables <jurgenex@hotmail.com>
Re: Problem with indirect variables <tadmc@augustmail.com>
Re: Server/Clients system (Jamie)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 22 Mar 2007 21:33:12 GMT
From: - Bob - <uctraing@ultranet.com>
Subject: CGI URL Redirect
Message-Id: <4dt50392s9nh2fiot4st9mi19ekple7bpm@4ax.com>
I'm familiar with using this raw statement to cause a redirect via a
CGI:
print "Location: http://www.someplace.yyy \n\n";
Is there a more modern way I should be doing it now. Something
built-in and/or more object oriented?
Thanks,
------------------------------
Date: Thu, 22 Mar 2007 17:43:52 -0400
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: CGI URL Redirect
Message-Id: <m2bqilar8n.fsf@local.wv-www.com>
- Bob - <uctraing@ultranet.com> writes:
> I'm familiar with using this raw statement to cause a redirect via a
> CGI:
>
> print "Location: http://www.someplace.yyy \n\n";
>
>
> Is there a more modern way I should be doing it now. Something
> built-in and/or more object oriented?
CGI.pm is a core module, and has a redirect() method. Assuming that $q is
an instance of the CGI class:
print $q->redirect('http://someplace.invalid');
sherm--
--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net
------------------------------
Date: 22 Mar 2007 17:47:28 -0700
From: nikolas.britton@gmail.com
Subject: How can I make my prime number generator better?
Message-Id: <1174610848.316977.94170@y80g2000hsf.googlegroups.com>
How can I improve my code?... faster, better style, proper programming
techniques, better algorithm? Thanks!
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
my $n = 100000; #limit at 6 * 10^14, regex's break.
my $k = 6; #6 * 10^14, min value is 6.
$k = int(($k / 6) + .5);
my $y1; my $y2;
for (1 .. int(($n * .1665) + .5)) {
#check 6k - 1:
$y1 = ((6 * $k) - 1);
if ($y1 !~ m/5$/o) {
for (1 .. (int((sqrt($y1) * .1665) + .5))) {
$y2 = ((6 * $_) - 1);
goto end1 if (($y1 / $y2) !~ m/\./o);
$y2 += 2;
goto end1 if (($y1 / $y2) !~ m/\./o);
}
#is prime tap 1:
print "$y1\n";
} end1:
#check 6k + 1:
$y1 += 2;
if ($y1 !~ m/5$/o) {
for (1 .. (int((sqrt($y1) * .1665) + .5))) {
$y2 = ((6 * $_) - 1);
goto end2 if (($y1 / $y2) !~ m/\./o);
$y2 += 2;
goto end2 if (($y1 / $y2) !~ m/\./o);
}
#is prime tap 2:
print "$y1\n";
} end2:
$k++;
}
------------------------------
Date: 22 Mar 2007 17:44:59 -0700
From: "Paul" <paul.pettus@gmail.com>
Subject: how to use perl to redirect web page?
Message-Id: <1174610699.031455.166610@y66g2000hsf.googlegroups.com>
hi, there,
I am using perl to access specified web pages. this is my code. when I
run the code, I get the following error: 302 Found. I checked and knew
this is because of redirect.
Would you guys tell me how to solve this problem?
thanks.
paul.
use LWP 5.64;
use LWP::UserAgent;
use HTTP::Headers;
use HTTP::Cookies;
$lurl='http://www.abcddefg.com';
$lbrowser = LWP::UserAgent->new;
$lbrowser->timeout(5);
$lcookie_jar = HTTP::Cookies->new;
$lreq = new HTTP::Request('GET',$lurl);
$lresponse = $lbrowser->simple_request($lreq);
$lcookie_jar->extract_cookies($response);
if ($lresponse->is_success)
{
print "OK. connected with server\n"
}
else
{
print "Error1: when get $lurl:\t" . $lresponse->status_line . "\n";
}
------------------------------
Date: Thu, 22 Mar 2007 16:41:30 -0400
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: is mod_perl better for CGI application?
Message-Id: <m2lkhpau4l.fsf@local.wv-www.com>
"John" <john1949@yahoo.com> writes:
> We use Perl primarily to write our website. The site uses CGI.pm
> extensively. So, every page begins my $x=param('x') etc and depending
> on whether it is 'undef' will jump to some routine or other. Lots of forms.
Handling incoming parameters is a wash - it has to be done either way, as
well as with FastCGI (which another reply mentioned), and none of them is
notably faster than the others at doing it.
The "big win" for mod_perl or FastCGI is script startup time. With a CGI,
the server has to start a new perl instance, which then has to load and
compile your script and any modules it uses, before it can even begin to
parse any parameters. Also, if your script accesses a database, it needs
to establish a connection to the DB and log in.
With mod_perl or FastCGI, most of that per-hit overhead goes away. Your
script is compiled only once. It can connect to a DB once, and then re-use
that connection for every hit. Each hit then has about as much overhead as
a function call.
sherm--
--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net
------------------------------
Date: Thu, 22 Mar 2007 23:47:55 +0100
From: Magnus Schmidt <news.20070322.nospam@27b-6.de>
Subject: Net::SNMP and Non-Blocking Calls
Message-Id: <etv12v$e9b$1@news.n-ix.net>
Hello,
I'm writing an application which polls data via SNMP from many devices just
every second. Now I'm stuck how to implement this in an efficient way.
The Perl programm should poll data from about 50-100 different devices via
SNMP (v1). Intervalls between polls are different, but can go down to 1
second. Any of the polled devices may not be reachable. In that case the
timeout should not block the other polls.
I first used perl threads (use threads, interpreter threads), one for every
device. It worked without problems, however it wasted too much resources
(one process for each poller), even if you take into account, that most
process-data is shared with the parent.
Therefore I tried to put all pollers into a non-blocking run, using
Net::SNMP. You can define callbacks and queue several SNMP-requests, but
then you have to call snmp_dispatcher(), which only returns after all
requests *received* an answer. So the "non-blocking feature" turns out to
be blocking one, hence not suitable for my needs.
Now I'm stuck, as I don't know how to proceed from here. The other
SNMP-Modules don't seem to be any better than Net::SNMP. Only thing left
now, would be to write a really asynchronous snmp-poller by myself, but
that's probably just reinventing the wheel.
Happy about any hints and input,
Magnus
--
This email-address will stop working in a few weeks.
Please use http://27b-6.de/email instead.
------------------------------
Date: 22 Mar 2007 13:26:11 -0700
From: "Sideswipe" <christian.bongiorno@gmail.com>
Subject: Re: parsing a tab delimited or CSV, but keep the delimiter
Message-Id: <1174595171.101300.40600@o5g2000hsb.googlegroups.com>
It also looks like double comma before a "" like this ,,"" -- the ,,
counts as 1 token
------------------------------
Date: Thu, 22 Mar 2007 21:20:49 -0000
From: "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org>
Subject: Re: parsing a tab delimited or CSV, but keep the delimiter
Message-Id: <4602f30a$0$757$bed64819@news.gradwell.net>
Sideswipe wrote:
> I need to parse either a CSV or a Tab delimited file, BUT I need to
> keep the delimiting token -- I am parsing these files as generated
> from excel and the user expects them to process EXACTLY as it appears
> in the spreadsheet.
>
> I am cross posting this in the Perl and Java groups because, my
> implementation is in Java, but Perl users use regexp far more
> frequently.
I do not believe that regular expressions (even the weird "enhanced" regular
expressions that Java has apparently borrowed from Perl) are a suitable tool
for this job. Even if you could find some way of expressing the complex logic
of CSV parsing using the extended regexps, your resulting code would be totally
incomprehensible.
A useful test case for your parser is that /any/ CSV file can (treated as
a single string) be used as the contents of a field within another CSV.
Embedded newlines, arbitrarily deep nesting, etc, are all allowed. That power
hardly makes it any harder to parse CVS /unless/ you've started out by relying
on an inadequate tool like regexps.
Also, don't forget to test your code with files generated from Excel in places
where the numeric thousands separator is not ','.
-- chris
P.S. Although I don't much like Java's regexps (I think they are /grossly/
overused), it seems that I like them rather better than OE's spellchecker,
which suggests that replace "regexp" with "rage" and "regexps" with "creeps".
True AI is clearly only just around the corner ;-)
------------------------------
Date: 22 Mar 2007 14:30:31 -0700
From: "Daniel Pitts" <googlegroupie@coloraura.com>
Subject: Re: parsing a tab delimited or CSV, but keep the delimiter
Message-Id: <1174599031.586060.124260@d57g2000hsg.googlegroups.com>
On Mar 22, 1:04 pm, "Sideswipe" <christian.bongio...@gmail.com> wrote:
> I know this question has been asked before, and believe me I checked
> the newsgroup and web extensively before asking, but I think my needs
> are slightly different.
>
> I need to parse either a CSV or a Tab delimited file, BUT I need to
> keep the delimiting token -- I am parsing these files as generated
> from excel and the user expects them to process EXACTLY as it appears
> in the spreadsheet.
>
> I am cross posting this in the Perl and Java groups because, my
> implementation is in Java, but Perl users use regexp far more
> frequently.
Cross posting is fine, but you should add a follow up header. Which I
have (to comp.lang.java.programmer)
>
> Here are the 3 different REGEX expressions I have found /created but
> none are correct. The only certainty I can get is to get rid of all
> the delimiters. I have to maintain the delimiters because the
> information I am accessing is column based (and thus fixed)
>
> private final Pattern COLUMN_PATTERN = Pattern.compile("(\"[^\"]*\",,|
> [^,]+)"); // I think this close
> private final Pattern COLUMN_PATTERN = Pattern.compile("([^\",]*|\"([^
> \"]|\"\")+\")(,)");
> private final Pattern COLUMN_PATTERN = Pattern.compile(",(?=(?:[^\\\"]*
> \\\"[^\\\"]*\\\")*(?![^\\\"]*\\\"))");
>
> So, you have the cases of:
>
> 1) continuous string or with space -> single ',' (comma) separated
> 2) String has a comma in it, and is "" -> it is followed by a ",,"
> double comma token. So the string in "" is a token and the double
> comma is also a token
> 3) blank cells are just a single comma ,
>
> That's my understanding of the cases. The logic should be IDENTICAL
> for tab delimited and simply substitute characters
I'm not sure that a regex is good enough to do everything...
Anyway, here are the cases that I can think of, ignoring the
delimiters.
Field value:
Field value: ,
Field value: "
Field value: a,b
Field value: "a and b"
Field value: 6"3
What are the encodings of this?
I'm guessing that
Field value:
Encoded value:
Field value: ,
Encoded value: ","
Field value: "
Encoded value: ""
Field value: a,b
Encoded value: "a,b"
Field value: "a and b"
Encoded value: ""a and b""
Field value: 6"3
Encoded value: 6""3
You can verify these cases in excel.
If those ARE the correct cases, then this would work:
import java.util.List;
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class ParseCSV {
final static String quoted = "\"(:?[^\"]|\"\")+\"";
public static List<String> parseCSV(String csv, String delim) {
final Pattern NEXT_COLUMN = nextColumnRegex(delim);
final List<String> strings = new ArrayList<String>();
final Matcher matcher = NEXT_COLUMN.matcher(csv);
while (!matcher.hitEnd() && matcher.find()) {
String match = matcher.group(1);
if (match.matches(quoted)) {
match = match.substring(1, match.length() - 1);
}
match = match.replaceAll("\"\"", "\"");
strings.add(match);
}
return strings;
}
private static Pattern nextColumnRegex(String comma) {
String unquoted = "(:?[^\"" + comma + "]|\"\")*";
String ending = "(:?" + comma +"|$)";
return Pattern.compile('(' + quoted + '|' + unquoted + ')' +
ending);
}
public static void main(String[] args) {
String csv = ",\",\",\"\",\"a,b\",\"\"a and b\"\",6\"\"3";
List<String> result = parseCSV(csv, ",");
for (String col : result) {
System.out.println("Field value:" + col);
}
}
}
------------------------------
Date: Thu, 22 Mar 2007 21:34:45 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: parsing a tab delimited or CSV, but keep the delimiter
Message-Id: <VDCMh.5478$zN.2814@trndny03>
Sideswipe wrote:
> I need to parse either a CSV or a Tab delimited file, BUT I need to
> keep the delimiting token -- I am parsing these files as generated
> from excel and the user expects them to process EXACTLY as it appears
> in the spreadsheet.
Text::CSV should do the job quite nicely for regular CSV files.
> Here are the 3 different REGEX expressions I have found /created but
> none are correct. [...]
That doesn't suprise me. While REs are quite powerful indeed, there are
many, many tasks, where you need a more complex logic than REs can provide.
Even if you have custom requirements and you need a custom version of a CSV
parser I would still start with one of the Text::CSV modules and customize
that instead of rolling my own code from scratch and repeating all the
mistakes that have been fixed in the ready-made modules a long time ago.
jue
------------------------------
Date: Thu, 22 Mar 2007 21:18:17 +0000
From: John Wilkinson <johnwilkinson@clara.co.uk>
Subject: Re: Perl Equivelent of C++ program
Message-Id: <1sk6u4ksdzc1z.c710xvvojhpi$.dlg@40tude.net>
On Thu, 22 Mar 2007 15:43:06 +0000, Ian Wilson wrote:
> John Wilkinson wrote:
>> Hi,
>> I have a simple C++ program example, I would dearly love to swap to perl.
>> It uses pipes, which I do not understand, and I was wondering if anyone
>> could convert this bit of code for me:
>> #include <stdio>
>> #include <string>
>> #include <sys/types>
>> #include <unistd>
>>
>> int main (int argc, char* argv{}){
>>
>> int n;
>> char line[1024];
>>
>> while((n = read(STDIN_FILENO, line, 1024)) > 0) {
>> line[n]= '\0';
>> if (strcmp(line,"firstcommand" == 0) {
>> strcpy(line, "answer,= 1234 -2345 155 test");
>> }
>> else {
>> strcpy(line, "unknown command");
>> }
>> n = strlen(line);
>> if (write(STDOUT_FILENO, line, n) != n) {
>> fprintf(stderr,"Server can't write to stdout\n");
>> }
>> }
>> }
>>
>> This is supposed to be a simple example server, started by another process,
>> and using pipes to communicate to this server.
>>
>
> Since I'm not a C programmer I suspect it is equivalent to something not
> entirely unlike this (untested):
>
> #!perl
> use strict;
> use warnings;
> my $reply = "Oops";
> while(<>) {
> if (/^firstcommand/) { $reply = "answer,= 1234 -2345 155 test"; }
> else { $reply = "unknown command"; }
> print $reply or warn "Server can't write to stdout because $!\n";
> }
>
> But I think you will be much better off abandoning your C examples and
> starting by looking for Perl examples instead.
Thanks for the help here.
I ran your example, the program started, however it gets no output from
this code.
If I start this from the command line, I have to enter a cr, before I get
back the characters entered. If the program that starts this server is not
sending cr, how can I do this in perl?
Thanks,
John
------------------------------
Date: 22 Mar 2007 22:40:55 GMT
From: jt@toerring.de (Jens Thoms Toerring)
Subject: Re: Perl Equivelent of C++ program
Message-Id: <56gevnF29hgv3U1@mid.uni-berlin.de>
John Wilkinson <johnwilkinson@clara.co.uk> wrote:
> On Thu, 22 Mar 2007 15:43:06 +0000, Ian Wilson wrote:
> > John Wilkinson wrote:
> >> I have a simple C++ program example, I would dearly love to swap to perl.
> >> It uses pipes, which I do not understand, and I was wondering if anyone
> >> could convert this bit of code for me:
> >> #include <stdio>
> >> #include <string>
> >> #include <sys/types>
> >> #include <unistd>
> >>
> >> int main (int argc, char* argv{}){
> >>
> >> int n;
> >> char line[1024];
> >>
> >> while((n = read(STDIN_FILENO, line, 1024)) > 0) {
> >> line[n]= '\0';
> >> if (strcmp(line,"firstcommand" == 0) {
> >> strcpy(line, "answer,= 1234 -2345 155 test");
> >> }
> >> else {
> >> strcpy(line, "unknown command");
> >> }
> >> n = strlen(line);
> >> if (write(STDOUT_FILENO, line, n) != n) {
> >> fprintf(stderr,"Server can't write to stdout\n");
> >> }
> >> }
> >> }
> >>
> >> This is supposed to be a simple example server, started by another
> >> process, and using pipes to communicate to this server.
> >
> > Since I'm not a C programmer I suspect it is equivalent to something not
> > entirely unlike this (untested):
> >
> > #!perl
> > use strict;
> > use warnings;
> > my $reply = "Oops";
> > while(<>) {
> > if (/^firstcommand/) { $reply = "answer,= 1234 -2345 155 test"; }
> > else { $reply = "unknown command"; }
> > print $reply or warn "Server can't write to stdout because $!\n";
> > }
> >
> > But I think you will be much better off abandoning your C examples and
> > starting by looking for Perl examples instead.
> Thanks for the help here.
> I ran your example, the program started, however it gets no output from
> this code.
> If I start this from the command line, I have to enter a cr, before I get
> back the characters entered. If the program that starts this server is not
> sending cr, how can I do this in perl?
Actually, you will have the same problem with your program if you
run it from the command line and enter the text manually - the
terminal only passes the data on to your program when you have
pressed the <RETURN> key. Moreover, while your program might work
most of the time if the input is coming from e.g. pipe, there's a
problem you should be aware of: read() may return less characters
than the whole string "firstcommand" has, so you may accidentally
skip "good" input!
Of course, you can rewrite the Perl script to use sysread() and
syswrite() (the Perl replacements for the C low level read() and
write() functions) and thus a (more or less) faithfully transla-
ting your program (including the bug) would be
#!/usr/bin/perl
use strict;
use warnings;
while ( sysread STDIN, my $buf, 1024 ) {
if ( $buf eq /^firstcommand' ) {
$buf = 'answer,= 1234 -2345 155 test';
} else {
$buf = 'unknown command';
}
syswrite STDOUT, $buf
or warn "Server can't write to stdout\n";
}
But I don't know if that really helps you much. Perhaps you
better specify exactly what you need instead of just giving
us a slightly broken program to start with. Only then the
things where Perl shines can be used. Like it is it's more
like a Babelfish translation...
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
------------------------------
Date: Thu, 22 Mar 2007 17:58:05 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Perl Equivelent of C++ program
Message-Id: <slrnf062ft.35f.tadmc@tadmc30.august.net>
John Wilkinson <john.wilkinson@philips.com> wrote:
> It uses pipes,
No it doesn't.
> which I do not understand,
Pipes are ridiculously easy. Understanding them is not hard.
All a pipe does is to connect the STDOUT of the "left" process
to the STDIN of the "right" process.
If you are writing a program to run in the "left" process, then
make your output on STDOUT.
If you are writing a program to run in the "right" process, then
take your program input from STDIN.
That is all that is required to use a pipe to connect the two programs.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 22 Mar 2007 23:57:59 GMT
From: jt@toerring.de (Jens Thoms Toerring)
Subject: Re: Perl Equivelent of C++ program
Message-Id: <56gjg7F28mmqgU1@mid.uni-berlin.de>
Tad McClellan <tadmc@augustmail.com> wrote:
> John Wilkinson <john.wilkinson@philips.com> wrote:
> > It uses pipes,
> No it doesn't.
> > which I do not understand,
> Pipes are ridiculously easy. Understanding them is not hard.
> All a pipe does is to connect the STDOUT of the "left" process
> to the STDIN of the "right" process.
>
> If you are writing a program to run in the "left" process, then
> make your output on STDOUT.
>
> If you are writing a program to run in the "right" process, then
> take your program input from STDIN.
>
> That is all that is required to use a pipe to connect the two programs.
In principle you're right, but if you use low level I/O C functions
like read() and write() (as the OP did) or the Perls equivalent
functions sysread() and syswrite() it starts to become a tad more
complicated. And then some people assume that pipes can temporarily
store infinite amounts of data and wonder why their programs hang;-)
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
------------------------------
Date: Thu, 22 Mar 2007 21:49:39 GMT
From: Marshall Dudley <mdudley@king-cart.com>
Subject: Re: Problem with indirect variables
Message-Id: <TRCMh.16227$G23.3269@newsreading01.news.tds.net>
Jürgen Exner wrote:
> Marshall Dudley wrote:
>
>> Why can I not get the second variable to print it's value in the
>> following code?
>>
>> $test = "Test String";
>> $form_data{'test'} = "form data variable";
>> foreach $string (test,"form_data{'test'}") {
>> print "$string = $$string\n";
>>
>
>
> Had you enabled warnings then perl would have told you:
> Use of uninitialized value in concatenation (.) or string at ....
>
> In short: You do not have a variable that is named
> form_data{'test'}
>
> Besides that what you are trying to do is known as symbolic references and
> it is A BAD IDEA!
> See the FAQ "How can I use a variable as a variable name?" for details about
> why and what to use instead.
>
> jue
>
>
>
I looked at the faq, but did not really see anything applicable. Here
is the deal. I have been notified that I have clean up all my variables
that print to not allow any <, >, ', &, ( or ) in them to prevent cross
platform scripting hacking. So I have a confirmation page that prints
dozens of variables that are global variables, most of which are form
variables from the form input. So I wanted to have a list of the
variable names, and use one small section of code to do the
substitutions, like this:
foreach my $variable
(customer_number,"form_data{'Ecom_BillTo_Postal_Name_First'}","form_data{'Ecom_BillTo_Postal_Name_Last'}",
"form_data{'Ecom_BillTo_Company'}","form_data{'Ecom_BillTo_Postal_Street_Line1'}","form_data{'Ecom_BillTo_Postal_City'}",
"form_data{'Ecom_BillTo_Postal_StateProv'}","form_data{'Ecom_BillTo_PostalCode'}","bill_country,form_data{'Ecom_BillTo_Telecom_Phon
"form_data{'Ecom_BillTo_Online_Email'}","form_data{'Ecom_ShipTo_Postal_Name_First'}","form_data{'Ecom_ShipTo_Postal_Name_Last'}",
"form_data{'Ecom_ShipTo_Postal_Street_Line1'}","form_data{'Ecom_ShipTo_Postal_City'}","ship_country","form_data{'PO'}",
"$form_data{'Comments'}") {
$$variable =~ s/\</\</g;
$$variable =~ s/\>/\>/g;
$$variable =~ s/\(/\(/g;
$$variable =~ s/\)/\)/g;
$$variable =~ s/\'/\'/g;
}
If I inline it, I end up with 5*18 or 90 lines of code in what should be
able to be done in 6 or 7.
Thanks,
Marshall
------------------------------
Date: Thu, 22 Mar 2007 17:46:18 -0500
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Problem with indirect variables
Message-Id: <x74poc7v7p.fsf@mail.sysarch.com>
>>>>> "MD" == Marshall Dudley <mdudley@king-cart.com> writes:
MD> I looked at the faq, but did not really see anything applicable. Here
MD> is the deal. I have been notified that I have clean up all my
MD> variables that print to not allow any <, >, ', &, ( or ) in them to
MD> prevent cross platform scripting hacking. So I have a confirmation
MD> page that prints dozens of variables that are global variables, most
MD> of which are form variables from the form input. So I wanted to have
MD> a list of the variable names, and use one small section of code to do
MD> the substitutions, like this:
you are thinking about this in a totally backwards way on several levels.
MD> foreach my $variable
MD> (customer_number,"form_data{'Ecom_BillTo_Postal_Name_First'}","form_data{'Ecom_BillTo_Postal_Name_Last'}",
MD> "form_data{'Ecom_BillTo_Company'}","form_data{'Ecom_BillTo_Postal_S
where did those 'variables' come from? show that code too. and as
several people have said, "form_data{'Ecom_BillTo_Postal_Name_First'}"
IS NOT A VARIABLE. it is not even a hash value as it has no normal perl
$hash{key} syntax (it is missing the leading $). i don't know what you
think it is nor where you learned it but it makes no sense in how you
describe it. it is a simple string expression. so it can't ever refer to
anything else in perl especially other 'variables' as it is not even a
proper variable name.
now you actually seem to have the values in a hash. i will assume that
the hash is named %form_data since it is perl. maybe it comes from some
bastard cgi parser (my main guess). so the first thing you need to do is
use a proper module CGI.pm to get your form params. but let's stick with
the hash for now and this is easily changed to use CGI.pm.
MD> $$variable =~ s/\</\</g;
MD> $$variable =~ s/\>/\>/g;
MD> $$variable =~ s/\(/\(/g;
MD> $$variable =~ s/\)/\)/g;
MD> $$variable =~ s/\'/\'/g;
again, perl has modules for that. why are you reinventing wheels?
MD> If I inline it, I end up with 5*18 or 90 lines of code in what should
MD> be able to be done in 6 or 7.
if you used a module it would be 1 line.
use HTML::Entities
encode_entities $_ for values %form_data ;
now isn't that a bit easier to understand? and it works too!
so please learn more perl and stop beating your head against the
wall. your loop code above had NO VARIABLES so don't use that term and
then expect us to understand you.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Thu, 22 Mar 2007 22:54:55 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Problem with indirect variables
Message-Id: <3PDMh.15045$dG.5218@trndny08>
Marshall Dudley wrote:
> Jürgen Exner wrote:
>> Marshall Dudley wrote:
>>> print "$string = $$string\n";
>>>
>>
>> Besides that what you are trying to do is known as symbolic
>> references and it is A BAD IDEA!
>> See the FAQ "How can I use a variable as a variable name?" for
>> details about why and what to use instead.
>>
>> jue
>>
>>
>>
> I looked at the faq, but did not really see anything applicable.
You were trying to do "$$string". This is dereferencing a symbolic
reference. Therefore the FAQ is very applicable.
> Here
> is the deal. I have been notified that I have clean up all my
> variables that print to not allow any <, >, ', &, ( or ) in them to
> prevent cross platform scripting hacking.
This seems like such a frequent task. I never use Perl for web site
development, but maybe it would be worthwhile to check CPAN if there isn't a
module that does this kink of cleanup already.
> So I have a confirmation
> page that prints dozens of variables that are global variables, most
> of which are form variables from the form input. So I wanted to have
> a list of the variable names, and use one small section of code to do
> the substitutions, like this:
>
> foreach my $variable
> (customer_number,"form_data{'Ecom_BillTo_Postal_Name_First'}","form_data{'Ecom_BillTo_Postal_Name_Last'}",
> "form_data{'Ecom_BillTo_Company'}","form_data{'Ecom_BillTo_Postal_Street_Line1'}","form_data{'Ecom_BillTo_Postal_City'}",
What is this "form_data{'xxxx'}"? Do you mean "$form_data{'xxxx'}"?
If yes, then you already got the hash that is suggested in the FAQ.
for my $variable(keys(%form_data)) {
> "form_data{'Ecom_BillTo_Postal_StateProv'}","form_data{'Ecom_BillTo_PostalCode'}","bill_country,form_data{'Ecom_BillTo_Telecom_Phon
> "form_data{'Ecom_BillTo_Online_Email'}","form_data{'Ecom_ShipTo_Postal_Name_First'}","form_data{'Ecom_ShipTo_Postal_Name_Last'}",
> "form_data{'Ecom_ShipTo_Postal_Street_Line1'}","form_data{'Ecom_ShipTo_Postal_City'}","ship_country","form_data{'PO'}",
> "$form_data{'Comments'}") {
> $$variable =~ s/\</\</g;
$form_data{$variable} =~ .....
> $$variable =~ s/\>/\>/g;
> $$variable =~ s/\(/\(/g;
> $$variable =~ s/\)/\)/g;
> $$variable =~ s/\'/\'/g;
> }
>
> If I inline it, I end up with 5*18 or 90 lines of code in what should
> be able to be done in 6 or 7.
>
> Thanks,
>
> Marshall
------------------------------
Date: Thu, 22 Mar 2007 18:23:42 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Problem with indirect variables
Message-Id: <slrnf063vu.35f.tadmc@tadmc30.august.net>
Marshall Dudley <mdudley@king-cart.com> wrote:
> Jürgen Exner wrote:
>> Besides that what you are trying to do is known as symbolic references and
>> it is A BAD IDEA!
>> See the FAQ "How can I use a variable as a variable name?" for details about
>> why and what to use instead.
> I have been notified that I have clean up all my variables
> that print to not allow any <, >, ', &, ( or ) in them
> So I wanted to have a list of the
> variable names, and use one small section of code to do the
> substitutions, like this:
>
> foreach my $variable
> (customer_number,"form_data{'Ecom_BillTo_Postal_Name_First'}","form_data{'Ecom_BillTo_Postal_Name_Last'}",
> "form_data{'Ecom_BillTo_Company'}","form_data{'Ecom_BillTo_Postal_Street_Line1'}","form_data{'Ecom_BillTo_Postal_City'}",
> "form_data{'Ecom_BillTo_Postal_StateProv'}","form_data{'Ecom_BillTo_PostalCode'}","bill_country,form_data{'Ecom_BillTo_Telecom_Phon
> "form_data{'Ecom_BillTo_Online_Email'}","form_data{'Ecom_ShipTo_Postal_Name_First'}","form_data{'Ecom_ShipTo_Postal_Name_Last'}",
> "form_data{'Ecom_ShipTo_Postal_Street_Line1'}","form_data{'Ecom_ShipTo_Postal_City'}","ship_country","form_data{'PO'}",
> "$form_data{'Comments'}") {
> $$variable =~ s/\</\</g;
> $$variable =~ s/\>/\>/g;
> $$variable =~ s/\(/\(/g;
> $$variable =~ s/\)/\)/g;
> $$variable =~ s/\'/\'/g;
> }
Do you need a bazillion hash entries to illustrate your problem,
or would, say, two entries do it?
Do you need 5 substitutions to illustrate your problem, or would, say,
one or two substitutions do it?
Have you seen the Posting Guidelines that are posted here frequently?
The
< > ' &
characters are not meta there, so they do not need any backslashing.
> If I inline it, I end up with 5*18 or 90 lines of code in what should be
> able to be done in 6 or 7.
Now that you have come clean on your XY problem, we can offer a fairly
easy solution that uses no crufty symbolic references.
Use the aliasing feature of foreach instead:
-------------------------------------------------
#!/usr/bin/perl
use warnings;
use strict;
my $customer_number = '<bad stuff>';
my %form_data = (
abc => 'more <bad> stuff',
xyz => 'yet more <ungood> stuff',
);
foreach my $var ( $customer_number, values %form_data ) {
$var =~ s/</</g;
$var =~ s/>/>/g;
}
print "customer_number=$customer_number\n";
print "$_=$form_data{$_}\n" for keys %form_data;
-------------------------------------------------
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Thu, 22 Mar 2007 20:31:13 GMT
From: nospam@geniegate.com (Jamie)
Subject: Re: Server/Clients system
Message-Id: <Lc1174574200123070x8b30754@pong.podro.com>
In <slrnf05noj.v7v.hjp-usenet2@yoyo.hjp.at>,
"Peter J. Holzer" <hjp-usenet2@hjp.at> mentions:
>On 2007-03-21 01:21, Jamie <nospam@geniegate.com> wrote:
>> One thing I would really like to do (alas, have no business model for it)
>> is to marry NNTP with podcasts and RSS.
>>
>> So called "flooding protocol" is something I find quite amusing, and aside from
>> NNTP (AFAIK) isn't widely used anymore. Seems like it's almost always a central
>> master -> slave model, (mirrors, etc.. CPAN for example)
>>
>> Having "NPTP" (Net Podcast Transfer Protocol? NRTP Net RSS Transfer Protocol?)
>[...]
>> Unlike NNTP, the enclosures and other RSS fields would have to be planned for
>> (possibly with delayed transfer/referral of enclosures)
>
>I think regular NNTP would work just fine for that. Just use appropriate
>content-types (application/rss+xml, audio/*, video/* or whatever). You
>may want a subscription mechanism in the protocol (NNTP for normal
>newsgroup could use one, too, but subscribing whole hierarchies is
>"traditional" and so far nobody has had enough incentive to push for a
>more fine-grained approach).
The bit I can't work out is how you'd associate other types of information.
The title, description and other elements of the RSS entry, keeping the
enclosures distinct, supporting multiple enclosures pr. item and ensuring
that the items are stuck to the correct feed. Unless each feed were
it's own newsgroup, this doesn't seem viable. (threading, maybe.. but followups
might be better used as discussion areas and/or corrections)
One may not want to spool all the enclosures, but rather defer them until
someone requests them. Just flood the metadata, rather the way leafnode/suck
type news servers work, the .overview should contain as much of the RSS
data as possible.
Would be "nice" if you could go RSS -> NRTP -> RSS, loosing as little data as
possible AND provide "discussion boards" on NNTP instead of those crummy
web-based things.
You're right about the fine-grained subscription model, categories and THEN
individual podcasts, but as the whole idea is to expose people to material
they might not otherwise have seen, it could be tricky. (this is why I much
perfer NNTP over mailing lists, you can easily sign on to a news server, browse
the groups awhile, subscribe/unsubscribe at a whim)
NNTP isn't 8-bit clean and doesn't really provide a way to xfer enclosures.
>> No money, and I have a hard time seeing how someone would want to pay for it
>> (much less pay for the hardware and bandwidth) If I could find a practical
>> business model for what I just described, I'd be doing it. :-)
>The business model is simple: Make huge losses for a couple of years,
>don't give any hint how you ever plan on making money, then sell to one
>of the big players for several hundred million bucks :-).
LOL! Only problem with that is, once you sell it.. you won't be able to
work on it anymore. heh
Come to think of it, there really isn't a "business model" as far as I can
see for NNTP either, except the commercial news servers. They would never
have been in business had it not been for the fact that NNTP was already
established. Kind of sad, commercialization really has ruined the internet.
Maybe if net neutrality fails or for whatever reason ISP's start limiting
access to certain websites, flooding protocol will come back. Also, I suppose
people could charge for value-added access or something?
This would be an incredibly fun project to work on, especially if you did it
in such a way as to not require graphical terminals or anything like that.
Jamie
--
http://www.geniegate.com Custom web programming
Perl * Java * UNIX User Management Solutions
------------------------------
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 255
**************************************