[10386] in Perl-Users-Digest
Perl-Users Digest, Issue: 3979 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Oct 15 06:09:41 1998
Date: Thu, 15 Oct 98 03:00:19 -0700
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 15 Oct 1998 Volume: 8 Number: 3979
Today's topics:
Re: Are there any "perl.newbie" group or forum? <eashton@bbnplanet.com>
Re: Are there any "perl.newbie" group or forum? (David Formosa)
Re: Batch-changing URL case in HTML pages with perl (Sam Holden)
Re: comp.lang.perl.win32?? (Paul Murray)
Re: Continuing s/// from the last position. <antti.boman***NOSP@M***helsinki.fi>
DBI and Paradox <kar@webline.dk>
Re: Don't kill me <eashton@bbnplanet.com>
File I/O michael@mirage.com.au
Re: File I/O (Larry Rosler)
Re: FREE UNIX TELNET ACCESS (Tad McClellan)
Re: Generating a blessed reference outside of 'new' (David Alan Black)
Re: Help! Structures in Perl (Ronald J Kimball)
Re: Help! Structures in Perl (Mark-Jason Dominus)
Re: help! trying to emulate server side includes... (Ronald J Kimball)
Re: how to remove trailing spaces? <Antti-Jussi.Korjonen@sonera.fi>
Outside commands nrf@mailcity.com
Re: Outside commands <arturr@_teleaudio.com.pl>
Re: Perl FAQ - error found in "#How_do_I_get_a_file_s_t <dgris@perrin.dimensional.com>
Re: Perl FAQ - error found in "#How_do_I_get_a_file_s_t (Martien Verbruggen)
Re: perl recursion <ebohlman@netcom.com>
Re: Sending mail via CGI/Perl ? <wezzy@dds.nl>
sorting file entries by time <lee@juno.ltd.uk>
Re: sorting file entries by time <lee@juno.ltd.uk>
What is overwriting my variable? <mds-resource@mediaone.net>
Re: What is overwriting my variable? <zenin@bawdycaste.org>
Special: Digest Administrivia (Last modified: 12 Mar 98 (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 15 Oct 1998 03:49:31 GMT
From: Elaine -HappyFunBall- Ashton <eashton@bbnplanet.com>
Subject: Re: Are there any "perl.newbie" group or forum?
Message-Id: <36256E49.FE9CEDD5@bbnplanet.com>
Russ Allbery wrote:
> You tend to find more Perl the less emphasis the CS department as a whole
> has on theoretical compiler design and the like. Academics tend not to
> like Perl very much for all of the sloppy, unconstrained reasons that one
> may expect.
Sloppy in what regard? See I like Perl for many reasons even while being
a C++ dork. It makes sense in a lot of ways that other more structured
languages don't. Maybe I'm psycho. Academics aren't always right. We
used to have a Jesuit over every Sunday nite for dinner which gave me
plenty of chance to debate many things at a young age. To accept things
as they are for no other reason than their existence is no reason not
question. Perl is interesting in a way that no other language is or ever
has been.
> For example, we seem to have a great deal of difficulty actually managing
> to keep a Perl class going here. This probably has something to do with
> the fact that we have one of the most theory-oriented CS departments
> anywhere.
Theory is great but it is just that theory. I used to go at one of the
C++ godz who still writes for Dobbs. If I can't use it, why bother?
Programming should be both practical and fun. If it is neither, then
somewhere the point has been missed. I miss academia for the ability for
theoretical fancy, but sometimes that very frame of mind can be a
hinderance.
> The main thing that I detest about Java is the naming convention. Apart
> from that, it does fix a few problems that C++ has, and it does move away
> from "manage your own memory."
Well, Perls memory management sucks. 'nuff said. :)
> The way we do that here is to teach classes where the assignments must be
> completed in C but every submitted solution must also run completely clean
> under Purify, including no leaked memory (even potentially).
Admirable. Do you teach Perl as well?
> My experience is that most programming in a CS cirriculum isn't learned
> from the programming classes. It's learned from the classes in other
> subjects that require you to write programs. I learned more C and C++
> from my operating system design and network programming classes than I
> learned from any programming classes.
A true academic. Have you read E. O. Wilson's 'Consilience'? :) All
knowledge is related.
e.
After all, the cultivated person's first duty is to
always be prepared to rewrite the encyclopedia. - U. Eco -
------------------------------
Date: 15 Oct 1998 14:01:45 +1000
From: dformosa@zeta.org.au (David Formosa)
Subject: Re: Are there any "perl.newbie" group or forum?
Message-Id: <703s39$t1k$1@godzilla.zeta.org.au>
In <ylr9wamv6e.fsf@windlord.stanford.edu> Russ Allbery <rra@stanford.edu> writes:
[...]
>My experience is that most programming in a CS cirriculum isn't learned
>from the programming classes.
The universty where I attend seems to have taken this taken this phisphy
to its logical extreem. There are officaly no programming classes. There
are however subjects that teach you a language as long as some task.
--
Please excuse my spelling as I suffer from agraphia. See the URL in my
header to find out more.
------------------------------
Date: 15 Oct 1998 04:35:48 GMT
From: sholden@pgrad.cs.usyd.edu.au (Sam Holden)
Subject: Re: Batch-changing URL case in HTML pages with perl
Message-Id: <slrn72aut4.60f.sholden@pgrad.cs.usyd.edu.au>
On Wed, 14 Oct 1998 19:54:31 -0400, Mark Cain <mark@uninetwork.com> wrote:
>case sensitive?
>
>following this link:
>
> HttP://Www.NEtscAPe.coM/
>
>Not trying to pick on you. Just wondering what you meant by "URLs are case sensitive" as the
>above link is a URL and it is not case sensitive on my browser of Netscape. Communicator 4.06
>[Win98].
Try http://www.netscape.com/index.html and http://www.netscape.com/INDEX.html
You can't prove something with an example... you can disprove with a
counter-example though...
The _file_ section of the URL is case-sensitive (for web-servers running on
machines with real file-systems that are case-sensitive anyway)
--
Sam
Another result of the tyranny of Pascal is that beginners don't use
function pointers.
--Rob Pike
------------------------------
Date: 15 Oct 1998 09:18:15 GMT
From: murray-paul@usa.net (Paul Murray)
Subject: Re: comp.lang.perl.win32??
Message-Id: <slrn72bfen.f60.murray-paul@unix2.netaxs.com>
On 14 Oct 1998 19:50:17 GMT, Ethan H. Poole <ehp@gte.net> wrote:
>In article <slrn72993i.lbu.murray-paul@unix3.netaxs.com>, murray-paul@usa.net
>says...
>>I don't know how accurate the numbers will be, but a DN search on clp.*
>>from Oct 1st gives about 59000 articles, 2400 matching subjects of
>>'win32 win95 NT microsoft'. So that's 4%, less than I thought. Body
>>searching on 'win32 win95 NT' gives 7500 or 12.5%, and body searching
>>on 'win32 win95 NT microsoft' gives 12000 or 20%, which sounds to high.
>As you've noticed, searching on the term "NT" will yield many undesired hits.
>
>e.g.: "waNTed", "caNT", "doesNT", "eNTer", etc.
No, I don't think so. Dejanews searches only seem to match whole words.
A search on 'erl' matches no articles!
-Paul Murray
------------------------------
Date: Thu, 15 Oct 1998 10:11:59 +0200
From: "Antti Boman" <antti.boman***NOSP@M***helsinki.fi>
Subject: Re: Continuing s/// from the last position.
Message-Id: <70475h$51v$1@hiisi.inet.fi>
> >> I'll put this on the perl wish list. To get \G to s///. That would
solve
> >> it.
>
> MR> It would be nice, but it is usually not a good thing to require a
language
> MR> change to solve a programming problem. This is my private opinion so
> MR> you don't need to agree.
You're correct, I don't have to ;)
>actually i think it is a good idea. other languages (even python i
>think) allow you to find the location of a match or replacement. this is
>useful in many areas. it you don't use /g then \G in s/// would make
>sense to begin the next search where the last replacement ended.
This is my point, too. I'm not happy with the answer "it only works with
m//g". Why not? Would it be logical if "$ matches the end of line only in
s///egm"? After all, this is just a small gimmick to make the language
simpler and easier to use.
>here is my solution which is reasonably fast (no extra copies) and clean
>code (tested):
Thanks, it works.
Btw, I posted my opinion to the wishlist of Perl, let's see if they agree or
not.
-a
------------------------------
Date: Thu, 15 Oct 1998 09:03:59 +0200
From: Kaare Rasmussen <kar@webline.dk>
Subject: DBI and Paradox
Message-Id: <36259E5E.B58F73B3@webline.dk>
I'm going to make a Perl script that will read and update a Paradox
database. I believe that I will have to use DBI and DBD ODBC to do so.
Is this correct? This will be with Perl for Win32 (at first at least).
Did anybody try this?
Do I have to look out for something in particular when I try this?
------------------------------
Date: Thu, 15 Oct 1998 03:35:42 GMT
From: Elaine -HappyFunBall- Ashton <eashton@bbnplanet.com>
Subject: Re: Don't kill me
Message-Id: <36256B0C.F47BEF45@bbnplanet.com>
David Adler wrote:
> >I think you mean 52 people.
> >
> >HTH, HAND
>
> Oooooohhhh you're evil!
Perhaps, but boyz can never approach bitch. :) Donut taunt HFB. I could
drag out my leather but I save that for special occasions. I didn't
count the number of emails I received on the topic but it was excessive.
Evil? Nay.
/me flips hair in the general direction of the silly boyz. Well, DUH. :P
e.
After all, the cultivated person's first duty is to
always be prepared to rewrite the encyclopedia. - U. Eco -
------------------------------
Date: Thu, 15 Oct 1998 04:18:51 GMT
From: michael@mirage.com.au
Subject: File I/O
Message-Id: <703t3b$fsg$1@nnrp1.dejanews.com>
Gday, please forgive me if this quesion is a little simple but so far, have
not found any other help. When I open a text file, shove it into a variable
and then print it out, I get spaces at the front of the lines. I think these
are called qhitespaces or something like it. Anyway, can someone help me to
get rid of them. Thanks in advanced
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
------------------------------
Date: Wed, 14 Oct 1998 21:48:38 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: File I/O
Message-Id: <MPG.108f1e85a4c9f0e9898b8@nntp.hpl.hp.com>
[Posted to comp.lang.perl.misc and copy mailed.]
In article <703t3b$fsg$1@nnrp1.dejanews.com> on Thu, 15 Oct 1998
04:18:51 GMT, michael@mirage.com.au <michael@mirage.com.au> says...
> Gday, please forgive me if this quesion is a little simple but so far, have
> not found any other help. When I open a text file, shove it into a variable
Presumably into an array.
> and then print it out, I get spaces at the front of the lines. I think these
> are called qhitespaces or something like it. Anyway, can someone help me to
Something like whitespaces? Actually they are space characters.
> get rid of them. Thanks in advanced
Sight unseen, you are doing this:
print "@array";
The effect of this is to insert the contents of the special variable $"
(which by default is a space character) between each of the array
elements, hence (as you probably still have newline characters in there)
at the beginning of each line (except the first).
Take away the superfluous quotes in the print statement (which you
haven't shown us), and the leading spacs will go away. I guarantee it!
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Wed, 14 Oct 1998 22:05:09 -0500
From: tadmc@flash.net (Tad McClellan)
Subject: Re: FREE UNIX TELNET ACCESS
Message-Id: <5po307.dli.ln@flash.net>
Graham McMillan (gmcmillan@iname.com) wrote:
: Does anyone now of any good (fast if possible) unix servers that offer
: telnet access for free? If so please e-mail me or post a response.
This is the Perl newsgroup.
Do you have a Perl question?
--
Tad McClellan SGML Consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: 15 Oct 1998 04:54:21 -0400
From: dblack@pilot.njin.net (David Alan Black)
Subject: Re: Generating a blessed reference outside of 'new'
Message-Id: <704d7t$d99$1@pilot.njin.net>
Hello -
Chris_Schoenfeld@sonic.net writes:
>I have a package with a new() routine which creates a new blessed reference
>and returns it.
>The package also has a dbconnect() method.
>The dbconnect method reads:
>sub dbconnect{
> my $self = shift || new(); #create a new object if none passed.
> #make db connection... return $self; } This way I can create a new
>unconnected object with new() or go all the way and get a connected object
>with dbconnect().
>This doesn't work though - although new() sets up and blesses the object when
>called this way exactly as it does when it is called externally (as verified
>by 'print Dumper $blessedref' just before return), the object is not assigned
>to $self.
The thing is, when you call the dbconnect method (as a method, not,
presumably, as a subroutine), it *always* gets a first argument - either
an object reference, or the class name. Therefore, that call to new
never happens.
Besides, you're calling new() as a regular subroutine, which means it
wouldn't behave like an object constructor should anyway.
Here's little package which has a second constructor which, I think,
behaves something like the way you want dbconnect to behave:
#!/usr/local/bin/perl -w
use strict;
package Newtest;
sub new {
my $this = shift;
bless {}, ref $this || $this;
}
sub constr_too {
my $this = shift;
# Construct a new object if called as a class method, not otherwise.
if (ref $this) {
print "\t(constr_too says: argument is already a ref - returning it)\n";
return $this;
}
else {
print "\t(constr_too says: argument is not a ref - calling new())\n";
return new $this;
}
}
package main;
print "\nCalling new....\n";
my $n = new Newtest;
print "First new object: $n\n\n";
print "Calling constr_too as class method....\n";
my $s = Newtest->constr_too;
print "Result: $s\n\n";
print "Calling constr_too as object method....\n";
my $t = $s->constr_too();
print "Result: $t\n\n";
__END__
Output:
Calling new....
First new object: Newtest=HASH(0x80c3ab4)
Calling constr_too as class method....
(constr_too says: argument is not a ref - calling new())
Result: Newtest=HASH(0x80ca1f8)
Calling constr_too as object method....
(constr_too says: argument is already a ref - returning it)
Result: Newtest=HASH(0x80ca1f8)
David Black
dblack@pilot.njin.net
------------------------------
Date: Wed, 14 Oct 1998 23:12:55 -0400
From: rjk@coos.dartmouth.edu (Ronald J Kimball)
Subject: Re: Help! Structures in Perl
Message-Id: <1dgwwez.1s2bhv5dnueizN@bos-ip-2-161.ziplink.net>
Will <huckfin@hotmail.com> wrote:
> BTW, i'm still RTMF.
Probably you should stop doing one while you're doing the other.
(ITYM RTFM. HTH, HAND!)
--
_ / ' _ / - aka - rjk@coos.dartmouth.edu
( /)//)//)(//)/( Ronald J Kimball chipmunk@m-net.arbornet.org
/ http://www.ziplink.net/~rjk/
"It's funny 'cause it's true ... and vice versa."
------------------------------
Date: 14 Oct 1998 23:27:45 -0400
From: mjd@op.net (Mark-Jason Dominus)
Subject: Re: Help! Structures in Perl
Message-Id: <703q3h$do1$1@monet.op.net>
In article <703mvs$276q$1@news.gate.net>, Will <huckfin@hotmail.com> wrote:
>:what a novel idea! we should have this in perl! BTW this is not an array
>:of address structs, but an empty pointer to one.
>
>
>Why the sarcasm?
Yeah, and he was wrong anyway. Burf!
------------------------------
Date: Wed, 14 Oct 1998 23:05:44 -0400
From: rjk@coos.dartmouth.edu (Ronald J Kimball)
Subject: Re: help! trying to emulate server side includes...
Message-Id: <1dgwvgs.w9rkz75td37oN@bos-ip-2-161.ziplink.net>
minus the SPAMSUCKS <cstanley@SPAMSUCKSusit.net> wrote:
> so basically, i need a pattern that will find:
>
> <!--#include virtual="FILENAME" -->
>
> and replace it with the contents of FILENAME.
Assuming that the substring to match will be in exactly that form,
here's one way to do it:
s/<!--#include virtual="([^"]*)" -->/
my $r;
if (open(F,$1)) {
$r = join '', <F>;
} else {
$r = "Unable to open $1: $!\n";
# whatever error message is suitable
}
$r;
/gie;
--
_ / ' _ / - aka - rjk@coos.dartmouth.edu
( /)//)//)(//)/( Ronald J Kimball chipmunk@m-net.arbornet.org
/ http://www.ziplink.net/~rjk/
"It's funny 'cause it's true ... and vice versa."
------------------------------
Date: Thu, 15 Oct 1998 09:51:32 +0300
From: Antti-Jussi Korjonen <Antti-Jussi.Korjonen@sonera.fi>
To: Mark Cain <mark@uninetwork.com>
Subject: Re: how to remove trailing spaces?
Message-Id: <36259B74.DE842FE1@sonera.fi>
Mark Cain wrote:
> try this:
> # search for zero or more of the space character juxtaposed to the EOL
> # and replace it with nothing
> $temp =~ s/ *$//;
that worked, thank you very much!
------------------------------
Date: Thu, 15 Oct 1998 07:23:42 GMT
From: nrf@mailcity.com
Subject: Outside commands
Message-Id: <7047tu$u53$1@nnrp1.dejanews.com>
Is perl able to execute any outside commands like unix commands?
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
------------------------------
Date: Thu, 15 Oct 1998 09:05:24 GMT
From: "Artur Romanowicz" <arturr@_teleaudio.com.pl>
Subject: Re: Outside commands
Message-Id: <oViV1.4918$bY2.1148490@news.tpnet.pl>
For example:
@fileslist=`ls -al`;
nrf@mailcity.com napisa3(a) w wiadomo6ci:
<7047tu$u53$1@nnrp1.dejanews.com>...
>Is perl able to execute any outside commands like unix commands?
>
>-----------== Posted via Deja News, The Discussion Network ==----------
>http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
------------------------------
Date: Thu, 15 Oct 1998 03:32:36 GMT
From: Daniel Grisinger <dgris@perrin.dimensional.com>
Subject: Re: Perl FAQ - error found in "#How_do_I_get_a_file_s_timestamp_"
Message-Id: <m33e8qbj9g.fsf@perrin.dimensional.com>
James Ludlow <ludlow@us.ibm.com> writes:
> Locally, I'm using version 5.004_02, and the error is in my copy of the
> FAQ also.
So create a patch and send it to Tom. You obviously know the
solution, fix it.
dgris
- Amazed at the fact that so many people use free software
but still don't get it.
--
Daniel Grisinger dgris@perrin.dimensional.com
`By about halfway through I was beginning to guess the
ending, but it still kind of surprised me.'
David Hatunen, talking about the movie Titanic
------------------------------
Date: Thu, 15 Oct 1998 06:07:27 GMT
From: mgjv@comdyn.com.au (Martien Verbruggen)
Subject: Re: Perl FAQ - error found in "#How_do_I_get_a_file_s_timestamp_"
Message-Id: <zigV1.117$AA3.313925@nsw.nnrp.telstra.net>
In article <36255EA5.D54CE867@us.ibm.com>,
James Ludlow <ludlow@us.ibm.com> writes:
> While answering someone else's FAQ by pointing them to the online HTML
> FAQ, I noticed that there seems to be an error in the section
> "#How_do_I_get_a_file_s_timestamp_".
>
> The code reads:
> $write_secs = (stat($file))[9];
> print "file $file updated at ", scalar(localtime($file)), "\n";
It's fixed in the documentation for 5.005_02
# perldoc -q timestamp
[snip]
$write_secs = (stat($file))[9];
printf "file %s updated at %s\n", $file,
scalar localtime($write_secs);
Martien
--
Martien Verbruggen |
Webmaster www.tradingpost.com.au | That's not a lie, it's a terminological
Commercial Dynamics Pty. Ltd. | inexactitude.
NSW, Australia |
------------------------------
Date: Thu, 15 Oct 1998 07:01:52 GMT
From: Eric Bohlman <ebohlman@netcom.com>
Subject: Re: perl recursion
Message-Id: <ebohlmanF0uwv5.BsM@netcom.com>
lietk@my-dejanews.com wrote:
: I am not able to create a new scope for variables in successive recursions of
: a function.
: #!/usr/local/bin/perl
: $word = $ARGV[0];
: scramble("",$word);
: sub scramble {
: local($lead,$rest) = @_;
: local($lim) = length($rest);
: for($i = 0;$i < $lim;$i++){
Note that $i is a global variable.
: $leadstr = $lead.substr($rest,$i,1);
: $remain = substr($rest,0,$i).substr($rest,$i+1);
: if($remain eq undef){
: print "$leadstr\n";
: }
: else {
: scramble($leadstr,$remain);
Note that you're making the recursive call inside the loop controlled by
the global variable $i, which means that when you come back from it, $i
will have been changed "out from under you."
: }
: }
: }
To fix this problem, change "for ($i..." to "for (my $i...". But also
see Tom Phoenix's response, since you have other problems.
------------------------------
Date: Thu, 15 Oct 1998 11:01:43 +0200
From: "WeZzy" <wezzy@dds.nl>
Subject: Re: Sending mail via CGI/Perl ?
Message-Id: <704e0i$jod$1@enterprise.cistron.nl>
Thanks everyone for your comments on security issues, I will
look into it...
Thanks again,
Wesly Grefrath
------------------------------
Date: Thu, 15 Oct 1998 09:28:20 +0100
From: Lee Musgrave <lee@juno.ltd.uk>
Subject: sorting file entries by time
Message-Id: <3625B224.EBB86C71@juno.ltd.uk>
I have a file which contains entries timestamped in the form HH:MM:SS,
these entries are not
actually in chronological order, and the time is only a small part of
each line, but is the only part
i want sorted,
each line looks roughly like this:
BUND DEC 98 BUY 19981014 08:07:02 259 238412992 30 114.56 343680 SMKA01
| time |
here
currently each line is split into an array to be worked on, this i have
running fine, but what i need
now is a sort routine that will sort every line in this file so that a
have a file where every entry is
in chronological order. You'll probably notice that this line also
contains the date, but this is not
important, i have a function which discards all but the current date.
i've had a look at the built in sort function, but i don't think that
will work without splitting the
entire file into an array, this file is a couple of thousand lines long,
and speed is essential.
lee.
------------------------------
Date: Thu, 15 Oct 1998 10:06:34 +0100
From: Lee Musgrave <lee@juno.ltd.uk>
Subject: Re: sorting file entries by time
Message-Id: <3625BB1A.B6FC8F40@juno.ltd.uk>
my mail program seems to have messed up this posting.
the time on the line in the posting is 08:07:02, i know
it should be obvious, but just in case :)
lee
------------------------------
Date: Wed, 14 Oct 1998 23:10:43 -0500
From: "Michael D. Schleif" <mds-resource@mediaone.net>
Subject: What is overwriting my variable?
Message-Id: <362575C3.D13CB804@mediaone.net>
I have written a Perl program that invokes several proprietary system
calls. In so doing, I have run into a very peculiar phenomenon --
something is overwriting my variable!
This all hinges around sub-routining a `case' statement.
I have tried to simplify the situation below (curious.plx.) Some of
this code maybe ridiculous, due to reducing it to fit herein. Much now
is hardcoded, rather than determined at runtime. While simplifying for
submission, I found another workaround; but, I still do _not_ understand
_why_ this is happening?
This should return: AIX 4.2.1.0 aix4-r1
Instead, it returns: AIX 4.2.1.0 4.2.1.0
Clearly, $winterp is being overwritten by wruntask() -- but, why?
Note(1): If, instead of ``for (@_) {}'', I use ``$_ = shift'', the
problem does _not_ occur.
Note(2): If I hardcode ``return '4.2.1.0';'' as first line of sub
wruntask, the problem does _not_ occur.
Note(3): This problem does _not_ happen if the case statement is in
main.
I am sure that this is something simple, probably a FAQ; but, sometimes
I am too dense for my own good ;)
--
Best Regards,
mds
mds resource
888.250.3987
"Dare to fix things before they break . . . "
"Our capacity for understanding is inversely proportional to how much we
think we know. The more I know, the more I know I don't know . . . "
##########################################
#!d:/perl5/bin/perl.exe -w
### curious.plx ###
require 5;
use diagnostics;
use strict;
my %hash;
my $node = shift;
build_hash();
my $winterp = 'aix4-r1';
my $arg = $hash{$winterp};
print parse($winterp), "\n";
exit 0;
sub build_hash {
while (<DATA>) {
my ($key, @value) = split;
my $value = join ' ', @value;
$hash{$key} = $value;
}
1;
}
sub parse {
# $_ = shift;
my $specific;
for (@_) {
if (/^aix4-r1$/) {
$specific = "AIX " . wruntask() . " ";
} else {
die "\n\tERROR: What to do with \'$_\' OS ? \n\n";
}
}
$specific .= $winterp;
return $specific;
}
sub wruntask {
# return '4.2.1.0';
my $wruntask = "echo 4.2.1.0";
my $string;
open WRUNTASK, "$wruntask |" or die "\n\tERROR: Cannot invoke
\'$wruntask\': $! \n\n";
while (<WRUNTASK>) {
next if /(^#|^$)/;
chomp;
$string = $_, last;
}
close WRUNTASK;
return $string;
}
__DATA__
aix4-r1 oslevel
hpux10 uname -a
solaris2 uname -a
------------------------------
Date: 15 Oct 1998 06:12:04 GMT
From: Zenin <zenin@bawdycaste.org>
Subject: Re: What is overwriting my variable?
Message-Id: <908431916.984281@thrush.omix.com>
[posted & mailed]
Michael D. Schleif <mds-resource@mediaone.net> wrote:
: I have written a Perl program that invokes several proprietary system
: calls. In so doing, I have run into a very peculiar phenomenon --
: something is overwriting my variable!
>snip<
: sub build_hash {
: while (<DATA>) {
Add the line "local $_;" before that while() loop.
: sub wruntask {
: # return '4.2.1.0';
: my $wruntask = "echo 4.2.1.0";
: my $string;
: open WRUNTASK, "$wruntask |" or die "\n\tERROR: Cannot invoke \'$wruntask\': $! \n\n";
: while (<WRUNTASK>) {
And this one too, which is what your current problem is.
That's the sort answer, here's the long one:
*Everything* in perl is passed by reference, even in perl4 that had
no "references". More correctly, it is "aliased" actually.
print parse($winterp), "\n";
Here, $winterp is being passed to parse(). All arguments to
perl functions are aliased to that function's @_ array. Thus, if
you modify $_[0] inside of parse() you'll modify the value of
$winterp inself, because $_[0] is now aliased to $winterp.
But you don't do that...or do you?
for() loops do a similar alias of each value of there list
to $_ or whatever iteration variable you might give it. When
you passed it @_ ("for (@_) {}") the first value of @_, ie $_[0],
was aliased to $_, since you didn't name another variable to
use ("foreach my $foo (@_) {}"). And $_ is a (danger!) global.
So now...
$winterp has been aliased to $_[0]
$_[0] has been aliased to $_
Now you call wruntask(), which has a magic while (<>) loop. Such
magic while() loops do not automatically localize $_ (for() loops
do, btw, but *only* localize, not lexical...).
So now...
Your while(<>) loop in wruntask just changed the value of $_.
Since $_ is aliased to parse()'s $_[0], it gets changed too.
Since parse()'s $_[0] is aliased to $winterp, it gets changed.
Rules of thumb to not hit this problem:
*always* localize $_ for any use other then for() loops.
And most of the time, copy @_ to something else to avoid the
possibility of the magic aliasing biting you like this. Example:
sub foo {
my @args = @_;
...
}
Simple, hay? :-)
--
-Zenin (zenin@archive.rhps.org) From The Blue Camel we learn:
BSD: A psychoactive drug, popular in the 80s, probably developed at UC
Berkeley or thereabouts. Similar in many ways to the prescription-only
medication called "System V", but infinitely more useful. (Or, at least,
more fun.) The full chemical name is "Berkeley Standard Distribution".
------------------------------
Date: 12 Jul 98 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Special: Digest Administrivia (Last modified: 12 Mar 98)
Message-Id: <null>
Administrivia:
Special notice: in a few days, the new group comp.lang.perl.moderated
should be formed. I would rather not support two different groups, and I
know of no other plans to create a digested moderated group. This leaves
me with two options: 1) keep on with this group 2) change to the
moderated one.
If you have opinions on this, send them to
perl-users-request@ruby.oce.orst.edu.
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.
To submit articles to comp.lang.perl.misc (and this Digest), send your
article to perl-users@ruby.oce.orst.edu.
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.
The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.
The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.
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 V8 Issue 3979
**************************************