[24026] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 6223 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Mar 5 14:10:41 2004

Date: Fri, 5 Mar 2004 11:10:09 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Fri, 5 Mar 2004     Volume: 10 Number: 6223

Today's topics:
    Re: shellwords.pl - certain string makes it gobble up a <dmcbride@naboo.to.org.no.spam.for.me>
    Re: shellwords.pl - certain string makes it gobble up a <usenet@morrow.me.uk>
    Re: Spreadsheet::WriteExcel <bart.lateur@pandora.be>
        Sys::Syslog under Solaris <lool@dooz.org>
        Unpack binary file with C Struct to text? (RT)
    Re: What with this open file descriptor/"Read on closed (Rex Gustavus Adolphus)
    Re: What with this open file descriptor/"Read on closed <usenet@morrow.me.uk>
    Re: What with this open file descriptor/"Read on closed (Rex Gustavus Adolphus)
    Re: What with this open file descriptor/"Read on closed <usenet@morrow.me.uk>
    Re: What with this open file descriptor/"Read on closed <ittyspam@yahoo.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Fri, 05 Mar 2004 15:51:55 GMT
From: Darin McBride <dmcbride@naboo.to.org.no.spam.for.me>
Subject: Re: shellwords.pl - certain string makes it gobble up all ram!
Message-Id: <vm12c.707414$X%5.486939@pd7tw2no>

Anthony Rumble wrote:

> After a lot of tearing hair out finding this.. I have found a rather nasty
> little bug in a piece of perl that has been hanging around since perl4
> days..
> 
> shellwords.pl
> 
> Basically, if you give it a string where the last character is a
> backslack, it goes off and gobbles all available ram (quite quickly too
> mind you).
> 
> Here is an example.
> 
> !! WARNING !! Be warned.. this will gobble up all ram and possibly
> kill your machine... You have been warned..
> 
> #!/usr/bin/perl
> 
> require "shellwords.pl";
> 
> my(@words) = shellwords("This is a test\\");
> 
> Im just surprised that such a bug has been left around!
> I can't really even work out where or how it's even doing it..
> 
> Any ideas?

Yeah - don't put a backslash in there.  ;-)

Patch:

--- /share/perl5/lib/5.8.3/shellwords.pl        2004-01-31
21:17:07.000000000 -0700
+++ ./shellwords.pl     2004-03-05 08:49:37.000000000 -0700
@@ -36,6 +36,9 @@
            elsif (s/^([^\s\\'"]+)//) {
                $snippet = $1;
            }
+           elsif (m/^\\$/) {
+               die "Escape character found without anything to escape";
+           }
            else {
                s/^\s+//;
                last;


Output:

$ perl -e 'require "./shellwords.pl"; shellwords("this\\")'
Escape character found without anything to escape at ./shellwords.pl line
40.



------------------------------

Date: Fri, 5 Mar 2004 16:27:28 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: shellwords.pl - certain string makes it gobble up all ram!
Message-Id: <c2a9pg$gov$2@wisteria.csv.warwick.ac.uk>


Anthony Rumble <lk7ho6c02@removethis.sneakemail.com> wrote:
> After a lot of tearing hair out finding this.. I have found a rather nasty
> little bug in a piece of perl that has been hanging around since perl4 days..
> 
> shellwords.pl
> 
> Basically, if you give it a string where the last character is a backslack,
> it goes off and gobbles all available ram (quite quickly too mind you).
<snip>
> Any ideas?

Use Text::ParseWords or Text::Balanced instead.

Ben

-- 
For the last month, a large number of PSNs in the Arpa[Inter-]net have been
reporting symptoms of congestion ... These reports have been accompanied by an
increasing number of user complaints ... As of June,... the Arpanet contained
47 nodes and 63 links. [ftp://rtfm.mit.edu/pub/arpaprob.txt] * ben@morrow.me.uk


------------------------------

Date: Fri, 05 Mar 2004 17:30:23 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: Spreadsheet::WriteExcel
Message-Id: <hodh409f58880b3hq86q3ujpr2ghp5rrsq@4ax.com>

Fabio wrote:

>But how can I put some values into the cells of an existing worksheet? 

I thought there was a module on CPAN that combines
Spreadsheet::ParseExcel with Spreadsheet::WriteExcel. If so, it's well
hidden, 'cos I can't find any trace of it.

-- 
	Bart.


------------------------------

Date: Fri, 5 Mar 2004 14:09:51 +0100
From: Loic Minier <lool@dooz.org>
Subject: Sys::Syslog under Solaris
Message-Id: <slrnc4gv0v.ogj.lool@zen.via.ecp.fr>

   Hi,

 I have trouble using the Sys::Syslog package under Solaris.  If I use
 'stream', I don't get anything logged, and if I log via udp instead,
 all my logs are prefixed with the program name ($ident).  This doesn't
 happen with other programs running on the same host.

 Here's the relevant Perl :
 use Sys::Syslog qw(:DEFAULT setlogsock);
 #setlogsock('stream'); # doesn't work
 setlogsock('inet'); # or udp
 syslog('info', "test\n");

 Any idea on how to get stream at work?  Or to get rid of the prefix?

   Thanks,

-- 
Loïc Minier <lool@dooz.org>


------------------------------

Date: 5 Mar 2004 07:00:01 -0800
From: rctantx@netscape.net (RT)
Subject: Unpack binary file with C Struct to text?
Message-Id: <cae896e4.0403050700.79a08b8c@posting.google.com>

Hello,

Hope someone can help me with this question.

I have this binary file (octal dump below):

0000000 0027 c08c 0000 0404 002c 8ce0 0000 0000
0000020 4047 a06c 000b 789d
0000030

It consists of a C structure of

struct {
unsigned int where;
std::string info;
clock_t clock;
timeval time;
}

# BELOW IS THE SNIP OF MY SCRIPT. PLEASE IGNORE THE SYNTAX.

#!/usr/local/bin/perl -w

my $data_file = 'test.bin';
open INFILE, "<$data_file" or die "Unable to open \"$data_file\"\n";
binmode INFILE;

@data = ReadData ($data_file);

sub ReadData
{
   my $binary;

  if (!(sysread (INFILE, $binary, 24))) { return (); }
   
  # HOW TO UNPACK THE FILE? I KNOW THE TEMPLATE BELOW IS WRONG
  return (unpack ("LLLLLL", $binary));

}

My question is how do I unpack the string in the binary file and 
display the output? I have read the PACK/UNPACK Perldoc but still
couldn't understand. I would really appreciate your input.

Please response to the group.

Thanks in advance!
-ryan-


------------------------------

Date: 5 Mar 2004 06:51:47 -0800
From: uffesterner@spamhole.com (Rex Gustavus Adolphus)
Subject: Re: What with this open file descriptor/"Read on closed filehandle " stuff?
Message-Id: <c70a85ff.0403050651.7b90c07a@posting.google.com>

Ben Morrow <usenet@morrow.me.uk> wrote in message news:<c27aco$cmg$2@wisteria.csv.warwick.ac.uk>...
> [please wrap attribution lines at 72 columns]

Sorry, english is not my first language, what's an "attribution line"?
If it's the "References: <c70a85ff.0402200341..."-stuff at the start
of my posts,
it's nothing I can help, Google adds that when I post (I'm using
Google to read/post usenet).

> 
> uffesterner@spamhole.com (Rex Gustavus Adolphus) wrote:
> > Ben Morrow <usenet@morrow.me.uk> wrote in message
> > news:<c14sju$c84$1@wisteria.csv.warwick.ac.uk>...
> > >
> > > You don't really need to declare these up here: it's better to keep
> > > variables in as small a scope as possible, and simply declare them the
> > > first time you use them.
> > 
> > I guess this is mostly a matter of personal taste (TMTOWTDI), or is
> > there any special reason your way is better?
> 
> It is not so much a matter of taste as a matter of good programming
> practice.  If variables are declared in the smallest scope possible
> there is the minimal chance of confusion with other variables of the
> same name. Also, it is then (usually) possible to see at a glance the
> whole scope the variable is valid over.

OK.
A related question, is it in any way inefficient to redeclare
variables,
as in ex 2 below:
ex 1:
my $foo;
while ($this_is_true) {
    $foo = 0;
    ...
    # do fancy stuff
    ...
    $foo = $something;
}

ex 2:
while ($this_is_true) {
    my $foo = 0;
    ...
    # do fancy stuff
    ...
    $foo = $something;
}
> 
> > > Don't call subs with & unless you need to (here you don't).
> > 
> > Isn't this another case of TMTOWTDI?
> 
> No, the two different ways of calling subs have different semantics (see
> perlsub).  Even if it makes no difference in this case, it will confuse
> someone else reading your code (they will spend time trying to work out
> why you needed the &-semantics).

OK, thanks, I just found the very useful www.perldoc.com-site,
I'll read up on perlsub there,
I was not aware of the differences

> 
> > > On older perls (pre-5.6), glob is performed by forking a csh(1). This is
> > > a Bad Thing, so it was replaced with File::Glob in 5.6.
> > 
> > But even if it is performed that way, why is the program running out
> > of file descriptors eventually?
> > Seems to me like a bug in the pre-5.6-glob() or File::Copy::move then.
> 
> Yes, it does; unless you are performing many globs simultaneously?
No, my program doesn't. Just one glob at a time.

> 
> > But File::Glob isn't available right now (pre-5.6 perl as it is),
> > so I figure I could do it this way:
> 
> But you can install File::Glob from CPAN and use that. It is a much
> better solution. (See the FAQ if you don't have root to install new
> modules globally.)
> 

<snip>
> > It's not a perfect solution, but will at least make the number of
> > glob()-call much less.
> 
> There is still a bug waiting to bite, though.

But fewer bugbites are better than many, in my opinion ... ;)

> 
> > I guess it won't help much if the real problem is the
> > File::Copy::move-call
> 
> Just for the hell of it, you could try installing the latest version of
> File::Copy from CPAN as well.
> 
> Ben

Actually, I convinced the systems owner to install and use 5.8.0,
I'll see what happens then.


------------------------------

Date: Fri, 5 Mar 2004 15:08:09 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: What with this open file descriptor/"Read on closed filehandle " stuff?
Message-Id: <c2a54p$dhv$1@wisteria.csv.warwick.ac.uk>


uffesterner@spamhole.com (Rex Gustavus Adolphus) wrote:
> Ben Morrow <usenet@morrow.me.uk> wrote in message
> news:<c27aco$cmg$2@wisteria.csv.warwick.ac.uk>...
> > [please wrap attribution lines at 72 columns]
> 
> Sorry, english is not my first language, what's an "attribution line"?
> If it's the "References: <c70a85ff.0402200341..."-stuff at the start
> of my posts,
> it's nothing I can help, Google adds that when I post (I'm using
> Google to read/post usenet).

No, it's the 'Ben Morrow <usenet@morrow.me.uk> wrote...' line. The stuff
in the header (the bit above the first blank line) you need not worry
about; the stuff in the body (the rest of your article) should be
wrapped at 72 columns.

> OK.
> A related question, is it in any way inefficient to redeclare
> variables,

Don't even ask. You should not be thinking about efficiency over clarity
until you have written your program and found it to be too slow; then
you should profile it and only optimize the bits that are actually
slowing it down.

> > > It's not a perfect solution, but will at least make the number of
> > > glob()-call much less.
> > 
> > There is still a bug waiting to bite, though.
> 
> But fewer bugbites are better than many, in my opinion ... ;)

Well, yes; but when there is a solution with no bugs (use File::Glob)
this is better still.

Ben

-- 
$.=1;*g=sub{print@_};sub r($$\$){my($w,$x,$y)=@_;for(keys%$x){/main/&&next;*p=$
$x{$_};/(\w)::$/&&(r($w.$1,$x.$_,$y),next);$y eq\$p&&&g("$w$_")}};sub t{for(@_)
{$f&&($_||&g(" "));$f=1;r"","::",$_;$_&&&g(chr(0012))}};t    # ben@morrow.me.uk
$J::u::s::t, $a::n::o::t::h::e::r, $P::e::r::l, $h::a::c::k::e::r, $.


------------------------------

Date: 5 Mar 2004 07:20:19 -0800
From: uffesterner@spamhole.com (Rex Gustavus Adolphus)
Subject: Re: What with this open file descriptor/"Read on closed filehandle " stuff?
Message-Id: <c70a85ff.0403050720.49da8327@posting.google.com>

Ben Morrow <usenet@morrow.me.uk> wrote:
> 
> > > Don't call subs with & unless you need to (here you don't).
> > 
> > Isn't this another case of TMTOWTDI?
> 
> No, the two different ways of calling subs have different semantics (see
> perlsub).  Even if it makes no difference in this case, it will confuse
> someone else reading your code (they will spend time trying to work out
> why you needed the &-semantics).
> 

Well, now I looked in perlsub.
Quoting:
"
    NAME(LIST);	   # & is optional with parentheses.
    NAME LIST;	   # Parentheses optional if predeclared/imported.
    &NAME(LIST);   # Circumvent prototypes.
    &NAME;	   # Makes current @_ visible to called subroutine.  
"
and:
"A subroutine may be called using an explicit & prefix. The & is
optional in modern Perl..."

The second quote seems to contradict the first partly.

The first I dont understand fully.
I think I understand
&NAME;	   # Makes current @_ visible to called subroutine.  
that's the same as
NAME(@_);
right?

But I don't understand the difference between
NAME(LIST);	   # & is optional with parentheses.
and
&NAME(LIST);   # Circumvent prototypes.

could you clarify that for me?


------------------------------

Date: Fri, 5 Mar 2004 16:24:57 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: What with this open file descriptor/"Read on closed filehandle " stuff?
Message-Id: <c2a9kp$gov$1@wisteria.csv.warwick.ac.uk>


uffesterner@spamhole.com (Rex Gustavus Adolphus) wrote:
> Well, now I looked in perlsub.
> Quoting:
> "
>     NAME(LIST);	   # & is optional with parentheses.
>     NAME LIST;	   # Parentheses optional if predeclared/imported.
>     &NAME(LIST);   # Circumvent prototypes.
>     &NAME;	   # Makes current @_ visible to called subroutine.  
> "
> and:
> "A subroutine may be called using an explicit & prefix. The & is
> optional in modern Perl..."
> 
> The second quote seems to contradict the first partly.

Yesss.... the first is right, though. The two forms have different
semantics.

> The first I dont understand fully.
> I think I understand
> &NAME;	   # Makes current @_ visible to called subroutine.  
> that's the same as
> NAME(@_);
> right?

No. Firstly, it would be the same as &NAME(@_): prototypes are still ignored.
Secondly, consider

perl -le'sub a { &b(@_); print @_; &b; print @_ }
         sub b { push @_, "b" }
         a("a")'
a
ab

 . The first call copies the current @_ into b's @_; the second actually
gives b the *same* @_ as a had, so modifying it will modify a's. This
sort of strange-action-at-a-distance effect is very bad for
maintainability.

> But I don't understand the difference between
> NAME(LIST);	   # & is optional with parentheses.
> and
> &NAME(LIST);   # Circumvent prototypes.
> 
> could you clarify that for me?

This is because you don't understand prototypes :). If you want to, read
the section on prototypes until you understand them; in the meanwhile,
suffice it to say that calling a sub with a prototype with & causes its
arguments to be parsed in a different way, and that the author of the
sub will not have been expecting this. Thus, anything might happen: you
should be sure you know what the effects will be before you do it. It
says somewhere in perlsub that this effect is left in as a way of
allowing experts to cheat if they need to: so yes, TMTOWTDI, but they do
different things :).

Ben

-- 
Like all men in Babylon I have been a proconsul; like all, a slave ... During
one lunar year, I have been declared invisible; I shrieked and was not heard,
I stole my bread and was not decapitated.
~ ben@morrow.me.uk ~                   Jorge Luis Borges, 'The Babylon Lottery'


------------------------------

Date: Fri, 5 Mar 2004 10:11:37 -0500
From: Paul Lalli <ittyspam@yahoo.com>
Subject: Re: What with this open file descriptor/"Read on closed filehandle " stuff?
Message-Id: <20040305100846.Y27834@dishwasher.cs.rpi.edu>

On Fri, 5 Mar 2004, Rex Gustavus Adolphus wrote:

> OK.
> A related question, is it in any way inefficient to redeclare
> variables,
> as in ex 2 below:
> ex 1:
> my $foo;
> while ($this_is_true) {
>     $foo = 0;
>     ...
>     # do fancy stuff
>     ...
>     $foo = $something;
> }
>
> ex 2:
> while ($this_is_true) {
>     my $foo = 0;
>     ...
>     # do fancy stuff
>     ...
>     $foo = $something;
> }

Efficiency isn't the issue here.  These two programs are doing something
different.  The first one is declaring $foo's scope to be the whole file;
the second one's scope is only within the while loop.  In other words, if
you put
print "$foo\n";
after the } that closes the while loop, the first script will print the
value $foo had at the last iteration of the while.  The second script will
print the undef value (or, if you've enabled strictures, will give you a
compile-time error.

Make sure you know what you're telling your script to do before you worry
about how efficient it is.

Paul Lalli


------------------------------

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 6223
***************************************


home help back first fref pref prev next nref lref last post