[24909] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 7159 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Sep 21 21:23:20 2004

Date: Tue, 21 Sep 2004 18:21:56 -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, 21 Sep 2004     Volume: 10 Number: 7159

Today's topics:
        In search of elegant code: Combining two statements int (David Filmer)
        magical goto in __WARN__ handler <dog@dog.dog>
    Re: magical goto in __WARN__ handler (Anno Siegel)
        Module::Install:CustomInstallationPath 0.10 released <newspost@coppit.org>
        Module::Install:GetProgramLocations 0.10.0 released <newspost@coppit.org>
        NDBM module for ActiveState/Winxx? <corff@cis.fu-berlin.de>
    Re: NDBM module for ActiveState/Winxx? <see@sig.invalid>
    Re: NDBM module for ActiveState/Winxx? <corff@cis.fu-berlin.de>
    Re: NDBM module for ActiveState/Winxx? <corff@cis.fu-berlin.de>
    Re: NDBM module for ActiveState/Winxx? <see@sig.invalid>
    Re: Need more efficient use of the substitution operato (Niall Macpherson)
    Re: Need more efficient use of the substitution operato (Niall Macpherson)
        Net::SMTP <cdurand@telebec.com>
    Re: Net::SMTP <anfi@priv.onet.pl>
        new commands written in perl (wana)
    Re: new commands written in perl <rob_gamble99@hotmail.com>
    Re: new commands written in perl <emschwar@pobox.com>
    Re: new commands written in perl <emschwar@pobox.com>
    Re: new commands written in perl <zebee@zip.com.au>
    Re: new commands written in perl <mritty@gmail.com>
    Re: new commands written in perl <emschwar@pobox.com>
        open3 and signals (Mike)
        OT Re: perl in crontab <scobloke2@infotop.co.uk>
        OT: Re: Replacing spaces <scobloke2@infotop.co.uk>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 21 Sep 2004 16:44:37 -0700
From: IneverReadAnythingSentToMe@hotmail.com (David Filmer)
Subject: In search of elegant code: Combining two statements into one (DBI)
Message-Id: <e4c916dd.0409211544.23499977@posting.google.com>

(I'm gonna use a DBI query as an example, but this question is really
a Perl syntax inquiry (how to add elegance to a bit of code), not a
database question).

Suppose I have a database table "department" with a simple structure
such as:

   id     integer primary key   #might not be sequential because of
deletes
   name   varchar

I want to build a hash (%dept) from the database such that (for
example) $dept{2} eq "Accounting".

I access the database:

   $sth = $dbh->prepare("SELECT id, name FROM department");
$sth->execute;

Now, I COULD do this:

   $dept_ref = %{$sth->fetchall_hashref('id')};

Which gives me a situation where $dept_ref->{2}->{'name'} eq
"Accounting" - not really very close to the data structure I want (a
simple hash). So I COULD, instead, populate a hash like this:

   %dept = %{$sth->fetchall_hashref('id')};

That gets me closer - now $dept{2}{'name'} eq "Accounting." DBI does
this, of course, because I might be selecting multiple columns. But,
in this case, I'm only selecting a key/value pair from the database
and would like to go directly to a simple hash. But I need an EXTRA
STEP to get to my goal - something like this:

   $dept{$_} = $dept{$_}{'name'} for keys %dept; #now $dept{2} eq
'Accounting'

That got me what I wanted, but it's not elegant - I had to populate
the hash and then manipulate it. Is there an elegant way I can express
the subroutine call (in this case to fetchall_hashref) so that it does
this in just one step?


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

Date: Mon, 20 Sep 2004 13:26:02 +0200
From: "Peter Michael" <dog@dog.dog>
Subject: magical goto in __WARN__ handler
Message-Id: <cimd0s$o01@news-1.bank.dresdner.net>

Hi,

is the use of magical "goto" deprecated in __WARN__ and 
__DIE__ handlers?

Consider the following simple attempt to provide stack traces for all
warnings:

    use Carp ();
    local $SIG{__WARN__} = sub
    {   unshift @_, "[stack trace by warn handler] ";
        goto &Carp::cluck;
    };

When calling warn() this results in an endless loop.

The "goto" is intended to hide the calling of the handler in order not
to appear in the stack trace produced by cluck(). Carp::cluck() uses
warn() itself to generate the message, but the handler is disabled during
its call.

perlfunc says about goto: "Instead, it exits the current subroutine
(losing any changes set by local()) and immediately calls in its place
the named subroutine using the current value of @_."

From the wording "calls in its place" I infer that Carp::cluck becomes
$SIG{__WARN__} at this point having the same effect as

    local $SIG{__WARN__} = \&Carp::cluck;

except for manipulating @_ before.

However "goto" seems to revert the effect of disabling the handler
during its call. Why (and how) is this done?

I am grateful for any explanations and hints.

Regards,

    Peter



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

Date: 20 Sep 2004 12:43:03 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: magical goto in __WARN__ handler
Message-Id: <cimj8n$t1i$1@mamenchi.zrz.TU-Berlin.DE>

Peter Michael <dog@dog.dog> wrote in comp.lang.perl.misc:
> Hi,
> 
> is the use of magical "goto" deprecated in __WARN__ and 
> __DIE__ handlers?
> 
> Consider the following simple attempt to provide stack traces for all
> warnings:
> 
>     use Carp ();
>     local $SIG{__WARN__} = sub
>     {   unshift @_, "[stack trace by warn handler] ";
>         goto &Carp::cluck;
>     };
> 
> When calling warn() this results in an endless loop.
> 
> The "goto" is intended to hide the calling of the handler in order not
> to appear in the stack trace produced by cluck(). Carp::cluck() uses
> warn() itself to generate the message, but the handler is disabled during
> its call.
>
> perlfunc says about goto: "Instead, it exits the current subroutine
> (losing any changes set by local()) and immediately calls in its place
> the named subroutine using the current value of @_."

I'm not too surprised by that behavior.  It seems the flag that
disables the handler follows "local" semantics and is reset on
"goto &...".

As a workaround you could call Carp::longmess (in a normal call)
and clean up the unwanted stack frame.

Anno


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

Date: Tue, 14 Sep 2004 22:59:55 GMT
From: David Coppit <newspost@coppit.org>
Subject: Module::Install:CustomInstallationPath 0.10 released
Message-Id: <I49Dtt.KKD@zorch.sf-bay.org>

Description:
- Module::Install::CustomInstallationPath is a Module::Install extension that
   allows the user to interactively specify custom installation directories

Download:
- You can download the module from CPAN:
   http://www.cpan.org/authors/id/D/DC/DCOPPIT/Module-Install-CustomInstallationPath-0.10.tar.gz
- Until the file propagates to the mirrors, you can use the following URL:
   http://coppit.org/code/Module-Install-CustomInstallationPath-0.10.tar.gz

Changes:
- This is the first release

See the README for additional notes.

Regards,
David




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

Date: Wed, 15 Sep 2004 22:51:05 GMT
From: David Coppit <newspost@coppit.org>
Subject: Module::Install:GetProgramLocations 0.10.0 released
Message-Id: <I49Du6.1409@zorch.sf-bay.org>

Description:
- Module::Install::GetProgramLocations is a Module::Install extension that
   allows the user to interactively specify the location of programs needed by
   the module to be installed

Download:
- You can download the module from CPAN:
   http://www.cpan.org/authors/id/D/DC/DCOPPIT/Module-Install-GetProgramLocations-0.10.0.tar.gz
- Until the file propagates to the mirrors, you can use the following URL:
   http://coppit.org/code/Module-Install-GetProgramLocations-0.10.0.tar.gz

Changes:
- This is the first release

See the README for additional notes.

Regards,
David




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

Date: 19 Sep 2004 12:51:10 GMT
From: <corff@cis.fu-berlin.de>
Subject: NDBM module for ActiveState/Winxx?
Message-Id: <2r5dluF16a212U1@uni-berlin.de>

Dear all,

I'd like to tie a flatfile db to an array but the NDBM module which
is supposed to offer this type of functionality does not come with
AS Perl for Windows (on Un*x systems Perl offers it, though).

Is there any way to convince SDBM (the only DBM module shipped with
AS Perl) to tie arrays instead of hashes to external db files?

Thanks for any pointers to information,

Oliver.

-- 
Dr. Oliver Corff              e-mail:    corff@zedat.fu-berlin.de


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

Date: Sun, 19 Sep 2004 19:35:35 -0400
From: Bob Walton <see@sig.invalid>
Subject: Re: NDBM module for ActiveState/Winxx?
Message-Id: <414E17C7.6080601@sig.invalid>

corff@cis.fu-berlin.de wrote:

 ...


> I'd like to tie a flatfile db to an array but the NDBM module which
> is supposed to offer this type of functionality does not come with
> AS Perl for Windows (on Un*x systems Perl offers it, though).
> 
> Is there any way to convince SDBM (the only DBM module shipped with
> AS Perl) to tie arrays instead of hashes to external db files?


No.  But check out the DB_File module.

See:

    perldoc DB_File

for how to tie it to an array.  SDBM and the other DBM-type files don't 
work with arrays, only hashes.

You could also check out the Tie::File module, which will let you tie a 
Perl array to the lines of an ordinary text file.  I don't know about 
the performance, though -- probably not in the same league as DB_File.

 ...


> Oliver.

-- 
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl



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

Date: 20 Sep 2004 05:58:28 GMT
From: <corff@cis.fu-berlin.de>
Subject: Re: NDBM module for ActiveState/Winxx?
Message-Id: <2r79s4F16up0tU2@uni-berlin.de>

Bob Walton <see@sig.invalid> wrote:


: See:

:     perldoc DB_File


Doesn't work in the target installation with AS Perl 5.8.0 on WinXP.

: for how to tie it to an array.  SDBM and the other DBM-type files don't 
: work with arrays, only hashes.

Right, and things get inflated with unnecessary overhead.

: You could also check out the Tie::File module, which will let you tie a 
: Perl array to the lines of an ordinary text file.  I don't know about 
: the performance, though -- probably not in the same league as DB_File.


This is a very good idea. I'll try this one as it is available.

Will report in a few days as I have an important presentation tomorrow
and the day after tomorrow.

Thanks,

Oliver.

-- 
Dr. Oliver Corff              e-mail:    corff@zedat.fu-berlin.de


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

Date: 20 Sep 2004 06:24:00 GMT
From: <corff@cis.fu-berlin.de>
Subject: Re: NDBM module for ActiveState/Winxx?
Message-Id: <2r7bc0F17b1g9U1@uni-berlin.de>

Bob Walton <see@sig.invalid> wrote:

: > Is there any way to convince SDBM (the only DBM module shipped with
: > AS Perl) to tie arrays instead of hashes to external db files?

:     perldoc DB_File

Not available on Windows XP AS Perl.

: You could also check out the Tie::File module, which will let you tie a 
: Perl array to the lines of an ordinary text file.  I don't know about 
: the performance, though -- probably not in the same league as DB_File.


I'll quote from http://perl.plover.com/TieFile/why-not-DB_File:

DB_File is only installed by default if you already have the db
library on your system; Tie::File is pure Perl and is installed by
default ...

As far as the performance is concerned, I think that for all MY concerns,
Tie::File is much more practical than DB_File as it only keeps in memory
the line/record I am working on, so things should actually speed up at
start and end of program runs.

I'll report more.

Oliver.

-- 
Dr. Oliver Corff              e-mail:    corff@zedat.fu-berlin.de


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

Date: Mon, 20 Sep 2004 20:32:04 -0400
From: Bob Walton <see@sig.invalid>
Subject: Re: NDBM module for ActiveState/Winxx?
Message-Id: <414F7684.1070308@sig.invalid>

corff@cis.fu-berlin.de wrote:

> Bob Walton <see@sig.invalid> wrote:
> 
> : > Is there any way to convince SDBM (the only DBM module shipped with
> : > AS Perl) to tie arrays instead of hashes to external db files?
> 
> :     perldoc DB_File
> 
> Not available on Windows XP AS Perl.


Hmmmmm...from a generic install of ActiveState Perl 5.8.4 on Windows XP sp2:

     C:\>perl -MDB_File -e "print 'hi'"
     hi
     C:\>

Or do you mean "perldoc DB_File" isn't available?  But that is also:

C:\>perldoc DB_File

NAME
     DB_File - Perl5 access to Berkeley DB version 1.x

SYNOPSIS
      use DB_File;

      [$X =] tie %hash,  'DB_File', [$filename, $flags, $mode, $DB_HASH] ;
      [$X =] tie %hash,  'DB_File', $filename, $flags, $mode, $DB_BTREE ;
      [$X =] tie @array, 'DB_File', $filename, $flags, $mode, $DB_RECNO ;
 ....


> 
> : You could also check out the Tie::File module, which will let you tie a 
> : Perl array to the lines of an ordinary text file.  I don't know about 
> : the performance, though -- probably not in the same league as DB_File.
> 
> 
> I'll quote from http://perl.plover.com/TieFile/why-not-DB_File:
> 
> DB_File is only installed by default if you already have the db
> library on your system; Tie::File is pure Perl and is installed by
> default ...


I think "plover"'s comments are referring to Unix systems.  Recent AS 
installs come with everything needed for DB_File.  I'm not sure how 
Tie::File is implemented -- since it deals with an ordinary text file, 
it needs to rewrite the entire file if any given line changed length 
during a modification.  For large files (where tied hashes to DBM-type 
files are useful), rewriting the entire file has to be time-consuming. 
I have found tied hashes are very advantageous for typical CGI programs 
which need to open/access/modify/close quickly on very large sets of 
data, but where the complexity of a full-blown external database and its 
connection overhead is not required (and yes, I know about mod_perl to 
avoid that overhead).


> 
> As far as the performance is concerned, I think that for all MY concerns,
> Tie::File is much more practical than DB_File as it only keeps in memory
> the line/record I am working on, so things should actually speed up at
> start and end of program runs.


Well, Tie::File does a vastly different thing than using a hash tied to 
a DBM-type file.  If you need to look up the record with key "foo", the 
tied hash can do that in short order, sometimes in perhaps two disk 
accesses.  With an array, you would need to search through the records 
until you found the one with key "foo".  You haven't really said what 
you need to do with your file contents -- if they are stored by a record 
number index, you could look them up using Tie::File -- although 
searching for record 1000000 will require that all the records from 1 
through 1000000 be read (since there is no foreknowledge of how long 
each record is), which would not be a fast process.

Also note that Tie::File (in its docs) states that by default the 
*entire file* following that record is written every time a record is 
changed.  Deferred writing can be turned on, but even then the entire 
file following the first modified record is written when the connection 
is shut down.  So if record 0 of a 1000000 record file is changed, the 
entire file is rewritten.

Yet another alternative is to use fixed-length records in a file.  Then 
the offset into the file is computable, and one may use seek() and 
tell() to hop around as one pleases.  And one need not even waste space 
on record separator characters.  Of course, that only works if the 
length of the biggest record to be stored has a reasonable upper bound 
which isn't much bigger than the typical record length.


> I'll report more.
> 
> Oliver.

-- 
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl



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

Date: 20 Sep 2004 00:56:19 -0700
From: niall.macpherson@emea.telerate.com (Niall Macpherson)
Subject: Re: Need more efficient use of the substitution operator
Message-Id: <9d1d054d.0409192356.eee1464@posting.google.com>

Michael Slass <miknrene@drizzle.com> wrote in message news:<m34qlw6a9n.fsf@eric.rossnet.com>...
> Is there a differnce in regex efficiency between the non-greedy ".*?" as
> used above, and the more specific "[^]]*"  ?  I can't remember the
> backtracking rules for NFA non-greedy quantifiers, and my Mastering
> Regular Expressions is out on loan.

This 'Mastering Regular Expressions' book sounds useful - this is
presumably the O'Reilly book by Jeffrey Freidl ? Think I had better
get myself a copy. Is there much Perl related stuff in this book ?


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

Date: 20 Sep 2004 01:06:35 -0700
From: niall.macpherson@emea.telerate.com (Niall Macpherson)
Subject: Re: Need more efficient use of the substitution operator
Message-Id: <9d1d054d.0409200006.69187ae5@posting.google.com>

Abigail <abigail@abigail.nl> wrote in message news:<slrnckmfud.qm8.abigail@alexandra.abigail.nl>...
> 
> Well, it isn't clear what you want to return from:
> 
>     one [two [three] four] five.
> 
> Should it be
>    a) two [three] four
>    b) two [three
>    c) three
> 
> 

Sorry - should have made this clearer. I always want the text between
the first '[' and the first ']' (since anything inside the '[]' which
is non-alpha is invalid in my case ) so the answer would be b)


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

Date: Mon, 20 Sep 2004 16:55:45 -0400
From: "Christophe" <cdurand@telebec.com>
Subject: Net::SMTP
Message-Id: <2r8uekF176io2U1@uni-berlin.de>

Hello,

I used Net::SMTP for send Mail,

sub ProgSMTPUSER {
 my ($dest) = @_;
 my $destuser = $dest."\@test.com";
 my $from = "test\@telebec.com";
  my $smtp = Net::SMTP->new("$SrvExch");
  $smtp->mail($from);
  $smtp->to($destuser);
  $smtp->data();
      $smtp->datasend("To: $destuser\n");
      $smtp->datasend("From: $from\n");
      $smtp->datasend("Subject: RAPPORT VPN\n");
$smtp->datasend("\n");
   $smtp->dataend();
      $smtp->quit;
return;
}

Result :
From : test@test.com
Date : Neant
To : test@test.com
Subject :RAPPORT VPN

How for have the Date not equal NEANT ?




Bye
christophe




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

Date: Mon, 20 Sep 2004 23:15:39 +0200
From: Andrzej Adam Filip <anfi@priv.onet.pl>
Subject: Re: Net::SMTP
Message-Id: <cinhg4$87v$1@nemesis.news.tpi.pl>

Christophe wrote:
> I used Net::SMTP for send Mail,
> 
> sub ProgSMTPUSER {
>  my ($dest) = @_;
>  my $destuser = $dest."\@test.com";
>  my $from = "test\@telebec.com";
>   my $smtp = Net::SMTP->new("$SrvExch");
>   $smtp->mail($from);
>   $smtp->to($destuser);
>   $smtp->data();
>       $smtp->datasend("To: $destuser\n");
>       $smtp->datasend("From: $from\n");
>       $smtp->datasend("Subject: RAPPORT VPN\n");
> $smtp->datasend("\n");
>    $smtp->dataend();
>       $smtp->quit;
> return;
> }
> 
> Result :
> From : test@test.com
> Date : Neant
> To : test@test.com
> Subject :RAPPORT VPN
> 
> How for have the Date not equal NEANT ?

1) Good mail server should add missing "Date:" header in correct format.
If it does not and you date command supports -R (output RFC-2822 compliant 
date string) the try the line below:
	$smtp->datasend(`/bin/date -R`);
or make your script generate rfc822 date itself.

What mail server do you use ?

2) Why have not you used "inline document" ?

$smtp->data(<<"END");
To: $destuser
From: $from
Subject: RAPPORT VPN

END
$smtp->quit;

-- 
Andrzej [en:Andrew] Adam Filip anfi@priv.onet.pl anfi@xl.wp.pl
Home Page http://anfi.homeunix.net/ [ PageRank 6 ]


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

Date: 21 Sep 2004 10:57:47 -0700
From: ioneabu@yahoo.com (wana)
Subject: new commands written in perl
Message-Id: <bf0b47ca.0409210957.418fd081@posting.google.com>

I made up a new Linux command, but I don't know if it already existed.

I named my command 'into'.  It just appends a string to a file from
the command line.  You can use it if you need to write down a phone
number or a quick note and you don't want to go into vi.

usage: into [file] string

Here's the code:

#! /usr/bin/perl -w
# This script allows a quick note to be saved to file
use strict;
my ($len,$string,$file);
$len = scalar @ARGV;

if (($len > 2) || ($len < 1) || ($ARGV[0] eq "--help"))
{
print <<end_of_line
Usage: into [file] string
I recommend using 'single quotes' around string if more than one word.
Will create file with string or append string to file.
If no file specified, string is appended to ~/.into.
end_of_line
}
else
{
if ($len == 1)
{
$file = "$ENV{'HOME'}/.into";
$string = $ARGV[0];
}
else
{
$file = $ARGV[0];
$string = $ARGV[1];
}
open OUT, ">>$file";
print OUT "$string\n";
close OUT;
}
exit(0);


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

Date: Tue, 21 Sep 2004 14:21:50 -0400
From: Robert Gamble <rob_gamble99@hotmail.com>
Subject: Re: new commands written in perl
Message-Id: <pan.2004.09.21.18.21.50.399234@hotmail.com>

On Tue, 21 Sep 2004 10:57:47 -0700, wana wrote:

> I made up a new Linux command, but I don't know if it already existed.
> 
> I named my command 'into'.  It just appends a string to a file from
> the command line.  You can use it if you need to write down a phone
> number or a quick note and you don't want to go into vi.
> 
> usage: into [file] string

It already exists, it's called "echo string >> file"

Rob Gamble


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

Date: Tue, 21 Sep 2004 12:13:21 -0600
From: Eric Schwartz <emschwar@pobox.com>
Subject: Re: new commands written in perl
Message-Id: <etollf3cwxq.fsf@wilson.emschwar>

ioneabu@yahoo.com (wana) writes:
> I named my command 'into'.  It just appends a string to a file from
> the command line.

You mean, like

$ echo "some quick note" >> file

or

$ cat >>file
some multi-line
text
^D

?

-=Eric
-- 
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
		-- Blair Houghton.


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

Date: Tue, 21 Sep 2004 12:34:40 -0600
From: Eric Schwartz <emschwar@pobox.com>
Subject: Re: new commands written in perl
Message-Id: <etohdprcvy7.fsf@wilson.emschwar>

ioneabu@yahoo.com (wana) writes:
> #! /usr/bin/perl -w

Don't use -w; 'use warnings;' instead.

> # This script allows a quick note to be saved to file
> use strict;
> my ($len,$string,$file);

Don't declare these so early; wait until you need them.  If there's an
error, or the user requests help, you don't need $string or $file at
all.  (You don't need $len either, but more about that in a bit.)

> $len = scalar @ARGV;
> if (($len > 2) || ($len < 1) || ($ARGV[0] eq "--help"))
> {

This is a poorly named variable (len implies 'length', usually of
strings, not of arguments).  Also, the 'scalar' is unnecessary; the
scalar variable on the left-hand side of the assignment puts it into
scalar context already.

if (@ARGV > 2 or @ARGV < 1 or $ARGV[0] eq '--help')
{

Notice how I used single quotes there.  Some people will tell you it's
mandatory; I wouldn't go that far.  But as a general principle, I do
think it's helpful to only use double-quotes when you expect to
interpolate a variable in your string.

> print <<end_of_line

BTW, this is a bit misleading; it's not the end of the line that
you're stopping at, it's the end of the help text.  And most people
around here tend to use all caps for the end-of-heredoc marker.

Also, you're missing a semicolon after the print statement; this isn't
strictly required, since it's the only statement in a block, but what
if you decided to exit afterwards, and put an 'exit 1' at the end of
the block?

This should look like:

print <<EOHELP;

> Usage: into [file] string
> I recommend using 'single quotes' around string if more than one word.
> Will create file with string or append string to file.
> If no file specified, string is appended to ~/.into.
> end_of_line
> }

This is a stylistic thing; I would have put an exit at the end of the
if statement, and not bothered putting the else inside an explicit
else block.  To me, this clearly points out that the bits that follow
are the 'normal' operation of the program, and the 'if' statement is
just for exception handling.

Also, I just noticed-- fix your indentation, or lack thereof.
Indentation is a clue to your program's structure.  I like three
spaces inside a block, some people like four, others prefer 8.  I
don't care what you use, but use *some*, mmmkay?

> else
> {

Remember, whitespace is NOT endangered!  Use it!

> if ($len == 1)
> {
> $file = "$ENV{'HOME'}/.into";
> $string = $ARGV[0];
> }
> else
> {
> $file = $ARGV[0];
> $string = $ARGV[1];
> }

Indent!

At this point, I've forgotten what $len even means; I have to go look
for it.  This wouldn't normally happen for such a small program, but
I've written quite a few comments already, so my brain got full.  If
you swapped your command-line around so that it was

into string [file]

Your code could look like:

my ($string, $filename) = @ARGV;
$filename = "$ENV{HOME}/.into" unless $filename;

Even as it is, you could write this whole silly parsing block as 

my ($filename, $string) = @ARGV;
if (!defined $string) {  # only one argument passed in
  $string = $filename;
  $filename = "$ENV{HOME}/.into";
}

> open OUT, ">>$file";

Always always always ALWAYS check the return from an open()!  I cannot
emphasize this strongly enough.  You probably ought to check on
close() as well-- in fact, if you're going to be anal, you should
check the results of every function that references a system call.
But you wouldn't believe the sheer number of problems we have
uncovered in this newsgroup by virtue of people simply rewriting that
as

open OUT, ">>$file" or die "Couldn't open $file: $!";

Just Do It(tm).

> print OUT "$string\n";
> close OUT;

You can also do this with lexical filehandles, and the three-arg form
of open() (which I think it the cat's pyjamas):

open my $file, '>>', $filename or die "Couldn't open $filename: $!";
print $file $string,"\n";
close $file or die "Error closing $filename: $!";

> }
> exit(0);

Fine, but unnecessary.

-=Eric
-- 
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
		-- Blair Houghton.


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

Date: Tue, 21 Sep 2004 19:56:22 GMT
From: Zebee Johnstone <zebee@zip.com.au>
Subject: Re: new commands written in perl
Message-Id: <slrncl11fj.efc.zebee@zeus.zipworld.com.au>

In comp.lang.perl.misc on Tue, 21 Sep 2004 12:34:40 -0600
Eric Schwartz <emschwar@pobox.com> wrote:
> ioneabu@yahoo.com (wana) writes:
>> #! /usr/bin/perl -w
> 
> Don't use -w; 'use warnings;' instead.

Why?

> Notice how I used single quotes there.  Some people will tell you it's
> mandatory; I wouldn't go that far.  But as a general principle, I do
> think it's helpful to only use double-quotes when you expect to
> interpolate a variable in your string.

Why?  The main reason I've found is that copying and pasting into a
print statement is made easier.



Zebee

-- 
Zebee Johnstone (zebee@zip.com.au), proud holder of
aus.motorcycles Poser Permit #1.
"Motorcycles are like peanuts... who can stop at just one?"


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

Date: Tue, 21 Sep 2004 20:03:50 GMT
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: new commands written in perl
Message-Id: <GO%3d.3082$Ec4.502@trndny04>

"Zebee Johnstone" <zebee@zip.com.au> wrote in message
news:slrncl11fj.efc.zebee@zeus.zipworld.com.au...
> In comp.lang.perl.misc on Tue, 21 Sep 2004 12:34:40 -0600
> Eric Schwartz <emschwar@pobox.com> wrote:
> > ioneabu@yahoo.com (wana) writes:
> >> #! /usr/bin/perl -w
> >
> > Don't use -w; 'use warnings;' instead.
>
> Why?

Because -w is a global switch.  Once you use it, you can't refine it.

use warnings; on the other hand, is a lexical pragma.  You can turn it
off for certain sections of the code if you need to.  For example:

use warnings;
print "Here I am!\n";
my $str = "23foo";
my $num;
{
    no warnings 'numeric';
    $num = $str + 3;
}

In that block, you're effectively saying you *know* what you're about to
do is going to cause a "$str is not numeric" warning, and you're okay
with that.  There are times when you're coding that you might want to do
something that would generate a warning.  This way, you can shut off
that warning.

> > Notice how I used single quotes there.  Some people will tell you
it's
> > mandatory; I wouldn't go that far.  But as a general principle, I do
> > think it's helpful to only use double-quotes when you expect to
> > interpolate a variable in your string.
>
> Why?  The main reason I've found is that copying and pasting into a
> print statement is made easier.

There are three schools of thought on this.

One school holds that you should always use single quotes, unless you
need to do interpolation.  That way, when looking over your code, you
have a clear signal that the string you're looking at will contain a
variable you might need to do something with.
The second holds that you should aways use double quotes, unless you
very specifically do *not* want interpolation.  That way, when looking
over your code, you're given a clear signal that the string you're
looking at does not contain any variables.
The third school of thought says "whatever" and does whatever it feels
like at any given moment.

Paul Lalli




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

Date: Tue, 21 Sep 2004 16:13:09 -0600
From: Eric Schwartz <emschwar@pobox.com>
Subject: Re: new commands written in perl
Message-Id: <etoacvjclu2.fsf@wilson.emschwar>

Zebee Johnstone <zebee@zip.com.au> writes:
> In comp.lang.perl.misc on Tue, 21 Sep 2004 12:34:40 -0600
> Eric Schwartz <emschwar@pobox.com> wrote:
>> ioneabu@yahoo.com (wana) writes:
>>> #! /usr/bin/perl -w
>> 
>> Don't use -w; 'use warnings;' instead.
>
> Why?

The warnings pragma catches slightly more things, I believe, but the
big trick is that you can turn off certain warnings selectively; with
-w you can clear $^W to turn them all off, or set it again to turn
them all back on, but that's it.

>> Notice how I used single quotes there.  Some people will tell you it's
>> mandatory; I wouldn't go that far.  But as a general principle, I do
>> think it's helpful to only use double-quotes when you expect to
>> interpolate a variable in your string.
>
> Why?  The main reason I've found is that copying and pasting into a
> print statement is made easier.

If you see 'string', you know there can't be any interpolation inside
it.  If you see "string", there can be.  If you always get in the
habit of using 'string' unless you specifically want interpolation,
then it's just one more small clue that people reading your code can
use to help decipher it.  

This follows from the basic principle that Writing Code Is Easy;
Reading Code is Hard (I think I stole that from Damian Conway; I'm not
sure).  If you do everything you can to make reading your code easier,
it'll make your code more maintainable and useful.  Sure, quotes are
not THAT big a deal, which is why I'm not an absolutist about them.
But they can provide some help you didn't have before.

-=Eric
-- 
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
		-- Blair Houghton.


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

Date: 20 Sep 2004 19:43:05 -0700
From: jmw_misc@hotmail.com (Mike)
Subject: open3 and signals
Message-Id: <e64d100e.0409201843.7a4304ed@posting.google.com>

Hi,
I am using open3 to run an external program from my perl script.
Whenever the script recieves a Ctrl-C, it appaers the open pipes are
immediately shutdown. I have installed a signal handler for INT, which
sets a variable. I then immediately leave a while loop and try to send
(CLOSE\nEXIT\n") to the write handle for the open process. But it is
already closed.

Looking for any ideas to try and write the message before the process
is shudown.

Thanks
Mike


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

Date: Mon, 20 Sep 2004 22:06:22 +0000 (UTC)
From: Ian Wilson <scobloke2@infotop.co.uk>
Subject: OT Re: perl in crontab
Message-Id: <cink8u$q5e$2@hercules.btinternet.com>

news.hinet.net wrote:

> This test can work when i run it anywhere.
> But the chdir seem does not work when i set it in crontab??
> ==============================================
> #!/usr/bin/perl
> 
> chdir("/usr/local/admin/dir");
> system("tar zcvf test.tgz *");
> 
> 
Also man tar, many tar implementations have a command line option for 
setting the initial directory for the operation.


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

Date: Mon, 20 Sep 2004 21:49:25 +0000 (UTC)
From: Ian Wilson <scobloke2@infotop.co.uk>
Subject: OT: Re: Replacing spaces
Message-Id: <cinj95$q5e$1@hercules.btinternet.com>

Aristotle wrote:

> I'm trying to replace spaces at the beggining of a string, with
> "&nbsp;" .
> Not all spaces by a single &nbsp; , but rather each space by a single
> "&nbsp;"
> 
> eg 
> "    make love not war" --> "&nbsp;&nbsp;&nbsp;&nbsp;make love not
> war"
> "  follow the white rabbit" --> "&nbsp;&nbsp;follow the white rabbit"
> 
> ie "&nbsp;" should replace only the beggining spaces (one by one), but
> not other spaces.
> 
> The way i'm doing this for now is 
> 
>    	$string =~ s/ /\&nbsp\;/g; 
>  	$string  =~ s/([A-Za-z])\&nbsp\;([A-Za-z])/$1 $2/g;
> 
> ie, first replacing all spaces with &nbsp; , then replacing again
> those between two words. It gets the job somewhat done (a bit
> inefficiently, since if there are other characters within the string
> (like ",.-:;" ) the "&nbsp;" arent being replaced).
> 
> If i try to use $string =~ s/^\s+/\&nbsp\;/; then all beggining spaces
> are being replaced by a single "&nbsp;", while what i need is the
> number of "&nbsp;" to match the number of spaces at the beggining of
> the string.
> 
> I'd appreciate your help on this.
> 
> Thank you in advance.

I'd be using <pre> or <blockquote>, probably with some CSS. The 
non-breaking space isn't intended for indentation and, to me, looks an 
ugly way of achieving that.

YMMV :-)



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

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


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