[19087] in Perl-Users-Digest
Perl-Users Digest, Issue: 1282 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jul 11 04:02:13 2001
Date: Wed, 11 Jul 2001 01:01:45 -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: <994838505-v10-i1282@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Wed, 11 Jul 2001 Volume: 10 Number: 1282
Today's topics:
Why is var using same memory location (Brian)
Re: Why is var using same memory location <uri@sysarch.com>
Re: Why is var using same memory location <mjcarman@home.com>
Re: Why is var using same memory location (Brian)
why is Vars an undefined subroutine? (TuNNe|ing)
Re: why is Vars an undefined subroutine? <pne-news-20010711@newton.digitalspace.net>
WIN32 & Flock??? <admin@nospam.gidesigns.f2s.com>
Re: WIN32 & Flock??? <mh2@isis.co.za>
Re: WIN32 & Flock??? <dbe@wgn.net>
Working with packed strings <cjmackie@princeton.edu>
Re: Working with packed strings <krahnj@acm.org>
Re: Working with packed strings <pne-news-20010711@newton.digitalspace.net>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 9 Jul 2001 12:34:12 -0700
From: redace1919@yahoo.com (Brian)
Subject: Why is var using same memory location
Message-Id: <d550bb5.0107091134.79eabc59@posting.google.com>
I don't get this.
#CODE START
foreach my $tc (@{$tuxconfig[$cfgFile]}) {
$tc = "a"; # This is a test
}
#CODE END
Now after I run through this loop, my entire array is filled with
"a"'s. So if I modify $tc, I'm actually modifying the array. I don't
want this.
Here is how I define this @tuxconfig array.
#CODE START
my $x = 0;
foreach (@TFs) {
$ENV{'TUXCONFIG'} = $_;
$tuxconfig[$x] = [`tmunloadcf`];
$x++;
}
#CODE END
tmunloadcf is a UNIX program that returns a few hundered lines of code
and this will normally run twice (@TFs has two elements)
I debugged this and \$tc has the same memory location as
\$tuxconfig[0][0].
I don't understand why.... can someone fill me in?
Regards,
Brian
------------------------------
Date: Mon, 09 Jul 2001 19:57:25 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Why is var using same memory location
Message-Id: <x7ofqt510c.fsf@home.sysarch.com>
>>>>> "B" == Brian <redace1919@yahoo.com> writes:
B> I don't get this.
B> #CODE START
B> foreach my $tc (@{$tuxconfig[$cfgFile]}) {
B> $tc = "a"; # This is a test
B> }
B> #CODE END
B> Now after I run through this loop, my entire array is filled with
B> "a"'s. So if I modify $tc, I'm actually modifying the array. I don't
B> want this.
then don't mdoify $tc. foreach is DEFINED to alias its looping variable
to each element in turn just so you can do that. if you don't want to
have it modified, then why are you modifying it?
uri
--
Uri Guttman --------- uri@sysarch.com ---------- http://www.sysarch.com
SYStems ARCHitecture and Stem Development ------ http://www.stemsystems.com
Learn Advanced Object Oriented Perl from Damian Conway - Boston, July 10-11
Class and Registration info: http://www.sysarch.com/perl/OOP_class.html
------------------------------
Date: Mon, 09 Jul 2001 15:31:18 -0500
From: Michael Carman <mjcarman@home.com>
Subject: Re: Why is var using same memory location
Message-Id: <3B4A1496.B1D2F97E@home.com>
Brian wrote:
>
> I don't get this.
>
> foreach my $tc (@{$tuxconfig[$cfgFile]}) {
> $tc = "a"; # This is a test
> }
>
> Now after I run through this loop, my entire array is filled with
> "a"'s. So if I modify $tc, I'm actually modifying the array.
Correct.
> I don't want this.
Then don't modify $tc, or make a copy and mangle that instead.
> Here is how I define this @tuxconfig array.
Irrelevant.
> I debugged this and \$tc has the same memory location as
> \$tuxconfig[0][0].
Yep.
> I don't understand why.... can someone fill me in?
This is a design feature of Perl. From the description of foreach in the
perlsyn manpage:
If any element of LIST is an lvalue, you can modify it by
modifying VAR inside the loop. That's because the foreach
loop index variable is an implicit alias for each item in
the list that you're looping over.
Most of the time, this is useful (or at least harmless). But
occasionally you *don't* want changes to propagate back, and in those
situations you have to takes steps to avoid it.
-mjc
------------------------------
Date: 10 Jul 2001 08:04:49 -0700
From: redace1919@yahoo.com (Brian)
Subject: Re: Why is var using same memory location
Message-Id: <d550bb5.0107100704.36b5ddb1@posting.google.com>
Ahhh... thanks. I was getting pretty confused. I was starting to
question my understanding of references.
Michael Carman <mjcarman@home.com> wrote in message news:<3B4A1496.B1D2F97E@home.com>...
>
> This is a design feature of Perl. From the description of foreach in the
> perlsyn manpage:
>
> If any element of LIST is an lvalue, you can modify it by
> modifying VAR inside the loop. That's because the foreach
> loop index variable is an implicit alias for each item in
> the list that you're looping over.
>
> Most of the time, this is useful (or at least harmless). But
> occasionally you *don't* want changes to propagate back, and in those
> situations you have to takes steps to avoid it.
>
> -mjc
------------------------------
Date: Wed, 11 Jul 2001 01:54:40 GMT
From: troll@gimptroll.com (TuNNe|ing)
Subject: why is Vars an undefined subroutine?
Message-Id: <3b4bce89.169861728@news>
I'm trying really hard to get out of the "do it yourself" mode. So
before I slip down the learning curve and break my neck. Is it me or
what?
All i want to do is print all the submitted data.
I get "Undefined subroutine CGI::Vars "
Thanks,
TuNNe|ing
[begin code]
#!/usr/bin/perl -w
use CGI::Carp "fatalsToBrowser"; # errors to the browser
use CGI;
my $q = new CGI;
my %params = $q->Vars;
print "Content-type: text/plain\n\n";
print "The Form data is:\n";
while (my ($key,$value) = each %params) {
print "$key = $value\n";
}
exit;
[/end code]
------------------------------
Date: Wed, 11 Jul 2001 06:57:39 +0200
From: Philip Newton <pne-news-20010711@newton.digitalspace.net>
Subject: Re: why is Vars an undefined subroutine?
Message-Id: <tlmnktcqkhrnp3oc88iot2abupmul2unnc@4ax.com>
On Wed, 11 Jul 2001 01:54:40 GMT, troll@gimptroll.com (TuNNe|ing) wrote:
> use CGI;
> my $q = new CGI;
> my %params = $q->Vars;
Where did you get the method 'Vars' from? It's not in my copy of the
CGI.pm documentation. (Hint, hint[1].)
Cheers,
Philip
[1] Bigger hint: try the section labelled "FETCHING THE NAMES OF ALL THE
PARAMETERS PASSED TO YOUR SCRIPT".
Side note: Note that parameters can take on multiple values, in which
case you'll need to use an array to catch them all. It's not simple
(key, value) pairs since you keys can occur multiple times in the form
data.
--
Philip Newton <nospam.newton@gmx.li>
That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.
------------------------------
Date: Tue, 10 Jul 2001 05:08:03 +0800
From: "GI designs" <admin@nospam.gidesigns.f2s.com>
Subject: WIN32 & Flock???
Message-Id: <3b4a1cb0.0@news2.tm.net.my>
I am a just beginning to learn perl on my Windows ME PC and using Apache Web
server to develop my site (locally on my PC).
I understand that flock() is unimplemented in Win32 and so I wonder if
someone out there can tell me how i can fix or workaround to this piece of
code:
sub lock
{
$i = 1;
while ($i < 1000)
{
if (flock(@_[0], 2 | 4))
{
return 0;
}
$i++;
}
return 1;
}
Anyway, I just need a workaround to make this script work on my PC... when
uploading to my host server, I can use the flock() as in the original
script...
Thanks for your help.
Jay
------------------------------
Date: Tue, 10 Jul 2001 16:25:14 +0200
From: "Web Wizards - PHP/PERL Developers" <mh2@isis.co.za>
Subject: Re: WIN32 & Flock???
Message-Id: <9if381$mud$1@ctb-nnrp1.saix.net>
Hi,
Hmm, no Win32 flock is a bind! I ALWAYS forget to remove it!
What I usually do in complex scripts is define a variable $FLOCK_ENABLED =
0; or something in the config file.
Then I say something like
if ( $FLOCK_ENABLED ) { flock(...); }
Then I can move between systems that support flock or not by simply
modifying $FLOCK_ENABLED is one single place, the config file.
Of course, this doesn't help if your script needs file locking, it'll have
problems on a Win32 system. I typiclly just do what you do, basic tests on
Win98 platform with Apache, then actually run the thing on a UNIX box, so
this works great.
Regards,
Mark
--------------------------------------------------------------
Web Wizards - PHP/PERL Developers
E-Mail: mh2 at isis.co.za
--------------------------------------------------------------
"GI designs" <admin@nospam.gidesigns.f2s.com> wrote in message
news:3b4a1cb0.0@news2.tm.net.my...
> I am a just beginning to learn perl on my Windows ME PC and using Apache
Web
> server to develop my site (locally on my PC).
>
> I understand that flock() is unimplemented in Win32 and so I wonder if
> someone out there can tell me how i can fix or workaround to this piece of
> code:
>
> sub lock
> {
> $i = 1;
> while ($i < 1000)
> {
> if (flock(@_[0], 2 | 4))
> {
> return 0;
> }
> $i++;
> }
> return 1;
> }
>
> Anyway, I just need a workaround to make this script work on my PC... when
> uploading to my host server, I can use the flock() as in the original
> script...
>
> Thanks for your help.
>
> Jay
>
>
------------------------------
Date: Tue, 10 Jul 2001 16:41:11 -0700
From: "$Bill Luebkert" <dbe@wgn.net>
Subject: Re: WIN32 & Flock???
Message-Id: <3B4B9297.B3DC0FC7@wgn.net>
GI designs wrote:
>
> I am a just beginning to learn perl on my Windows ME PC and using Apache Web
> server to develop my site (locally on my PC).
>
> I understand that flock() is unimplemented in Win32 and so I wonder if
> someone out there can tell me how i can fix or workaround to this piece of
> code:
>
> sub lock
> {
> $i = 1;
return 0 if $^O =~ /Win32/i);
> while ($i < 1000)
> {
> if (flock(@_[0], 2 | 4))
> {
> return 0;
> }
> $i++;
> }
> return 1;
> }
>
> Anyway, I just need a workaround to make this script work on my PC... when
> uploading to my host server, I can use the flock() as in the original
> script...
--
,-/- __ _ _ $Bill Luebkert ICQ=14439852
(_/ / ) // // DBE Collectibles Mailto:dbe@todbe.com
/ ) /--< o // // http://dbecoll.webjump.com/ (Free Perl site)
-/-' /___/_<_</_</_ Castle of Medieval Myth & Magic http://www.todbe.com/
------------------------------
Date: Tue, 10 Jul 2001 18:13:42 -0400
From: "Christopher J. Mackie" <cjmackie@princeton.edu>
Subject: Working with packed strings
Message-Id: <9ifumm$98n$1@cnn.Princeton.EDU>
I'm trying to tweak performance in some code to process very large binary
files, and I'm wondering if it's safe to substring a packed string, or if I
could end up in trouble if the string contains the binary code that would
normally be interpreted as string-end, or some other non-ascii weirdness.
i.e., will the following always put the first 15 bytes of $buffer into
$subsetted, no matter what info $buffer contains?
sysread(INFILE, $buffer, 8, 32);
$subsetted=substr($buffer, 0, 15);
------------------------------
Date: Tue, 10 Jul 2001 23:16:56 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Working with packed strings
Message-Id: <3B4B8CE7.783BCEBD@acm.org>
"Christopher J. Mackie" wrote:
>
> I'm trying to tweak performance in some code to process very large binary
> files, and I'm wondering if it's safe to substring a packed string, or if I
> could end up in trouble if the string contains the binary code that would
> normally be interpreted as string-end, or some other non-ascii weirdness.
>
> i.e., will the following always put the first 15 bytes of $buffer into
> $subsetted, no matter what info $buffer contains?
Yes.
> sysread(INFILE, $buffer, 8, 32);
> $subsetted=substr($buffer, 0, 15);
John
--
use Perl;
program
fulfillment
------------------------------
Date: Wed, 11 Jul 2001 06:57:38 +0200
From: Philip Newton <pne-news-20010711@newton.digitalspace.net>
Subject: Re: Working with packed strings
Message-Id: <nimnktojndap4qsbmrfstuiia6io68jfrf@4ax.com>
On Tue, 10 Jul 2001 18:13:42 -0400, "Christopher J. Mackie"
<cjmackie@princeton.edu> wrote:
> I'm trying to tweak performance in some code to process very large binary
> files, and I'm wondering if it's safe to substring a packed string,
Yes.
> or if I could end up in trouble if the string contains the binary code that
> would normally be interpreted as string-end,
No; Perl strings know how long they are and don't need an explicit
terminator. They can contain "\0" characters (which is the 'string end'
marker in C).
> or some other non-ascii weirdness.
Shouldn't be a problem.
> i.e., will the following always put the first 15 bytes of $buffer into
> $subsetted, no matter what info $buffer contains?
>
> sysread(INFILE, $buffer, 8, 32);
> $subsetted=substr($buffer, 0, 15);
Yes, as long as $buffer is at least 15 bytes long, of course.
Cheers,
Philip
--
Philip Newton <nospam.newton@gmx.li>
That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.
------------------------------
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 1282
***************************************