[24531] in Perl-Users-Digest
Perl-Users Digest, Issue: 6711 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Jun 20 18:05:41 2004
Date: Sun, 20 Jun 2004 15:05:06 -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 Sun, 20 Jun 2004 Volume: 10 Number: 6711
Today's topics:
Creating a scalar value from other scalars <ducott@hotmail.com>
Re: Creating a scalar value from other scalars <usenet@morrow.me.uk>
Re: Creating a scalar value from other scalars <postmaster@castleamber.com>
Re: Creating a scalar value from other scalars <ittyspam@yahoo.com>
Re: Embedding perl in Java (Shalini Joshi)
Re: Embedding perl in Java <usenet@morrow.me.uk>
Re: how to determine whether variable is a scalar or an <superdoehli_nospam@gmx.at>
Re: multiline comments don't work as explained in the F <fake@nowhere.com>
Re: multiline comments don't work as explained in the F <matthew.garrish@sympatico.ca>
Re: print a hash ordered by value (Anno Siegel)
Re: Text Parsing (Shalini Joshi)
Re: unbuffered pipe ctcgag@hotmail.com
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sun, 20 Jun 2004 19:17:32 GMT
From: "Robert TV" <ducott@hotmail.com>
Subject: Creating a scalar value from other scalars
Message-Id: <gplBc.832453$Ig.490304@pd7tw2no>
Hi, I thought I read somewhere once that creating a scalar value from other
scalars is bad. My small script below creates a data based on the users IP
address and the current Epoch. I'm asking for advice on whether this is bad
scripting.
__ACTUAL CODE__
#!/usr/bin/perl
use CGI qw/:standard/;
use CGI::Carp qw(fatalsToBrowser);
$timestamp = time();
$ipaddress = $ENV{'REMOTE_ADDR'};
$ipaddress =~ s/\.//g; #remove periods from IP address
$filename = "$ipaddress\_$timestamp\.dat";
open (FILE,">>$filename"); #create new filename based on above variables
close(FILE);
print header;
print "File name: $filename has been created on server";
exit;
__END CODE__
The above creates a new file on the server with a name similair to
"20553102224_1087758244.dat". While the code functions without error, is the
scripting sound? I would appreciate alternative methods if this script is
inefficient or erroneous. Thank you.
Robert
------------------------------
Date: Sun, 20 Jun 2004 19:48:28 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Creating a scalar value from other scalars
Message-Id: <cb4pmc$h7d$2@wisteria.csv.warwick.ac.uk>
Quoth "Robert TV" <ducott@hotmail.com>:
> Hi, I thought I read somewhere once that creating a scalar value from other
> scalars is bad.
I have no idea where you heard that, but it is total nonsense. Most of
programming consists of creating variables from the values of other
variables...
> My small script below creates a data based on the users IP
> address and the current Epoch. I'm asking for advice on whether this is bad
> scripting.
>
> __ACTUAL CODE__
>
> #!/usr/bin/perl
You should probably use taint mode for CGI scripts. See perldoc perlsec.
You also need
use warnings;
use strict;
here.
> use CGI qw/:standard/;
> use CGI::Carp qw(fatalsToBrowser);
>
> $timestamp = time();
my $timestamp = time;
as you're now using strict. Ditto below.
> $ipaddress = $ENV{'REMOTE_ADDR'};
You will need to untaint this before you can create a file based on it.
A good solution would be to use Regexp::Common.
> $ipaddress =~ s/\.//g; #remove periods from IP address
> $filename = "$ipaddress\_$timestamp\.dat";
There is no need to escape a . in strings; I would use ${} rather than
escaping the backslash:
my $filename = "${ipaddress}_$timestamp.dat";
> open (FILE,">>$filename"); #create new filename based on above variables
ALWAYS check that open succeeded.
Use three-arg open.
Use lexical filehandles.
{
open my $FILE, '>>', $filename
or die "can't append to $filename: $!";
}
> close(FILE);
As $FILE is a lexical, it will be automatically closed at the end of the
scope.
> print header;
> print "File name: $filename has been created on server";
> exit;
>
> __END CODE__
If you just write __END__ then perl will understand you as well...
Ben
--
I must not fear. Fear is the mind-killer. I will face my fear and
I will let it pass through me. When the fear is gone there will be
nothing. Only I will remain.
ben@morrow.me.uk Frank Herbert, 'Dune'
------------------------------
Date: Sun, 20 Jun 2004 15:03:48 -0500
From: John Bokma <postmaster@castleamber.com>
Subject: Re: Creating a scalar value from other scalars
Message-Id: <40d5eda5$0$191$58c7af7e@news.kabelfoon.nl>
Robert TV wrote:
> Hi, I thought I read somewhere once that creating a scalar value from other
> scalars is bad. My small script below creates a data based on the users IP
> address and the current Epoch. I'm asking for advice on whether this is bad
> scripting.
Your script is bad.
> __ACTUAL CODE__
>
> #!/usr/bin/perl
I recommend adding -T to the she-bang. Look up taint mode
Also add:
use strict;
use warnings;
> use CGI qw/:standard/;
> use CGI::Carp qw(fatalsToBrowser);
>
> $timestamp = time();
my $timestamp;
etc.
> $ipaddress = $ENV{'REMOTE_ADDR'};
> $ipaddress =~ s/\.//g; #remove periods from IP address
> $filename = "$ipaddress\_$timestamp\.dat";
>
> open (FILE,">>$filename"); #create new filename based on above variables
open can fail, check this.
MORE IMPORTANT: you build a filename from external DATA. CHECK THAT IT'S
VALID, and more important don't fix it, reject it if it's not valid. So
clearly define what you consider valid and secure.
> close(FILE);
can fail, check, and hence make use of the CGI mods.
--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
------------------------------
Date: Sun, 20 Jun 2004 17:14:55 -0400
From: Paul Lalli <ittyspam@yahoo.com>
Subject: Re: Creating a scalar value from other scalars
Message-Id: <20040620171119.U23512@dishwasher.cs.rpi.edu>
On Sun, 20 Jun 2004, Robert TV wrote:
> Hi, I thought I read somewhere once that creating a scalar value from other
> scalars is bad. My small script below creates a data based on the users IP
> address and the current Epoch. I'm asking for advice on whether this is bad
> scripting.
I think it's possible you misunderstood what you read. My impression is
that you saw a conversation involving creating variable _names_ from other
variables, not the actual variables themselves. This is bad. An example
of this is:
$name = 'John';
${$name} = 23;
Using this code, a new variable $John springs into existence, with the
value of 23. This is called Symbollic References. They are very much
considered bad. For a thorough discussion of why, run the following
command:
perldoc -q 'variable name'
What you were doing, however, is creating variable *values* based on
existing variables. This is not only not wrong, it's one of the most
basic foundations of programming.
I hope this helps clarify. Or maybe I guessed completely wrong. It's
been known to happen. :)
Paul Lalli
------------------------------
Date: 20 Jun 2004 11:53:53 -0700
From: shalinij1@yahoo.com (Shalini Joshi)
Subject: Re: Embedding perl in Java
Message-Id: <283d6b7e.0406201053.eafedff@posting.google.com>
Ben Morrow <usenet@morrow.me.uk> wrote in message
>
> No. The Perl source would still be visible; the user would still need
> Perl installed.
>
> For advice on (the futility of) concealing Perl code in general, see
> perldoc -q hide.
>
> Ben
Hi.
Thanks a lot Ben for this information. All this while we were really
feeling smart thinking we could actually solve the problem of hiding
the source by embedding perl in java. Is there a solution to this
problem? Do u mean to say that no commercial product makes use of Perl
as a source language??
So since most of the work involved in this project deals with text
parsing, would it be advisable to use perl now, since we wouldn't be
able to keep the source code to ourselves? We are thinking of
licensing the product rather than provide services.
I am an intern and most of this business and product concept is very
new to me as am mostly used to just rattling off code and making
applications, and have never done the product or source code hiding
thiing before. I'd really appreciate any help in this regard. Needless
to say, the group has helped me out a lot already and would like to
thank you for that :)..
Regards,
Shalini.
------------------------------
Date: Sun, 20 Jun 2004 19:41:17 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Embedding perl in Java
Message-Id: <cb4p8t$h7d$1@wisteria.csv.warwick.ac.uk>
Quoth shalinij1@yahoo.com (Shalini Joshi):
> Ben Morrow <usenet@morrow.me.uk> wrote in message
> >
> > For advice on (the futility of) concealing Perl code in general, see
> > perldoc -q hide.
>
> Thanks a lot Ben for this information. All this while we were really
> feeling smart thinking we could actually solve the problem of hiding
> the source by embedding perl in java. Is there a solution to this
> problem? Do u mean to say that no commercial product makes use of Perl
> as a source language??
Have you read that faq? There are a variety of solutions, none of them
secure. It doesn't matter, though, as source code hiding never works
anyway and all you need to do is slap a big fat copyright notice on it
and noone can steal it.
You would need to talk to your company lawyers, of course.
Ben
--
Outside of a dog, a book is a man's best friend.
Inside of a dog, it's too dark to read.
ben@morrow.me.uk Groucho Marx
------------------------------
Date: Sun, 20 Jun 2004 21:00:10 +0200
From: Alex <superdoehli_nospam@gmx.at>
Subject: Re: how to determine whether variable is a scalar or an array?
Message-Id: <MPG.1b3ffd2741d014a2989681@news.inode.at>
> perldoc -f ref
Thank you both (Paul and Gunnar)! Silly me! *look_ashamed_to_the_ground*
Never met this function before...
Alex
------------------------------
Date: Sun, 20 Jun 2004 14:13:27 -0500
From: fake <fake@nowhere.com>
Subject: Re: multiline comments don't work as explained in the FAQ
Message-Id: <abobd0tgv0sfkn9774t5ccqunokkpvhaf8@4ax.com>
On Sun, 20 Jun 2004 11:41:30 -0400, "Matt Garrish"
<matthew.garrish@sympatico.ca> wrote:
>
>"fake" <fake@nowhere.com> wrote in message
>news:b6aad01n9th3lr6uo3e6f9tke8kja655v8@4ax.com...
>> From the FAQ:
>> How can I comment out a large block of perl code?
>>
>> Use embedded POD to discard it:
>>
>> # program is here
>>
>> =for nobody
>> This paragraph is commented out
>>
>> # program continues
>>
>> =begin comment text
>>
>> all of this stuff
>>
>> here will be ignored
>> by everyone
>>
>> =end comment text
>>
>> =cut
> ^^^^^^^
>
>Important little bit, there.
>
>
>> This can't go just anywhere. You have to put a pod directive where
>> the parser is expecting a new statement, not just in the middle of
>> an expression or some other arbitrary yacc grammar production.
>> >>>>>>>>>>>>>>>
>>
>>
>> However, only a "1" is printed from this program:
>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>..
>>
>>
>> #!c:/perl/bin/perl
>>
>> print "\n 1";
>>
>>
>> =begin comment text
>>
>> print "\n 2";
>>
>>
>>
>> =end comment text
>>
>
>Hmm, no =cut here...
>
>
>> print "\n 3";
>>
>>
>>
>> >>>>>>>>>>>>>>
>>
>> Hence the multiline comments as explained in the perldoc do not work.
>>
>> That is all...
>
>Failing to understand the documentation is not grounds for claiming it is
>wrong. Next time ask for an explanation of why your code is not working
>before you assume that you are right and the documentation is wrong...
>
I cannot understand why I did not see the "=cut". I must have read the
perdoc query results 6 times. Also, I read at least 10
webpages/newsgroup posts on the subject, and the first time I saw
"=cut" was in this thread. Very peculiar! But it works....thanks!
------------------------------
Date: Sun, 20 Jun 2004 16:39:08 -0400
From: "Matt Garrish" <matthew.garrish@sympatico.ca>
Subject: Re: multiline comments don't work as explained in the FAQ
Message-Id: <FBmBc.2147$Nz.148253@news20.bellglobal.com>
"fake" <fake@nowhere.com> wrote in message
news:abobd0tgv0sfkn9774t5ccqunokkpvhaf8@4ax.com...
>
> I cannot understand why I did not see the "=cut". I must have read the
> perdoc query results 6 times. Also, I read at least 10
> webpages/newsgroup posts on the subject, and the first time I saw
> "=cut" was in this thread. Very peculiar! But it works....thanks!
>
Never trust a web page! Especially when there's a whole perldoc on the
subject:
http://www.perldoc.com/perl5.8.4/pod/perlpod.html
(irony of referencing a web page duly noted... : )
Matt
------------------------------
Date: 20 Jun 2004 18:05:19 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: print a hash ordered by value
Message-Id: <cb4jkv$rf0$2@mamenchi.zrz.TU-Berlin.DE>
Yiping Zhan <yzhan@andrew.cmu.edu> wrote in comp.lang.perl.misc:
> What is a good way of getting a printed output like:
>
> d 7
> b 6
> a 3
> c 1
>
> if I have a hash like:
>
> %h = qw(a 3 b 6 c 1 d 7);
That's a frequently asked question. You can look up the answer through
perldoc -q 'sort a hash'.
Anno
------------------------------
Date: 20 Jun 2004 13:11:23 -0700
From: shalinij1@yahoo.com (Shalini Joshi)
Subject: Re: Text Parsing
Message-Id: <283d6b7e.0406201211.2bc48623@posting.google.com>
Bob Walton <invalid-email@rochester.rr.com> wrote in message news:<40D3BD8F.90207@rochester.rr.com>...
> Well, that code doesn't go into an infinite loop for me. It generates a
> compile error.
No, no...am sorry for the confusion, I meant the code that Jeff
posted(the one with the redo etc..that one was going into an infinite
loop)...This code(which you tried) is hte one i had started out with
initially before consulting the Group..:)...
THanks for the feedback though.
Sorry about the confusion!
Regards,
Shalini
Fixing that by adding the missing }, it works fine,
> assuming that what you want to do is take the lines of a text file with
> records separated by FPR and put them all on one line with no whitespace
> in between. Are you sure the program isn't just waiting for you to
> enter records separated by FPR on standard input (which is where it will
> look for input unless you give it some filenames as arguments)? It will
> continue to do that until you give it an end-of-file. And note that
> your "empty record" must be separated from the next record by the
> character sequence FPR , not a newline. And depending upon your OS and
> whether you have STDIN coming from a console, a file or something else,
> you might not get anything until the OS thinks you sent a line, using
> the OS's definition of a line (terminated by the OS's newline sequence),
> not Perl's FPR idea of a newline which you gave Perl.
>
>
> ...
>
>
> > Shalini
------------------------------
Date: 20 Jun 2004 22:03:05 GMT
From: ctcgag@hotmail.com
Subject: Re: unbuffered pipe
Message-Id: <20040620180305.647$5P@newsreader.com>
spacehopper_man@yahoo.com (Oliver) wrote:
> I'm using a pipe to talk to a forked child process -
>
> I do not want any data to get lost if the child process exits
> unexpectedly -
>
> - so I was hoping to turn off buffering so that there was at most 1
> piece of data (i.e. one line) in the pipe.
>
> I thought if I did:
>
> select(PIPEHANDLE);
> $| = 1;
>
> then my pipe would not do buffering, and I was expecting that that
> would lead to blocking writes - i.e. if the data hadn't been read from
> the pipe, then a write to the pipe would block until it was.
>
> however - after doing the above - I find I can still write data to the
> pipe - regardless of how much is read out of the other end.
You have prevented Perl from buffing up it's output before handing it to
the OS, but the OS still has it's own pipe buffer.
If you want the two processes to proceed in lock-stop, you could require
an acknowledgement from the child before the parent attempts the next
print.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
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 V10 Issue 6711
***************************************