[15523] in Perl-Users-Digest
Perl-Users Digest, Issue: 2933 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed May 3 14:05:36 2000
Date: Wed, 3 May 2000 11:05:21 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <957377121-v9-i2933@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Wed, 3 May 2000 Volume: 9 Number: 2933
Today's topics:
Re: *** To retrieve remote files regularly? (remove this part)
A debugging question (jkroger)
about proxy <97909585d@polyu.edu.hk>
Re: about proxy <tony_curtis32@yahoo.com>
Cleaning Up Strings <skpurcell@hotmail.com>
Re: Clear and then Reuse a package name space <rootbeer@redcat.com>
Re: Conditional use ? - IGNORE... (R1CH)
Conditional use ? (R1CH)
Re: Conditional use ? <blah@nospam.com>
Re: Conditional use ? <tony_curtis32@yahoo.com>
Re: Conditional use ? <blah@nospam.com>
Re: Conditional use ? nobull@mail.com
Re: Creating IIS V4 virtual directory using Perl... <rootbeer@redcat.com>
Re: Creating IIS V4 virtual directory using Perl... <john.alderson@fmr.com>
DBD::Oracle for MAC yogishb@hotmail.com
FYI : Compiling Perl to EXE (Windows or UNIX) exhacker@my-deja.com
how do I catch user's input? bing-du@tamu.edu
Re: How do I change the Color of a Link <mjcarman@home.com>
Re: How to define my own Perl Operator? (Greg Snow)
Re: How to test perl CGI scripts on my computer first? <bernie@fantasyfarm.com>
Re: if file already exists then remove... <htp@mac.com>
Re: if file already exists then remove... <lr@hpl.hp.com>
Re: if file already exists then remove... <htp@mac.com>
Re: If slices are so great... (M.J.T. Guy)
Re: If slices are so great... <perin@panix.com>
Re: If slices are so great... <mjcarman@home.com>
Installing modules with CPAN rscrane@my-deja.com
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 04 May 2000 00:08:27 +0800
From: I'm a good man <"goodman888"@hongkong.com(remove this part)>
Subject: Re: *** To retrieve remote files regularly?
Message-Id: <8epivd$mm8$1@news.ctimail.com>
Andy Jeffries wrote:
> It feels really strange answering a PHP question on a Perl newsgroup,
> but using PHP is easy, something like the following should do it:
Thanks a lot for your solution!
BTW, which are the newsgroup for discussings PHP? I cant find one with my
news server.....!
Regards,
Fung "http://i.am/goodman888/"
------------------------------
Date: Wed, 03 May 2000 12:30:05 -0400
From: jkrogerSPAMBLOCKER@earthlink.net (jkroger)
Subject: A debugging question
Message-Id: <jkrogerSPAMBLOCKER-0305001230050001@tritone.csbmb.princeton.edu>
I posted a request for pointers to debugger instructions. However, I do
have on question I'll go ahead and ask.
If your program requires input, for example,
print "How much would you like to invest in Microsoft? \n"
$invest = <STDIN>;
chomp $invest;
$losses = $invest;
print "You will lose $losses \n";
and I run perldebug, using s to step through the lines, when I get to the
second line, I will need to input something. When running the debugger,
though, it just steps through this line and $invest remains undefined. How
do I get the debugger to let me input the values where the program needs
them?
Thanks in advance,
Jim
------------------------------
Date: Thu, 04 May 2000 01:22:04 +0800
From: Kei <97909585d@polyu.edu.hk>
Subject: about proxy
Message-Id: <3910603B.C490BFFA@polyu.edu.hk>
Dear all
I have a perl program to get the webpages and extract the content
of it. Why does not the perl program work when the connection is
through proxy server? How can the perl program work through proxy
server?Thanks
------------------------------
Date: 03 May 2000 12:39:55 -0500
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: about proxy
Message-Id: <87og6nttw4.fsf@shleppie.uh.edu>
>> On Thu, 04 May 2000 01:22:04 +0800,
>> Kei <97909585d@polyu.edu.hk> said:
> Dear all I have a perl program to get the webpages and
> extract the content of it. Why does not the perl
> program work when the connection is through proxy
> server?
You haven't given any information here to alloow anyone to
tell you for sure, but presumably the reason you have a
proxy is to go through a firewall of some kind.
> How can the perl program work through proxy server?
perldoc LWP::UserAgent
hth
t
------------------------------
Date: Wed, 3 May 2000 12:44:18 -0500
From: "spurcell" <skpurcell@hotmail.com>
Subject: Cleaning Up Strings
Message-Id: <39106551$0$5144@wodc7nh1.news.uu.net>
I would like to make sure that there is not a better way to clean up some
garbage either in front or in back of the $filename and $id string in the
code below. Currently, I am removing either the front or back space or
newline, but in the example below, I am creating two for loops to do the
job. Is there anyway to knock this out in one sweep? Just knowing what I do
aboiut Perl, it looks a little convaluted and I believe it could be
simplified. ???
Thanks
Scott
### code below that I would like to simplify
my @hstring = @_;
foreach (@hstring) {
my ($filename, $id, $dbname) = split /SSPPLLIITT/, $_, 4;
for ($filename) {
s/^\s+//; s/\s+$//;
}
for ($id) {
s/^\s+//; s/\s+$//;
}
# do some more stuff here
------------------------------
Date: Wed, 3 May 2000 09:56:08 -0700
From: Tom Phoenix <rootbeer@redcat.com>
Subject: Re: Clear and then Reuse a package name space
Message-Id: <Pine.GSO.4.10.10005030911050.13677-100000@user2.teleport.com>
On Wed, 3 May 2000 richard_chen@my-deja.com wrote:
> One example is making legacy cgi scripts to work under modperl. There
> you can use Apache::Registry to wrap you cgi script as another package
> and this gets cached by apache server and used many times.
>
> Since each invocation of the script will involve completely different
> variables in a given specified package name space, it is absolutely
> essential to clear and then reuse the name space. Otherwise, one
> request's data will get used by the next request.
Well, the CGI program shouldn't be using globals except if it's intending
to leave data in memory for a future request. The manpage for
Apache::Registry should make this clear, but it doesn't.
> Sorry I did not give enough motivation for this. But I think this
> should be feasible in perl, given that it is such a mature language.
> The question is not how to do it another way, but if there is a way to
> do it head on.
Well, clearing the symbol table _is_ possible. But a part of the problem
in your situation is that, for efficiency reasons, clearing the symbol
table isn't enough to destroy a variable - the compiled code itself
can "remember" the variable.
#!/usr/bin/perl -wl
$fred::flintstone = "I'm still here";
undef %{fred::}; # Delete the fred:: symbol table
# This gets bound at compile time
print $fred::flintstone;
# But this won't be seen until runtime
eval 'print $fred::flintstone';
So, deleting a symbol table may not do you any good.
"Wait!" I can hear someone saying. "That eval complained that the variable
was undefined - so clearing the symbol table worked! Can't we just wrap
the code in an eval string?" Sure - but then you'd be re-compiling your
program every time it's called, throwing away the gains you could get by
using Apache::Registry.
If (instead of the simple undef above) I had used
Symbol::delete_package('fred'), I would have gotten the same result.
Despite the FAQ's implication to the contrary, the scrub_package() code
from perlfaq7 does something different - it hunts down the original value
and kills it. As you found, that wasn't what you wanted either. In this
example code, it deletes $barney::rubble, a variable imported from another
package.
#!/usr/bin/perl -wl
$barney::rubble = "I'm in another package";
# This aliases one variable to another, as done
# in the importer
*fred::buddy = \$barney::rubble;
print "Fred's buddy is $fred::buddy";
print "The original variable was $barney::rubble";
scrub_package('fred');
print "Fred's buddy is $fred::buddy";
print "The original variable was $barney::rubble";
sub scrub_package {
no strict 'refs';
my $pack = shift;
die "Shouldn't delete main package"
if $pack eq '' || $pack eq 'main';
my $stash = *{$pack . '::'}{HASH};
for my $name (keys %$stash) {
my $fullname = $pack . '::' . $name;
# Get rid of everything with that name
undef $$fullname;
undef @$fullname;
undef %$fullname;
undef &$fullname;
undef *$fullname;
}
}
Having said all that, I should make it clear that I don't know everything
about such topics. In fact, I think no one knows the whole picture,
although many are much better informed than I. If I've overlooked
something serious here, I hope that someone will say so.
But it looks to me as if programs written to work in Apache::Registry (or
similar environments) should use globals only when they intend to pass
those values along to be used in future requests.
Cheers!
--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: Wed, 03 May 2000 17:12:52 GMT
From: r1ch@n0sp4m.r1ch.net (R1CH)
Subject: Re: Conditional use ? - IGNORE...
Message-Id: <39105dff.68762755@news.screaming.net>
The friendly people in #perl helped :)
------------------------------
Date: Wed, 03 May 2000 16:32:50 GMT
From: r1ch@n0sp4m.r1ch.net (R1CH)
Subject: Conditional use ?
Message-Id: <39105437.66259165@news.screaming.net>
Hi,
I have a Perl program that is (supposed to) run on both MSWin32 Perl and
*nix flavors. However, I have some optional modules which are nice
features, but only work on the unixy version, if I try to use "mymodule"
in Win32 the program doesn't even start.
Is there any way I can do something like
if ($^O ne 'MsWin32') { use "mymodule"; }
Any help would be appreciated.
------------------------------
Date: Wed, 03 May 2000 18:45:19 +0200
From: Marco Natoni <blah@nospam.com>
Subject: Re: Conditional use ?
Message-Id: <3910579F.78B4C31C@nospam.com>
Hi R1CH,
R1CH wrote:
> I have a Perl program that is (supposed to) run on both MSWin32
> Perl and *nix flavors. However, I have some optional modules which
> are nice features, but only work on the unixy version, if I try to
> use "mymodule" in Win32 the program doesn't even start.
You should find the Config.pm module very actractive...
<code>
use Config;
if ($Config{'osname'} eq 'MSWin32') { use MyModule }
</code>
Best regards,
Marco
------------------------------
Date: 03 May 2000 11:51:21 -0500
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: Conditional use ?
Message-Id: <87r9bjtw52.fsf@shleppie.uh.edu>
>> On Wed, 03 May 2000 18:45:19 +0200,
>> Marco Natoni <blah@nospam.com> said:
> Hi R1CH, R1CH wrote:
>> I have a Perl program that is (supposed to) run on both
>> MSWin32 Perl and *nix flavors. However, I have some
>> optional modules which are nice features, but only work
>> on the unixy version, if I try to use "mymodule" in
>> Win32 the program doesn't even start.
> You should find the Config.pm module very
> actractive...
> use Config;
>
> if ($Config{'osname'} eq 'MSWin32') { use MyModule }
Unfortunately that still won't work. (Test code before
posting!)
The problem is that "use" is a compile-time directive and
the rest of the code won't happen until run-time.
See "perldoc -f use" about "BEGIN" (and "perldoc perlmod"
for more detail).
hth
t
------------------------------
Date: Wed, 03 May 2000 18:53:41 +0200
From: Marco Natoni <blah@nospam.com>
Subject: Re: Conditional use ?
Message-Id: <39105995.BA420B@nospam.com>
Hi Tony,
Tony Curtis wrote:
> Unfortunately that still won't work. (Test code before posting!)
Ooops... I have pointed my attention only to the first part of the
question! =)
Best regards,
Marco
------------------------------
Date: 03 May 2000 18:02:27 +0100
From: nobull@mail.com
Subject: Re: Conditional use ?
Message-Id: <u9hfcfbm8s.fsf@wcl-l.bham.ac.uk>
r1ch@n0sp4m.r1ch.net (R1CH) writes:
> Hi,
> I have a Perl program that is (supposed to) run on both MSWin32 Perl and
> *nix flavors. However, I have some optional modules which are nice
> features, but only work on the unixy version, if I try to use "mymodule"
> in Win32 the program doesn't even start.
>
> Is there any way I can do something like
> if ($^O ne 'MsWin32') { use "mymodule"; }
BEGIN {
if ($^O ne 'MsWin32') {
require mymodule;
import mymodule;
}
}
I don't use 5.6 but I seem to recall 5.6 allows:
use "mymodule" if $^O ne 'MsWin32';
I also see to recall seeing this question in the last week, maybe
that's where I saw the above.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Wed, 3 May 2000 08:14:54 -0700
From: Tom Phoenix <rootbeer@redcat.com>
Subject: Re: Creating IIS V4 virtual directory using Perl...
Message-Id: <Pine.GSO.4.10.10005030813420.13677-100000@user2.teleport.com>
On Wed, 3 May 2000, Shawn McKee wrote:
> Does anyone out there know a way, using Perl, to connect to the
> IIS metabase and add virtual directories and applications?
If you can find out what protocol that the "IIS metabase" likes for you to
speak to it, it shouldn't be hard. There are modules on CPAN for many
common protocols, and you can make your own. You probably want to search
for the docs, FAQs, and newsgroups about the "IIS metabase". Good luck
with it!
--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: Wed, 3 May 2000 09:47:50 -0400
From: "John Alderson - LTS" <john.alderson@fmr.com>
Subject: Re: Creating IIS V4 virtual directory using Perl...
Message-Id: <g6WP4.11$s65.50@news-srv1.fmr.com>
"Shawn McKee" <smckee@umich.edu> wrote in message
news:TxVP4.304$R12.5829@news.itd.umich.edu...
> I am trying to create a set of virtual directories in IIS V4 under WinNT
V4.
> The only examples of doing this that I have found involve either C++ code
or
> VBScript's GetObject function.
>
> I haven't been able to find a way to get Perl (or PerlScript) to
> do the equivalent of GetObject (CreateObject works fine though).
>
> Does anyone out there know a way, using Perl, to connect to the
> IIS metabase and add virtual directories and applications?
>
> I am using ActiveState build 522 and NT is SP6.
>
Shawn,
The best way to go about this task is to use ADSI via Win32::OLE. Your IIS
installation should have quite a few sample files installed. Search in
%SYSTEMROOT%\System32\Inetsrv\Adminsamples for files with a VBS extension.
These are written in VBScript, which can easily be translated into Perl.
You can also search msdn.microsoft.com for the IIS: namespace and ADSI.
John Alderson
------------------------------
Date: Wed, 03 May 2000 17:54:35 GMT
From: yogishb@hotmail.com
Subject: DBD::Oracle for MAC
Message-Id: <8epp4n$ftq$1@nnrp1.deja.com>
Hi All,
Did anyone worked on DBD::Oracle for MAC?
Any help on this would be greatly appreciated.
Thanx a lot,
-- Baliga
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Wed, 03 May 2000 16:13:49 GMT
From: exhacker@my-deja.com
Subject: FYI : Compiling Perl to EXE (Windows or UNIX)
Message-Id: <8epj7b$97d$1@nnrp1.deja.com>
Sorry about the promotion of a shareware application, but this solved a
lot of my problems. My problems were that, although all of my friends
have some version of Perl, they're not all the same. But with this
program (which has a 30-day trial) you can compile platform-specific
applications. I haven't played too much with the UNIX compiler, but it
works. The Windows compiler works fine after you remove the version
you have and install the version from ActiveState that's compatible.
If you use LWP modules in your scripts, then it requires one dll in
your windows/system dir. Otherwise, your exe is standalone. It also
allows TK to be used in your exe.
On one of my web-robot scripts that involved contacting http servers
repeatedly, the Executable version ran 56-58% *faster* than the Perl
version, and took up less memory (Perl takes up 4-8MB easily).
Here's the link: http://www.dynamicstate.com/perl2exe.htm
Hope this helps.
exhacker
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Wed, 03 May 2000 16:29:27 GMT
From: bing-du@tamu.edu
Subject: how do I catch user's input?
Message-Id: <8epk4f$agk$1@nnrp1.deja.com>
Greetings...
Here is a newbie question. If I want to make an interactive perl to
prompt user questions for answers, such as:
# test.pl
what's your name: <user's input>
what's your major: <user's input>
...
How should I refer those user's input in my script? Are there any
standard names for those variables? Basically, I want to collect
anwsers in a hash like this:
$user{$name} = <user's answer>
$user{$major} = <user's answer>
Your help would be greatly appreciated.
Bing
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Wed, 03 May 2000 10:26:02 -0500
From: Michael Carman <mjcarman@home.com>
Subject: Re: How do I change the Color of a Link
Message-Id: <3910450A.EADBC25F@home.com>
Drew Simonis wrote:
>
> sparky21@my-deja.com wrote:
> >
> > This seems simple, but I can't find the answer. All my web links
> > come up a blue. How in both html and with the cgi.pm mod do I do
> > this.
>
> This has nothing to do with CGI.pm and less to do with Perl.
Absolutely correct, but I'll be a sucker and answer it anyway.
> Your question is one of HTML tags and their attributes.
Specifically, the <BODY> tag, and the LINK, VLINK, and ALINK attributes.
Anytime you set any color you should set *all* of them, lest somebody
with different defaults than yours should see horrid combinations.
Therefore, find a good value for TEXT and BGCOLOR (or BACKGROUND if your
using an image) as well.
You might want to do a search for HTML references and tutorials.
Searching on "HTML" at www.google.com came up with several good ones on
the first page of matches.
-mjc
------------------------------
Date: 3 May 2000 17:38:47 GMT
From: snow@statsci.com (Greg Snow)
Subject: Re: How to define my own Perl Operator?
Message-Id: <8epo77$gmk$1@junior.statsci.com>
In article <AgHP4.34940$Xk2.128483@tor-nn1.netcom.ca>, k <none@none.ca> wrote:
>I would like to create an "in" operator which would test if a value is in an
>array.
>
>Here's how it would work:
>
> $car = "ford";
> @cars = ['toyota','ford'];
I don't think the above line is doing what you want it to do. It is
creating an anonymous array and storeing the reference to it in @cars, you
probably want () instead of [].
> print "something" if $car in @cars;
>
I have thought of doing something similar (but not implemented it yet)
the module Filter::Util::Call allows you to run your module through a
source filter (of your definition), or at least that's what it says it
does, I have not tried it. This means that you could set up a filter to
scan lines for a scalar followed by "in" followed by an array and replace
those with your function call, i.e. (untested)
s/($\S+) # get scalar
\s+ # followed by 1 or more whitespace
in # followed by the word "in"
(@\S+) # get array
/&in($1,$2)/gx
There are a few draw backs to this approach that I have thought of (I
would be interested in whether others think these drawbacks are enough to
not bother with this, or are they minor enough that this should be
pursued):
1) longer start-up time since the source has to be filtered before being
compiled (once you have a working version you could pre-filter it for the
version to actually run).
2) The above may do a replacement inside of a string or other construct
that would mess things up. You can get arround this somewhat by using
something less common than plain "in" that you don't expect to show up
anywhere else e.g. "!in!" or use some other punctuation that will not show
up often enough to cause problems.
3) if you don't want to limit yourself to the simple case, your filter
can get really complicated realy fast, what if you want the left argument
to be a function that returns a scalar? etc.
4) debbugging could be a pain because the code you see is not the code
being compiled.
5) Same drawbacks of overloading operators (I've seen major arguments
against allowing/using overloading). The intended purpose of doing
something like this is to make the script more readable, and it probably
will while you are doing the initial coding whith the definition fresh in
your mind, but what about a year from now when you are trying to update
the code, or if someone else is trying to read it. The subroutine call is
pretty straight forward, but seeing "in" may send someone looking through
perlop instead of the right direction.
>Is it possible to do this sort of thing without hacking the perl core?
It should be, but is it worth it is a question yet to be answered (I keep
going back and forth on this, any other thoughts out there?)
--
-------------------------------------------------------------------------------
Gregory L. Snow | Inertia makes the world go round,
(Greg) | Love makes the trip worth taking.
gsnow@splus.mathsoft.com |
------------------------------
Date: Wed, 03 May 2000 12:19:46 -0400
From: Bernie Cosell <bernie@fantasyfarm.com>
Subject: Re: How to test perl CGI scripts on my computer first?
Message-Id: <toj0hs02o4ed9i524h780158gmbn3ees9p@news.supernews.com>
"Tim Cuffel" <tcuffel@exactis.com> wrote:
} Bernie Cosell wrote in message ...
} >tadmc@metronet.com (Tad McClellan) wrote:
} >
} >} On Sun, 30 Apr 2000 07:05:15 -0400, Ryan & Treena Carrier <ryanc@nci1.net> wrote:
} >} >I'm trying to figure out how to set my computer up so I can write some CGI
} >} >scripts in Perl and test them on my computer before posting them to my
} >} >internet web server.
} >}
} >}
} >} Since you are using CGI.pm, you can get one level of testing
} >} by running your CGI program from the command line, and entering
} >} form values yourself (or redirect them from a file).
} >}
} >} To get the most realistic testing environment, you need to
} >} get a WWW server installed.
} >
} >Actually, I've found that you don't need the server at all [at least not a
} >local one] --- run the CGI, feeding it the environment/form/vbl stuff it
} >needs, collect the output as some suitable file ['cgiout.html' or some
} >such]. Then you can eyeball the HTML output, or just view it locally [I
} >believe that all browsers allow you to view 'local' HTML files directly,
} >without need of an actual server.
}
}
} And you test forms this way? How about multiple interacting pages?
Sure... forms are just big bunches of variables passed into the CGI
program... It can be a bit of a pain if you have 50 fields on your form,
but I haven't had that problem... usually I only need three or four fields
filled in or 'checked' and I can see what the CGI is doing...
And as for multiple interacting pages, I don't understand the problem: Yes,
often the output from one program is a form that has some other CGI program
as its 'submit' URL, but that's no problem. But still, at least in my
world CGIs proceed "one page at a time", so I get one working, then the
next...
Along these lines I once pondered writing a CGI.pm-feeder. That is, a
little program that'd parse through an HTML file, find all the various
input fields and prompt me for values for them, then run the associated CGI
program with the values 'filled in'... Sort of like a
super-mini-server/browser combination... I'm currently on CGI-hiatus
[working on other stuff], but if/when I get back in the CGI saddle maybe
I'll bang out something like this and CPAN it...
As mentioned in another reply, the only overall problem I've run into is,
as said, protections and PATHs and relatively minor environmental things
[chdir, groupmembership], but as for the actual CGI machinery, itself, it
is OK. I did have to do one thing: many of my CGI programs use the
incoming IP address [and some other things that the server sets up in the
CGI environment] and so I just have my .profile set to set dummy values in
for those.
/Bernie\
--
Bernie Cosell Fantasy Farm Fibers
bernie@fantasyfarm.com Pearisburg, VA
--> Too many people, too few sheep <--
------------------------------
Date: Thu, 04 May 2000 02:01:53 +0930
From: Henry <htp@mac.com>
Subject: Re: if file already exists then remove...
Message-Id: <htp-C5E28A.02015304052000@news.metropolis.net.au>
In article <39100F98.BDA249D2@bmjgroup.com>, Kourosh A Mojar
<kmojar@bmjgroup.com> wrote:
> Im inexperienced programmer so please forgive me. I simply want to
> remove a file if it already exists. i.e. if test.txt already exists in
> folder then remove. Here is what I have done so far
>
> $output = "test\.txt";
>
> if ($output exists) {
> # unlink($output) || die "could not delete $output";
> }
>
> Any help will be appreciated. Thanking you in advance and for your kind
> attention,
Err... Why bother testing for it's existence at all? Why not simply:
unlink($output);
If the file exists, it will get deleted. If the file doesn't exist,
unlink will fail, but provided you don't try and catch the error, your
program will keep running.
The usual caveats about unlink and file permissions apply.
Henry.
------------------------------
Date: Wed, 3 May 2000 10:14:17 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: if file already exists then remove...
Message-Id: <MPG.1379fe42616eb94b98a9d4@nntp.hpl.hp.com>
In article <htp-C5E28A.02015304052000@news.metropolis.net.au> on Thu, 04
May 2000 02:01:53 +0930, Henry <htp@mac.com> says...
> In article <39100F98.BDA249D2@bmjgroup.com>, Kourosh A Mojar
> <kmojar@bmjgroup.com> wrote:
>
> > Im inexperienced programmer so please forgive me. I simply want to
> > remove a file if it already exists. i.e. if test.txt already exists in
> > folder then remove. Here is what I have done so far
> >
> > $output = "test\.txt";
> >
> > if ($output exists) {
> > # unlink($output) || die "could not delete $output";
The diagnostic is good, and would be even better if it included the
reason for the failure, which is in $!. `perldoc perlvar`
> > }
> >
> > Any help will be appreciated. Thanking you in advance and for your kind
> > attention,
>
> Err... Why bother testing for it's existence at all? Why not simply:
^^^^
its
> unlink($output);
>
> If the file exists, it will get deleted.
Provided the permissions on the directory are appropriate.
> If the file doesn't exist,
> unlink will fail, but provided you don't try and catch the error, your
> program will keep running.
>
> The usual caveats about unlink and file permissions apply.
Yes, and they should be diagnosed. I think your advice to 'Just do it!'
and ignore any possible errors is spectacularly wrong-headed.
The original poster's attempt was fine. All he needed to know about was
the '-e' operator, not how to subvert the proper use of diagnostics.
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Thu, 04 May 2000 03:14:15 +0930
From: Henry <htp@mac.com>
Subject: Re: if file already exists then remove...
Message-Id: <htp-9F5611.03141504052000@news.metropolis.net.au>
In article <MPG.1379fe42616eb94b98a9d4@nntp.hpl.hp.com>, Larry Rosler
<lr@hpl.hp.com> wrote:
>> Err... Why bother testing for it's existence at all? Why not simply:
> ^^^^
> its
Not on a possessive, Larry. Its fine to drop the apostrophe when the
long form reads 'it is' - but not in the case of ownership. (Or, at
least, so I've been told by academics with lots of letters after their
names.)
>> The usual caveats about unlink and file permissions apply.
>
> Yes, and they should be diagnosed. I think your advice to 'Just do it!'
> and ignore any possible errors is spectacularly wrong-headed.
"Far better it is to dare mighty things, to win glorious triumphs, even
though checkered by failure, than to rank with those poor spirits who
neither enjoy much nor suffer much, because they live in the grey
twilight that knows not victory nor defeat." - Theodore Roosevelt
Henry.
------------------------------
Date: 3 May 2000 15:40:30 GMT
From: mjtg@cus.cam.ac.uk (M.J.T. Guy)
Subject: Re: If slices are so great...
Message-Id: <8eph9e$sgj$1@pegasus.csx.cam.ac.uk>
Rick Delaney <rick.delaney@home.com> wrote:
>
> (@keep[0,1], @keep) = split /,/, $string;
>
>That's why slices are so great.
Only if you *know* that the final @keep has two or more elements. And
of course the order of evaluation isn't defined, so you could be bitten
someday ...
Mike Guy
------------------------------
Date: 03 May 2000 11:25:29 -0400
From: Lewis Perin <perin@panix.com>
Subject: Re: If slices are so great...
Message-Id: <pc77ldb8xli.fsf@panix6.panix.com>
Uri Guttman <uri@sysarch.com> writes:
> >>>>> "LP" == Lewis Perin <perin@panix.com> writes:
>
> LP> Quantum Mechanic <quantum_mechanic@my-deja.com> writes:
>
> >> @keep = split( /,/, $string )[2..-1]
>
> LP> A list slice does appear to work if you rearrange the parentheses
> LP> *and* forgo the -1 locution:
>
> LP> bash-2.03$ perl -le '$s = "a,b,c,d";@k = (split /,/, $s)[1..2];print @k'
>
> you are missing the whole point. he wants the the slice of elements
> AFTER the first 2. and you don't know the length of the list in advance.
>
> we all know you can do list slices but you can't count from the end like
> he wants. in other languages where the syntax for list slices is builtin
> you can do things like that. perl only supports a list of indices for a
> slice and you can generate that list anyway you want but you can't get
> the number of elements of the list in that expression.
I wasn't missing what the original poster wanted to achieve, but I
hadn't known list, as opposed to array, slices were possible at all.
I don't understand why taking the length of a list is impossible.
The scalar pseudo-function is out because of the need to preserve the
C-like semantics of the comma operator, but -1 is OK as an index
rather than a slice bookend:
bash-2.03$ perl -le '$s = "a,b,c,d";print ((split /,/, $s)[-1])'
d
I'm not sure what you mean by saying "perl only supports a list of
indices for a slice" when it clearly supports a range as long as the
range bounds don't implicitly refer to the length of the list being
sliced.
/Lew
--
Lew Perin / perin@mail.med.cornell.edu / perin@acm.org
www.panix.com/~perin/
------------------------------
Date: Wed, 03 May 2000 12:19:49 -0500
From: Michael Carman <mjcarman@home.com>
Subject: Re: If slices are so great...
Message-Id: <39105FB5.39868B12@home.com>
Lewis Perin wrote:
>
> I'm not sure what you mean by saying "perl only supports a list of
> indices for a slice" when it clearly supports a range as long as the
> range bounds don't implicitly refer to the length of the list being
> sliced.
Right -- a single index of -1 will be interpreted as the last element,
but [2 .. N] is just shorthand for [2, 3, 4, ... N]. So [2 .. -1] is an
empty list; the -1 doesn't magically become the last element here.
This really has nothing to do with lists vs. arrays -- you can't take an
array slice in this manner either. It's just that for arrays you have
the option of using [2 .. $#array], while with a list you have to get
messy as the responses here have shown. Here's another ugly one:
$str = 'a,b,c,d';
@keep = (split(/,/, $str))[2 .. split(/,/, $str) - 1];
Which is fairly clear and easily scalable, even to odd slices like
[2..5, 9 .. <end>] but nasty due to the double split(). I kind of like
Larry's solution best for this, but would rewrite it thusly to make it
more easily scalable:
@keep = $string =~ /(?:[^,]*,){2}?(.+)/ ? split /,/ => $1 : ();
That takes a bit of effort to interpret, though. ;-)
-mjc
------------------------------
Date: Wed, 03 May 2000 15:17:46 GMT
From: rscrane@my-deja.com
Subject: Installing modules with CPAN
Message-Id: <8epfu7$578$1@nnrp1.deja.com>
On an SGI with perl and gcc installed in /usr/freeware, the make called
from a CPAN install tries to use "cc" -- how can I get it to use gcc?
tried exporting CC=gcc, and looked through ~/.cpan/CPAN/MyConfig.pm but
couldn't find anywhere to set it.
TIA --
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 16 Sep 99)
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: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.
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 V9 Issue 2933
**************************************