[21839] in Perl-Users-Digest
Perl-Users Digest, Issue: 4043 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Oct 29 18:06:45 2002
Date: Tue, 29 Oct 2002 15:05:13 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Tue, 29 Oct 2002 Volume: 10 Number: 4043
Today's topics:
Re: [Q] An associative array of file handles <newspeak@nowhere.com>
Re: Activestate Perl problem with multiple file opening (Jan Fure)
Re: amateur socketeer: long wait (Leaffoot)
Re: amateur socketeer: long wait (Leaffoot)
Re: amateur socketeer: long wait (Leaffoot)
Re: amateur socketeer: long wait <uri@stemsystems.com>
Re: amateur socketeer: long wait <goldbb2@earthlink.net>
Re: Can package names be supplied on-the-fly? <goldbb2@earthlink.net>
Creating a Microsoft Word file with Win32::OLE <cawong@cisco.com>
Re: Cut characters from string (SlimClity)
Deep misunderstanding of recursion (Cameron Laird)
Re: Deep misunderstanding of recursion <tassilo.parseval@post.rwth-aachen.de>
Re: Deep misunderstanding of recursion <bart.lateur@pandora.be>
Re: Deep misunderstanding of recursion <joe+usenet@sunstarsys.com>
Re: Deep misunderstanding of recursion (Cameron Laird)
Re: Deep misunderstanding of recursion <lusol@Pandora.cc.lehigh.edu>
files in perl <ivan@diprima.freeserve.co.uk>
Re: files in perl <tassilo.parseval@post.rwth-aachen.de>
Pascal comment RE (Fredrik Andersson)
Perl Ping script for Win 98 <mail@eircom.net>
Re: Perl Ping script for Win 98 <goldbb2@earthlink.net>
Re: pls help on array list pass to main from a function <krahnj@acm.org>
Re: POST filter <usenet@dwall.fastmail.fm>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 29 Oct 2002 14:51:54 -0800
From: Morden <newspeak@nowhere.com>
Subject: Re: [Q] An associative array of file handles
Message-Id: <3DBF110A.8090309@nowhere.com>
John W. Krahn wrote:
> Morden wrote:
>
>>Suppose I want to write snippets to files instead of pooling the text in
>>memory ($logtext associative array).
>
>
> If your data was sorted by store number (your hash key) this would be
> possible.
>
It is not sorted.
>
>>Is it doable to have an associative
>>array of filehandles?
>
>
> Yes.
>
How would I do this?
>
>>}
>>
>>while($_=<>) {
>
>
> If you are going to use the special null filehandle you should leave the
> file names in @ARGV and perl will open the file(s) for you. If you want
> to open the file yourself you should use a filehandle other then STDIN.
>
I use the input filename to derive the output filename prefix from.
If none then there's no prefix.
>
>
>> ($n1,$n2,$store,$therest) = split(/\t/, $_);
>
>
> If you only need one field you can use undef as a placeholder for the
> fields you don't need.
>
> my(undef,undef,$store) = split /\t/;
>
> Or you can coerce split to return a single field.
>
> my $store = (split /\t/)[2];
>
>
>
>> # remove doublequotes around store number
>> $store =~ s/"//g;
>
>
> You could always combine this with the split.
>
> my $store = (split /"?\t"?/)[2];
>
>
>
>> if("$store" ne "") {
>
>
> There is no need to put a scalar variable in quotes, this isn't a shell
> script.
>
>
>> $logtext{$store} .= $_;
>
>
> If the value of $store is equal to "" then concatenating it won't cause
> a problem so the if statement isn't required.
>
The program should drop the entries for empty stores on the floor.
>
>
>> }
>>}
>>
>>foreach $key (sort keys(%logtext)) {
>> #print "$key\n";
>> open(OUT, ">$ARG$key") || die("can't open file $ARG$key\n");
>> print OUT "$logtext{$key}";
>> close(OUT);
>>}
>
> You could do it this way without multiple filehandles or a hash:
>
> #!/usr/bin/perl -w
> use strict;
>
> die "usage: $0 filename\n" unless @ARGV;
>
> while ( <> ) {
> my $store = (split /"?\t"?/)[2];
> if ( open OUT, ">> $ARGV.$store" ) {
> print OUT;
> close OUT;
> }
> }
>
> __END__
This would work albeit I'd have to open and close files all the time.
And you won't be able to feed some input to it from stdin.
How does this look to you:
#!/usr/bin/perl -w
if ( defined( $PREF = shift ) ) {
open(STDIN, $PREF) || die("can't open file $PREF code $!\n");
$PREF .= ".";
} else {
# alternatively it could be "stdin."
$PREF="";
}
while ( <> ) {
my $store = (split /"?\t"?/)[2];
if ( $store && open OUT, ">>$PREF$store" ) {
print OUT;
close OUT;
}
}
(I've dropped "use strict" because perl was complaining:
lobal symbol "$PREF" requires explicit package name at
/usr/lib/spwww/spwwwlogsplit.pl line 4.
Global symbol "$PREF" requires explicit package name at
/usr/lib/spwww/spwwwlogsplit.pl line 5.
Global symbol "$PREF" requires explicit package name at
/usr/lib/spwww/spwwwlogsplit.pl line 5.
Global symbol "$PREF" requires explicit package name at
/usr/lib/spwww/spwwwlogsplit.pl line 6.
Global symbol "$PREF" requires explicit package name at
/usr/lib/spwww/spwwwlogsplit.pl line 9.
Global symbol "$PREF" requires explicit package name at
/usr/lib/spwww/spwwwlogsplit.pl line 14.
)
------------------------------
Date: 29 Oct 2002 14:11:31 -0800
From: jan_may2002_fure@attbi.com (Jan Fure)
Subject: Re: Activestate Perl problem with multiple file opening
Message-Id: <e47a84bf.0210291411.1e8b78da@posting.google.com>
> #!/usr/local/bin/perl
> use strict;
> use warnings;
>
> for my $file_number (1..3) {
> open WORKFILE, ">$file_number.txt"
> or die "Cannot open $file_number.txt: $!";
> for my $line_number (1..100000) {
> print WORKFILE
> "$line_number \t Second_column \t Third_column \t",
> rand(), " \n";
> }
> close WORKFILE;
> }
>
> This way you only have to hold one line at a time in memory instead of
> having a huge array. Or is there some reason you need all the data in
> memory at one time?
Thanks for the advice, it did resolve the slowdown problem when I
wrote the files I opened into the array line by line using the push
command:
open INPUT_FILE, "$file" or die "can't open $file";
while(<INPUT_FILE>){
push @array, $_;
}
close INPUT_FILE;
instead of the initial:
open INPUT_FILE, "$file" or die "can't open $file";
@array = <INPUT_FILE>;
close INPUT_FILE;
The reason I need the whole array in memory, is that I am implementing
the following algorithm:
1. Open file, assign to array.
2. Capture first line, which is heading.
3. Sort array.
4. Break array into chunks not exciding a set limit of lines, which
are written to output files.
The snag is that the data consists of natural units which should be
kept together, and the way I am accomplishing this is to accumulate
(sorted) data while checking for change from one unit to next, and
while check is true, the chunk of data is appended to one file if the
final number of lines does not exceed 32000, otherwise a new file is
opened, with same heading line written to it, followed by current
chunk of data.
I had found one solution before, but it involved running the part of
the script where the file was opened via the system() command,
invoking a different Perl script and the whole file could be read into
the array in one chunk without slowdown. I didn't like this solution.
This is definetly an ActivePerl bug, as the first file opens and reads
into an array quickly, whereas each subsequent file is slow. By
running a system call to open the file, each file becomes the first
file, and thus the code runs quickly.
I am assigning the bug to Activeperl, because there is no slowdown in
Unix.
Anyway, thanks to all posters who contributed constructive
suggestions, including Uri, John and Brian who replied to my previous
post with title:
"Slow code, improvements requested". I realize I have a lot to learn,
and appreciate all ideas.
Jan Fure
>
> [rest of code snipped, as it does similar things]
------------------------------
Date: 29 Oct 2002 08:50:50 -0800
From: leaffoot@hotmail.com (Leaffoot)
Subject: Re: amateur socketeer: long wait
Message-Id: <d7bd06a3.0210290850.75ace4f@posting.google.com>
"By the way, you should drop Perl 4 syntax, it's way too
noisy to read."
--point well taken, thanks.
>
> sub sendstr {
> my $sock = shift;
> my $str = shift;
> print $sock "$str" || confess "I can't send the str $str!\n"; # <=
> hope that you don't mix it with sysread/syswrite before entering this sub
> return 1;
> }
>
-- Um I don't *think* I do. Everything that happens prior to calling
sendstr is normal i/o.
> And it hangs on after the last data is transferred, as if still trying
> to read. Can anyone help? Thanks!!
> Indeed. You should close the socket at the sender end. It will signal an EOF
> and thus, the sysread call in the receiver will bail out.
-- That makes sense, but won't it make the script take a lot longer if
I have to reconnect to the socket for every record I send? I might
have 10,000 records to transmit- does this mean I'd have to reconnect
for each one?
Thanks for your help!!
> > Becka Louden
------------------------------
Date: 29 Oct 2002 08:55:17 -0800
From: leaffoot@hotmail.com (Leaffoot)
Subject: Re: amateur socketeer: long wait
Message-Id: <d7bd06a3.0210290855.2e94db22@posting.google.com>
> L> sub sendstr {
> L> # pass in socket and string to send
> L> my @args = @_;
> L> my $sock = shift @args;
> L> my $str = shift @args;
>
> where did you get that style of arg handling? why do you use the temp
> var @args? either just shift @_ directly or do the more common:
>
> my( $sock, $str ) = @_ ;
Thanks Uri- I do this mostly out of uncertainty, I'll admit. I never
know if I'm eventually going to want to original @_, so I copy it, and
I know that @args will always have the altered array, @_ the untouched
one. But point well taken.
> L> while($length_l = sysread $sock, $comeback_l, $len) {
> L> if(!defined $length_l) {
>
> unless is more readable than if( ! )
True, thanks.
------------------------------
Date: 29 Oct 2002 08:58:51 -0800
From: leaffoot@hotmail.com (Leaffoot)
Subject: Re: amateur socketeer: long wait
Message-Id: <d7bd06a3.0210290858.3850f19a@posting.google.com>
"Tan Nguyen" <nospam@nospam.com> wrote in message news:<3dbe328d_7@nopics.sjc>...
> "Newbie" <mike_constant@yahoo.com> wrote in message
> news:apk6br$2f2oo$1@ID-161864.news.dfncis.de...
> [snipped]
> > write(STDOUT, $comeback, $length_1) or die "write error: $!\n";
> "write" doesn't take the same arguments as "read". It should be something
> similar to the following
> write $comeback or die "write error: $!\n";
>
> or simple
> print $comeback;
I realize syswrite uses write, but they aren't the same. syswrite
takes a Filehandle, Scalar, Length, and offset. I believe I need to
use syswrite here in order to control the data.
Thanks,
Becka
------------------------------
Date: Tue, 29 Oct 2002 17:25:01 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: amateur socketeer: long wait
Message-Id: <x7znsxtaxf.fsf@mail.sysarch.com>
>>>>> "L" == Leaffoot <leaffoot@hotmail.com> writes:
L> "Tan Nguyen" <nospam@nospam.com> wrote in message news:<3dbe328d_7@nopics.sjc>...
>> "Newbie" <mike_constant@yahoo.com> wrote in message
>> news:apk6br$2f2oo$1@ID-161864.news.dfncis.de...
>> [snipped]
>> > write(STDOUT, $comeback, $length_1) or die "write error: $!\n";
>> "write" doesn't take the same arguments as "read". It should be something
>> similar to the following
>> write $comeback or die "write error: $!\n";
>>
>> or simple
>> print $comeback;
L> I realize syswrite uses write, but they aren't the same. syswrite
L> takes a Filehandle, Scalar, Length, and offset. I believe I need to
L> use syswrite here in order to control the data.
syswrite does not use (perl's) write. it is a wrapper around c's write.
and you can now just pass it the scalar value and no length. it will
figure out the length from that.
in general when dealing with sockets and pipes, sysread/syswrite is the
safest way to do i/o. you control the buffering yourself and skip any
wierdnesses with stdio.
perl's write is the format stuff and no one uses that with sockets. in
fact i have never use write/format in 9 years. and there are modules
which do a better job of it as well so it is a dead feature in my
book. perl6 is completely dropping it to a module and out of the
language core.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Tue, 29 Oct 2002 14:25:57 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: amateur socketeer: long wait
Message-Id: <3DBEE0C5.594B4E32@earthlink.net>
Leaffoot wrote:
>
> Hi,
>
> I am doing some socket-to-socket communication using io::socket, and
> this is my first time doing so, so bear with me. Sometimes the
> handshake and data transmittal occurs in less than a couple seconds,
> and sometimes it takes 10 minutes, for the same amount of data. I
> have a "last" statement that should kick me out if it's taking too
> long, but even so, it hangs for a very lengthy time before continuing.
> Does anyone know how I can get around this problem, or what is going
> on?? Here's the subs I use:
>
> sub sendstr {
> # pass in socket and string to send
> my @args = @_;
> my $sock = shift @args;
> my $str = shift @args;
>
> print $sock "$str" || confess "I can't send the str $str!\n";
> return 1;
> }
I note that here, you're using 'print $sock' ... print does output
buffering.
Also... you've got a precedence error -- the print statement parses as:
print $sock ("$str" || confess "I can't send the str $str!\n");
Which is not likely what you want. Change it to:
print $sock "$str" or confess "I can't send the str $str!\n";
Also, with socket communication, you most likely do not really want line
buffering semantics... turn on autoflush for the socket when you create
it ($sock->autoflush(1)).
> sub recstr {
> # pass in socket and length of line, returns str & flag if closed sock
> my @args = @_;
> my $sock = shift @args;
> my $len = shift @args;
As uri said, this would be more readable as:
my ($sock, $len) = @_;
> my $stuff = "";
> my $comeback_l = 0;
> my $length_l = 0;
> my $out = 0;
> my $flag = 0;
Declare these at the places that use them, rather than up here.
> my $time_start = new Benchmark;
Although there's nothing really *wrong* with the indirect object syntax
(method target(args)), you code will be more readable, and less prone to
subtle errors, if you always use the direct object syntax
(target->method(args)) ... so, you might want to write this as:
my $time_start = Benchmark->new;
It's your choice, though... TIMTOWTDI
> while($length_l = sysread $sock, $comeback_l, $len) {
This would be a good place to declare $length_l, $comeback_l, and $len.
> if(!defined $length_l) {
> confess "System read error: $!\n";
> }
You're only inside the while loop when $length_l is true. If it were
undefined, it would be false, and fall out of the loop.
So this needs to be *after* the while loop.
Also, due to scoping, that means that $length_l needs to be declared
just before the while loop, not inside the while condition.
> while($length_l) {
>
> #writes entire line, then length is cleared, next loop of
> read occurs
> my $written = syswrite STDOUT, $comeback_l,$length_l;
> $stuff = $stuff.$comeback_l;
> die "System error: $!\n" unless defined $written;
>
> #makes sure entire line was written entirely
> $length_l -= $written;
>
> }
Is there any reason why you don't simply 'print STDOUT $comeback_l;' ?
Of course, you might want to do STDOUT->autoflush(1) at some earlier
point in your program, but that's minor.
> if ($comeback_l =~ m/END OF REPORT/) {
> last;
> }
> if ($comeback_l =~ m/^\d+CERRUR\d+.*/) {
> last;
> }
These could be combined into one expression:
last if $comeback_l =~ /^\d+CERRUR\d|END OF REPORT/;
(There's no need to check for more than one \d, or for .*, after CERRUR)
> my $time_end = new Benchmark;
> my $this_time = timediff($time_end, $time_start);
The $time_start variable was set before entering the loop... if you go
through the loop twice, $this_time won't be the time for this most
recent sysread, but will be the sum of the times for both sysreads.
> my $timestr = timestr($this_time);
> print "Time taken to send & retrieve: ", timestr($this_time),
> "\n";
> if($timestr =~ m/\b(\d+) wallclock secs/) {
> if($1 > 50) {
> close($sock);
> $flag = 1;
> last;
> }
> }
It looks like you're trying to time things out after 50 seconds...
But if the last sysread itself takes more than that, or blocks
indefinitely, you've got a problem. Perhaps you should be using
IO::Select?
> }
>
> return $stuff,$flag;
> }
>
> Then after connecting, I call:
> &sendstr($socket, $dialstr) || die "Can't send for $ur";
>
> my ($report,$flag) = &recstr($socket, $linesize);
> if($flag) {$socket = &connect_to_socket($logfile);}
>
> And it hangs on after the last data is transferred, as if still trying
> to read. Can anyone help? Thanks!!
>
> Becka Louden
--
my $n = 2; print +(split //, 'e,4c3H r ktulrnsJ2tPaeh'
."\n1oa! er")[map $n = ($n * 24 + 30) % 31, (42) x 26]
------------------------------
Date: Tue, 29 Oct 2002 14:06:18 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Can package names be supplied on-the-fly?
Message-Id: <3DBEDC2A.BBD10C85@earthlink.net>
Randal L. Schwartz wrote:
>
> >>>>> "Benjamin" == Benjamin Goldberg <goldbb2@earthlink.net> writes:
>
> Benjamin> foreach (sort keys %{ $pkg . "::" } ) {
> Benjamin> $reprocess_subs{$_}++ if
> Benjamin> $_ =~ /^reprocess_/ and defined *{$_}{CODE};
> Benjamin> }
>
> And even simpler, a routine that can be called from any package:
>
> sub my_reprocess_subs {
> my $caller_package = shift || caller;
> no strict 'refs';
> grep { /^reprocess_/ and defined *{$_}{CODE} }
> sort keys %{ $caller_package . "::" }
> }
The keys of %Foo:: are not fully qualified... they are strings like
"bar". Checking for defined *{"bar"}{CODE} will check in the *current*
package. That was why I qualified my last suggested change with
pointing out that that $pkg always comes from __PACKAGE__.
--
my $n = 2; print +(split //, 'e,4c3H r ktulrnsJ2tPaeh'
."\n1oa! er")[map $n = ($n * 24 + 30) % 31, (42) x 26]
------------------------------
Date: Tue, 29 Oct 2002 14:52:30 -0800
From: Calvin Wong <cawong@cisco.com>
Subject: Creating a Microsoft Word file with Win32::OLE
Message-Id: <3DBF112E.157BA2C5@cisco.com>
Hello everyone,
I have a problem that I hope you guys can help me with.
What I am trying to do is create a word document and
populate it with data using Win32::OLE module.
I have grab code from many different web pages and none
of them seem to work.
Can someone please post a simple code to use Microsoft
word to open a new document, place "hello world" in it, save
the file, and then close Microsoft word.
Everytime when I do it, it opens Microword word places the
text in it and opens a dialog box to save the file, but it doesn't
save the file nor does it close microsoft word.
Calvin
------------------------------
Date: 29 Oct 2002 13:38:19 -0800
From: slimclity@hotmail.com (SlimClity)
Subject: Re: Cut characters from string
Message-Id: <3c209c5f.0210291338.2aa2da20@posting.google.com>
Everbody thanks their reply!
------------------------------
Date: Tue, 29 Oct 2002 20:30:48 -0000
From: claird@lairds.com (Cameron Laird)
Subject: Deep misunderstanding of recursion
Message-Id: <urtrvo7lv16824@corp.supernews.com>
I have a small program that behaves differently than I expect.
I'd generally attribute that to a misunderstanding on my part.
In this case, my experiments lead me to model Perl as "returning
too far". Who can help me understand this better?
$thing = 0;
sub a {
$candidate = $_[0];
if (($candidate == 1001) && !$thing) {
b();
return a($candidate);
}
if ($candidate == 1001) {
return 0;
}
return 1;
}
sub b {
a(3);
lthing = 1;
}
print a(1001), "\n";
The output I see is 1. I expect 0. The difference
appears to depend on having a invoke b which invokes a.
--
Cameron Laird <Cameron@Lairds.com>
Business: http://www.Phaseit.net
Personal: http://phaseit.net/claird/home.html
------------------------------
Date: 29 Oct 2002 20:50:30 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@post.rwth-aachen.de>
Subject: Re: Deep misunderstanding of recursion
Message-Id: <apmsam$bnp$1@nets3.rz.RWTH-Aachen.DE>
Also sprach Cameron Laird:
> I have a small program that behaves differently than I expect.
> I'd generally attribute that to a misunderstanding on my part.
> In this case, my experiments lead me to model Perl as "returning
> too far". Who can help me understand this better?
> $thing = 0;
>
> sub a {
> $candidate = $_[0];
> if (($candidate == 1001) && !$thing) {
> b();
> return a($candidate);
> }
> if ($candidate == 1001) {
> return 0;
> }
> return 1;
> }
>
> sub b {
> a(3);
> lthing = 1;
> }
>
>
> print a(1001), "\n";
> The output I see is 1. I expect 0. The difference
> appears to depend on having a invoke b which invokes a.
The reason is only indirectly related to recursion. The problem with
your code is that $candidate is a _global_ variable. That means, calling
a() from b() will change the previous value of $candidate so that the
second if-condition evaluates to false so that 1 is returned.
This is a good example why variables should be forced into the smallest
possible scope. If you prefix $candidate with my() (or local(), but
don't do that), the behaviour will be as you expected since each
instance of the subroutine has its own variable $candidate.
Tassilo
--
$_=q!",}])(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus;})(rekcah{lrePbus;})(lreP{rehtonabus;})(rehtona{tsuJbus!;
$_=reverse;s/sub/(reverse"bus").chr(32)/xge;tr~\n~~d;eval;
------------------------------
Date: Tue, 29 Oct 2002 21:11:35 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: Deep misunderstanding of recursion
Message-Id: <53utru420a3a4noq2pa0doc6vl7ijde54g@4ax.com>
Cameron Laird wrote:
> sub a {
> $candidate = $_[0];
> if (($candidate == 1001) && !$thing) {
> b();
> return a($candidate);
> }
> if ($candidate == 1001) {
> return 0;
> }
> return 1;
> }
>
> sub b {
> a(3);
> lthing = 1;
typo.
> }
>
>
> print a(1001), "\n";
>The output I see is 1. I expect 0. The difference
>appears to depend on having a invoke b which invokes a.
You haven't localised your variable $candidate. Now, it's a global. That
means that the second invocation of sub a will change the value for the
first invocation as well. That means that after a(3) returns, $candidate
is 3, not 1001.
The modern fix would be to use
sub a {
my $candidate = $_[0];
...
}
which lexically limits the scope to this block. (Meaning that when
recursing, the second invocation uses a different variable $candidate
than the first, and no subs you call from the sub can see this
variable.)
--
Bart.
------------------------------
Date: 29 Oct 2002 16:19:55 -0500
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: Deep misunderstanding of recursion
Message-Id: <m3r8e9nds4.fsf@mumonkan.sunstarsys.com>
claird@lairds.com (Cameron Laird) writes:
> $thing = 0;
>
> sub a {
> $candidate = $_[0];
^^
my
> if (($candidate == 1001) && !$thing) {
> b();
> return a($candidate);
> }
> if ($candidate == 1001) {
> return 0;
> }
> return 1;
> }
>
> sub b {
> a(3);
> lthing = 1;
^
$
> }
>
>
> print a(1001), "\n";
> The output I see is 1. I expect 0. The difference
> appears to depend on having a invoke b which invokes a.
With the above changes, I get 0. I think the problem
you are having is related to the scope of $candidate.
In you original code, $candidate is a global variable.
The execution order of your code goes roughly like this:
a(10001) -> ($candidate = 1001) -> if(true && true) ->
b()->a(3)-> ($candidate = 3) -> if(false) x 2 ->
return 1 -> ($thing = 1) -> return(
a(3) -> ($candidate = 3) -> 1)
By using "my $candidate" instead, each time you enter a(), you
get a brand new $candidate variable to work with. my $candidate
is basically a stack of values, that are pushed & popped on entry
and exit into a(). If you trace through the execution order now,
you have to keep track of which value of $candidate is active. At
the top-level call, it's 1001, but at the second level call (via b),
it's 3. And as b() returns control back to a, the original
$candidate value (1001) is restored. Now that puts you at the
"return a($candidate);" line with the value of $candidate being 1001,
not 3 (but also with $thing = 1, so you won't reenter the first if()
statement ad infinitum).
Does this help any?
--
Joe Schaefer "The surest protection against temptation is cowardice."
--Mark Twain
------------------------------
Date: Tue, 29 Oct 2002 22:01:41 -0000
From: claird@lairds.com (Cameron Laird)
Subject: Re: Deep misunderstanding of recursion
Message-Id: <uru1a59f34gld8@corp.supernews.com>
In article <53utru420a3a4noq2pa0doc6vl7ijde54g@4ax.com>,
Bart Lateur <bart.lateur@pandora.be> wrote:
.
.
.
>You haven't localised your variable $candidate. Now, it's a global. That
>means that the second invocation of sub a will change the value for the
>first invocation as well. That means that after a(3) returns, $candidate
>is 3, not 1001.
>
>The modern fix would be to use
>
> sub a {
> my $candidate = $_[0];
> ...
> }
>
>which lexically limits the scope to this block. (Meaning that when
>recursing, the second invocation uses a different variable $candidate
>than the first, and no subs you call from the sub can see this
>variable.)
.
.
.
Of course!
I repeat your explanation in full simply because I
think its precision merits repetition. Indeed: I
had not localized $candidate. My thanks to you.
--
Cameron Laird <Cameron@Lairds.com>
Business: http://www.Phaseit.net
Personal: http://phaseit.net/claird/home.html
------------------------------
Date: 29 Oct 2002 22:14:24 GMT
From: "Stephen O. Lidie" <lusol@Pandora.cc.lehigh.edu>
Subject: Re: Deep misunderstanding of recursion
Message-Id: <apn180$c7i@fidoii.CC.Lehigh.EDU>
Cameron Laird <claird@lairds.com> wrote:
> In article <53utru420a3a4noq2pa0doc6vl7ijde54g@4ax.com>,
> Bart Lateur <bart.lateur@pandora.be> wrote:
> .
> .
> .
>>You haven't localised your variable $candidate. Now, it's a global. That
>>means that the second invocation of sub a will change the value for the
>>first invocation as well. That means that after a(3) returns, $candidate
>>is 3, not 1001.
>>
>>The modern fix would be to use
>>
>> sub a {
>> my $candidate = $_[0];
>> ...
>> }
>>
>>which lexically limits the scope to this block. (Meaning that when
>>recursing, the second invocation uses a different variable $candidate
>>than the first, and no subs you call from the sub can see this
>>variable.)
> .
> .
> .
> Of course!
> I repeat your explanation in full simply because I
> think its precision merits repetition. Indeed: I
> had not localized $candidate. My thanks to you.
A fundamental difference between Tcl and Perl - unless told otherwise,
Tcl assumes "local", while Perl assumes "global". Ignoring fully
qualified variables names, upvar, etc. ...
Steve
--
@_=map{eval"100${_}"}split/!/,'/5!*2!+$]!/10+$]';use Tk;$m=tkinit;$t='just an'.
'other perl hacker';$z='createText';$c=$m->Canvas(-wi,$_[1],-he,25)->grid;$c->$
z(@_[2,3],-te,$t,-fi,'gray50');$c->$z($_[2]-$],$_[3]-$],-te,$t);$m->bind('<En'.
'ter>',sub{$y=int(rand($m->screenheight));$m->geometry("+$y+$y")});MainLoop;
------------------------------
Date: Tue, 29 Oct 2002 21:21:51 GMT
From: "Ivan" <ivan@diprima.freeserve.co.uk>
Subject: files in perl
Message-Id: <PZCv9.93942$%M1.2377956@twister2.libero.it>
what is the best/easiest way to see if a file exist in perl? even without
opening it.
thanks
------------------------------
Date: 29 Oct 2002 21:25:01 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@post.rwth-aachen.de>
Subject: Re: files in perl
Message-Id: <apmubd$e1f$1@nets3.rz.RWTH-Aachen.DE>
Also sprach Ivan:
> what is the best/easiest way to see if a file exist in perl? even without
> opening it.
See 'perldoc -f -X' for a long list of file test operators. '-e' is the
one to look for. But be aware of race conditions...they are a standard
ingredient for such a thing.
Tassilo
--
$_=q!",}])(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus;})(rekcah{lrePbus;})(lreP{rehtonabus;})(rehtona{tsuJbus!;
$_=reverse;s/sub/(reverse"bus").chr(32)/xge;tr~\n~~d;eval;
------------------------------
Date: 29 Oct 2002 09:05:10 -0800
From: fredrik.andersson@avionics.saab.se (Fredrik Andersson)
Subject: Pascal comment RE
Message-Id: <9df19bab.0210290905.5a7bdeb1@posting.google.com>
Hi, I'm trying to increment two variables ($line_old and $line_number_old)
if I don't get the below two matches:
\S+
A line contaning nothing
^\(\*.*\*\)
A line that only contains a Pascal comment (* text *)
The Pascal comment one don't work. I have tried this ones
with now luck.
\s*[^(][^*].*[^*][)^]
[^\(\*.*\*\)]
Please help!
Best Regards
Fredrik Andersson
while (<FH>) {
$line_number = $line_number +1;
$line = $_;
chomp $line;
if (/TESTVECTOR\[\s[0-9]{1,3}\]/) {
print "$line_number_old $line_old\n";
} elsif (\S+)|^\(\*.*\*\)/) { # <- this is the line!
$line_old = $line;
$line_number_old = $line_number;
}
}
------------------------------
Date: Tue, 29 Oct 2002 21:25:25 -0800
From: "NewsGroups" <mail@eircom.net>
Subject: Perl Ping script for Win 98
Message-Id: <apmqpg$gi7$1@dorito.esatclear.ie>
Does anyone have a perl script that will ping a list of machines and report
to a pingreport.txt file??
I need it for windows 98
------------------------------
Date: Tue, 29 Oct 2002 16:53:04 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Perl Ping script for Win 98
Message-Id: <3DBF0340.EE998177@earthlink.net>
NewsGroups wrote:
>
> Does anyone have a perl script that will ping a list of machines and
> report to a pingreport.txt file??
>
> I need it for windows 98
http://groups.google.com/groups?q=group:*perl+ping
--
my $n = 2; print +(split //, 'e,4c3H r ktulrnsJ2tPaeh'
."\n1oa! er")[map $n = ($n * 24 + 30) % 31, (42) x 26]
------------------------------
Date: Tue, 29 Oct 2002 20:23:31 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: pls help on array list pass to main from a function
Message-Id: <3DBEEE3B.C6783725@acm.org>
PT wrote:
>
> say if I have a configuration file looks as below:
>
> "filename1", "string a1", "string b1"
> "*.log", "string a", "string b"
>
> How can I pass the value of three parameters $filename, $stringA, $stringB
> into an array list and to be called by MainProgram(@ListName)?
> ** including the wildcard * expand?
perldoc -f glob
John
--
use Perl;
program
fulfillment
------------------------------
Date: Tue, 29 Oct 2002 16:35:32 -0000
From: "David K. Wall" <usenet@dwall.fastmail.fm>
Subject: Re: POST filter
Message-Id: <Xns92B675EBE6C0Adkwwashere@216.168.3.30>
hsasshofer <herbert@sasshofer.com> wrote on 29 Oct 2002:
> I want to write a CGI-script that filters some values of a POST
> request and then re-POSTS the data to another script. How do I
> transfer the param() values to a HTTP:request with the new post? Do I
> have to build an escaped string for the $request->content() call
> manually or is there an easier way to pass a hash to a POST request?
Look in lwpcook -- it has an example of posting data to a web server with
Perl. If you don't have LWP installed on your system, you'll need to get it
from CPAN. (I don't remember if LWP is in the standard modules or not.)
Here's a reference from perldoc.com:
http://www.perldoc.com/perl5.6/lib/lwpcook.html
--
David K. Wall - usenet@dwall.fastmail.fm
"Oook."
------------------------------
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.
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 4043
***************************************