[19983] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2178 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Nov 22 03:05:50 2001

Date: Thu, 22 Nov 2001 00:05:08 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <1006416308-v10-i2178@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Thu, 22 Nov 2001     Volume: 10 Number: 2178

Today's topics:
    Re: Capture stdout from subs (Malcolm Dew-Jones)
    Re: File upload in Internet Explorer using Perl (Malcolm Dew-Jones)
    Re: help w/ error: 'package does not return true' <aleppard@austrics.com.au>
    Re: help with member program (Malcolm Dew-Jones)
    Re: How can i load my config setting from a file ? (Malcolm Dew-Jones)
    Re: perlcc + require'd files (Malcolm Dew-Jones)
        replacing CGI.pm  delete() <perl@cableone.net>
    Re: result pages (10 by 10) (Malcolm Dew-Jones)
        Time::localtime problem (gaius.petronius)
    Re: Traversing directories (Tad McClellan)
    Re: Traversing directories (Tad McClellan)
    Re: variable scope <matthew.garrish@sympatico.ca>
    Re: variable scope <uri@stemsystems.com>
    Re: variable scope <uri@stemsystems.com>
    Re: variable scope <mgjv@tradingpost.com.au>
    Re: variable scope <joe+usenet@sunstarsys.com>
    Re: variable scope <ahamm@programmer.net>
    Re: variable scope <ahamm@programmer.net>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 21 Nov 2001 11:50:29 -0800
From: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
Subject: Re: Capture stdout from subs
Message-Id: <3bfc0585@news.victoria.tc.ca>

Marco Guazzone (sguazt@infodrome.net) wrote:
: Hi,
:     suppose you have a package with a method that write
:     some text to stdout, say:
(snip)

:     Now suppose you have another package that want to
:     capture output produced by Writer->write() and store

The "tie" function is what you want.

Read up on TIEHANDLE in the perltie.pod document ( it's in your perl
directory tree, or run the command "perldoc perltie"). 


You need to provide a TIEHANDLE function and a PRINT function.  The PRINT
function can just save each line into a global variable so other parts of
the program can read the data when they want to.




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

Date: 21 Nov 2001 14:36:11 -0800
From: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
Subject: Re: File upload in Internet Explorer using Perl
Message-Id: <3bfc2c5b@news.victoria.tc.ca>

Nitin Ghai (ghainitin@yahoo.com) wrote:
: Hi,
: In our applcation we are using the file upload utility of Perl. The
: files are uploaded to a UNIX server and the application is located on
: another server.
: We are having problems in calculating the size of the file in IE, the
: same code is working in NN4.7. But when we try to calculate the file
: size using -s($filename), it is giving "" as size for all the files in
: IE(as it is not able to access the file; 'fileexists' option returns
: "" in IE, while in NN the same command returns 1 , thus the
: conclusion!).

(What conclusion?)

Are you using CGI.pm ?

If you are _not_ then I assume your file upload code is broken.  The
format of the form-data sent by NN is not identical to that sent by IE, so
your perl code may be able to parse the NN data but unable to parse the IE
data, and that would explain why you don't get a file stored on the server
when it's uploaded from IE.

If you _are_ using CGI.pm then I would not expect this to be the
explanation. 

In any case, you would probably need to post some code for us to see to
get a more specific answer...


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

Date: Thu, 22 Nov 2001 17:00:10 +1030
From: Andrew Leppard <aleppard@austrics.com.au>
Subject: Re: help w/ error: 'package does not return true'
Message-Id: <3BFC9B72.1076F0EE@austrics.com.au>

I cut & paste this code (minus the 'use Text::PDF::API' because I dont
have that module) and I didnt get the error you mentioned.

Perhaps the problem is related to Text::PDF::API - maybe thats screwing
up the code?

Or it could be one of those 'invisible' charcters...try removing lines
one by one until the problem goes away. Then you'll know which line is
at fault.

bye
Andrew

> Here's my package code:
> 
> package PDFglobals;
> use Text::PDF::API;
> use strict;
> use Class::Struct;
> 
> struct (
>     font_encoding    => '$',
>     font_name        => '$',
>     xres            => '$',
>     yres            => '$',
>     pageX            => '$',
>     pageY            => '$',
>     font_size        => '$',
>     data_cols        => '$',
>     data_col_sizes    => '@',
>     bounding_rect    => '@',
>     pdf                => 'Text::PDF::API',
> );
> 
> 1;
> 
> The file is called 'PDFglobals.pm' and to use it, I have the line:
> 
> use PDFglobals;
> 
> in my calling script. Both files reside in the same directory. The error I
> get is:
> 
> File "PDFout-row.pl"; Line 13:  PDFglobals.pm did not return a true value
> 
> I'm running perl 5.6.0 on darwin 1.4.1 (Mac OS X v10.1.1).
> 
> Any help would be sincerely appreciated. Feel free to respond offline to
> brice@webprojkt.com
> 
> TIA,
> Brice Ruth


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

Date: 21 Nov 2001 14:48:34 -0800
From: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
Subject: Re: help with member program
Message-Id: <3bfc2f42@news.victoria.tc.ca>

Devon Perez (hoss@chungk.com) wrote:
: hi,
: im creating a service.. and lets say its a counter service

: i want each member to be able to log in and edit their counters.

: how can i do this without having to put a whole other counter managment
: program in every member directory??

Pass the member-id as a parameter to the program.  The program can then
use the member-id to figure out which counter file to edit. 

Note that the program will need to be able to edit all the counter files. 

Therefore, because the counter program can potentially be used by anyone
to edit anyone elses counter file, you must make sure the program is
protected somehow.  E.g. you could use .htpasswd to protect it, and then
the program would edit the file that belongs to the REMOTE_USER. 


On unix, you could also create a link to the program in each member
directory, so each member appears to get their own copy of the program.



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

Date: 21 Nov 2001 17:28:14 -0800
From: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
Subject: Re: How can i load my config setting from a file ?
Message-Id: <3bfc54ae@news.victoria.tc.ca>

Robert (robert_loui@yahoo.com) wrote:
: Hi all

: I have a program calling many files, and depended on some files located on
: different directories.  So at the beginig of my program i have some
: configuration setting like :

: my $MyFiles =$ENV{HOME}."/MyDir";
: my @search_path =("/opt/etc", $ENV{HOME}, '.');
: push @INC, "$ENV{HOME}/WORK/DATA";

: I want to put all these declarations in a separate file and load it when I
: am starting my program. But I don't know how can I load these configuration
: file. For example how can load these configuration if they were declared in
: a file called "myconfig.cfg".


require "myconfig.cfg";


You might need to specify the path

	require "/the/path/myconfig.cfg";

and if you can trust the current directory to be what you think it is
then

	require "./myconfig.cfg";

(Don't forget the 1; at the end of the file.)


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

Date: 21 Nov 2001 11:52:18 -0800
From: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
Subject: Re: perlcc + require'd files
Message-Id: <3bfc05f2@news.victoria.tc.ca>

Daniel Ellard,,, (ellard2@is04.fas.harvard.edu) wrote:
: I've got a perl program that is composed of a bunch of small packages
: that are all sucked into the main program via "require".  I'd like to
: use perlcc to speed up the execution of this program (I've had great
: success with perlcc for other programs).  Unfortunately, when I run
: perlcc on the "main" file, it only compiles the main file and doesn't
: touch the others.  (I was hoping that it would pull in the packages
: that this file requires, but that doesn't happen.)

: Any suggestions for how to compile the entire program?  I could
: rewrite it or do some preprocessing to create one monster file, and
: then compile it that way, but the guiding perl principles of laziness
: and impatience require that I make at least some attempt to find a
: better way...

I do not know, but I must ask - does perlcc recognize the "use" command?

k


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

Date: Thu, 22 Nov 2001 00:51:14 -0600
From: "Jason Gray" <perl@cableone.net>
Subject: replacing CGI.pm  delete()
Message-Id: <tvp83ebamun885@corp.supernews.com>

Is there anything I can replace the CGI.pm's delete() method with?

$q->delete('next_lot');

^ what can i replace that with if im not using cgi.pm, splice?

--
Regards,
Jason Gray

"Just Another Perl Programmer"




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

Date: 21 Nov 2001 15:15:42 -0800
From: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
Subject: Re: result pages (10 by 10)
Message-Id: <3bfc359e@news.victoria.tc.ca>

Jason Gray (perl@cableone.net) wrote:
: I'm currently trying to figure out how to display 10 results at a time then
: if there is more, a link will show and you'll be able to view the next 10
: results. Like if a file has 29 lines, I want to be able to view 3 pages. Any
: help would be good.

Most easily, your script needs to accept parameters that specify which
portion of the data to display. 

There are lots of possibilities, but personally I find it most useful to
have two parameters, one that specifies a page (default is page 1) and
another parameter that specifies a motion relative to the indicated page
(default is no motion). 

e.g.
	script.pl page=n

		display the requested page


	script.pl page=n next=next

		display the page that follows the indicated page, useful
		for NEXT links


	script.pl page=n next=prev

		display the page that precedes the indicated page, useful
		for PREV links



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

Date: 21 Nov 2001 21:26:54 -0800
From: rut@linuxmail.org (gaius.petronius)
Subject: Time::localtime problem
Message-Id: <188cd7b2.0111212126.2f11e1d4@posting.google.com>

On Sat, 05 Feb 2000 22:52:57 +1100 Darren Ward wrote:
> Can someone have a look at the snippet of code below and see if they can
> find my problem?
> > I'm trying to create a DNS style serial number such as yyyymmddxx
> > What it does is if the month is single digit it multiplies the year by
> 10 to add the preceding 0 and the same applies for the date but for some
> reason the mday parameter reckons it's never less than 10 which is
> impossible.
> > Todays the 5th here and it still fails the lt 10 test....
> > PS I'm not using %02d formatting because I'm joining them together to
> form a new integer that is passed back as a reurn.
> > i.e.    2000 2 5 becomes 20000 20 5 which joined becomes 20000205
> > TIA
> > $tm = localtime;
> $year = $tm->year;
> if ($year lt 1900) {
> $year = $tm->year + 1900;
> }
> $month = $tm->mon + 1;
> if ($tm->mon lt 10) {
> $year = $year * 10;
> }
> $day = $tm->mday;
> if ($day lt 10) {
> print "Inside Day lt 10";
> $month = $month * 10;
> } else {
> print "Missed Day lt 10";
> }
> 

I'm sorry I just cant leave this without comment - what *is* wrong
with:

  use Time::localtime;

  my $tm = localtime;

  $datestring = sprintf "%04d%02d%02d",$tm->year + 1900,
                                       $tm->mon + 1,
                                       $tm->mday;

You *can* treat this an integer simply by , er, treating it as an
integer

  $datestring++;

Will correctly give 20000206.  You might also be interested in looking
at
the function POSIX::strftime .

You have to bear in mind that anywhere you have a scalar whose value
*looks*
like a number than as far as Perl is concerned then it *is* a number
when
you go to use it as one .

Oh BTW that should be '<' rather than 'lt'.

/J\
-- 
Jonathan Stowe <jns@gellyfish.com>
<http://www.gellyfish.com>
** Uri Guttman - Have You CPANed Backward.pm Yet ? **

[root@ur root]# sed -e 's/^/<</' tmp.post
<<On Sat, 05 Feb 2000 22:52:57 +1100 Darren Ward wrote:
<<> Can someone have a look at the snippet of code below and see if
they can
<<> find my problem?
<<> > I'm trying to create a DNS style serial number such as
yyyymmddxx
<<> > What it does is if the month is single digit it multiplies the
year by
<<> 10 to add the preceding 0 and the same applies for the date but
for some
<<> reason the mday parameter reckons it's never less than 10 which is
<<> impossible.
<<> > Todays the 5th here and it still fails the lt 10 test....
<<> > PS I'm not using %02d formatting because I'm joining them
together to
<<> form a new integer that is passed back as a reurn.
<<> > i.e.    2000 2 5 becomes 20000 20 5 which joined becomes
20000205
<<> > TIA
<<> > $tm = localtime;
<<> $year = $tm->year;
<<> if ($year lt 1900) {
<<> $year = $tm->year + 1900;
<<> }
<<> $month = $tm->mon + 1;
<<> if ($tm->mon lt 10) {
<<> $year = $year * 10;
<<> }
<<> $day = $tm->mday;
<<> if ($day lt 10) {
<<> print "Inside Day lt 10";
<<> $month = $month * 10;
<<> } else {
<<> print "Missed Day lt 10";
<<> }
<<> 
<<
<<I'm sorry I just cant leave this without comment - what *is* wrong
with:
<<
<<  use Time::localtime;
<<
<<  my $tm = localtime;
<<
<<  $datestring = sprintf "%04d%02d%02d",$tm->year + 1900,
<<                                       $tm->mon + 1,
<<                                       $tm->mday;
<<
<<You *can* treat this an integer simply by , er, treating it as an
integer
<<
<<  $datestring++;
<<
<<Will correctly give 20000206.  You might also be interested in
looking at
<<the function POSIX::strftime .
<<
<<You have to bear in mind that anywhere you have a scalar whose value
*looks*
<<like a number than as far as Perl is concerned then it *is* a number
when
<<you go to use it as one .
<<
<<Oh BTW that should be '<' rather than 'lt'.
<<
<</J\
<<-- 
<<Jonathan Stowe <jns@gellyfish.com>
<<<http://www.gellyfish.com>
<<** Uri Guttman - Have You CPANed Backward.pm Yet ? **
<<
[root@ur root]# sed -e 's/^/>>/' tmp.post
>>On Sat, 05 Feb 2000 22:52:57 +1100 Darren Ward wrote:
>>> Can someone have a look at the snippet of code below and see if
they can
>>> find my problem?
>>> > I'm trying to create a DNS style serial number such as
yyyymmddxx
>>> > What it does is if the month is single digit it multiplies the
year by
>>> 10 to add the preceding 0 and the same applies for the date but
for some
>>> reason the mday parameter reckons it's never less than 10 which is
>>> impossible.
>>> > Todays the 5th here and it still fails the lt 10 test....
>>> > PS I'm not using %02d formatting because I'm joining them
together to
>>> form a new integer that is passed back as a reurn.
>>> > i.e.    2000 2 5 becomes 20000 20 5 which joined becomes
20000205
>>> > TIA
>>> > $tm = localtime;
>>> $year = $tm->year;
>>> if ($year lt 1900) {
>>> $year = $tm->year + 1900;
>>> }
>>> $month = $tm->mon + 1;
>>> if ($tm->mon lt 10) {
>>> $year = $year * 10;
>>> }
>>> $day = $tm->mday;
>>> if ($day lt 10) {
>>> print "Inside Day lt 10";
>>> $month = $month * 10;
>>> } else {
>>> print "Missed Day lt 10";
>>> }
>>> 
>>
>>I'm sorry I just cant leave this without comment - what *is* wrong
with:
>>
>>  use Time::localtime;
>>
>>  my $tm = localtime;
>>
>>  $datestring = sprintf "%04d%02d%02d",$tm->year + 1900,
>>                                       $tm->mon + 1,
>>                                       $tm->mday;
>>
>>You *can* treat this an integer simply by , er, treating it as an
integer
>>
>>  $datestring++;
>>
>>Will correctly give 20000206.  You might also be interested in
looking at
>>the function POSIX::strftime .
>>
>>You have to bear in mind that anywhere you have a scalar whose value
*looks*
>>like a number than as far as Perl is concerned then it *is* a number
when
>>you go to use it as one .
>>
>>Oh BTW that should be '<' rather than 'lt'.
>>
>>/J\
>>-- 
>>Jonathan Stowe <jns@gellyfish.com>
>><http://www.gellyfish.com>
>>** Uri Guttman - Have You CPANed Backward.pm Yet ? **
>>

this is failing for me; is perl code written 2 years ago unacceptable
to a new compiler?

code:
#!/usr/bin/perl -w

# gdump.system.fang
#
# loki 2001-11-22
#
# dump rotation using Perl
# Error checking on mounted filesystem
# Mail report to root

use strict;
use Time::localtime;

my $tm = localtime;
$centuryoffset='1900';
$datestring = sprintf "%04d%02d%02d",$tm->year + $centuryoffset,
                                     $tm->mon,
                                     $tm->mday;


$reportfile='/var/log/gdump';
$mailto='root@dns1';
$program='/usr/sbin/dump';
$dumplevel=0;
$dumpslice='/system';
$dumpfile='dump';

print "Date=$datestring";

-----------
errors:
-Global symbol "$centuryoffset" requires explicit package name at
/usr/local/bin/gdump.pl line 15.
Global symbol "$datestring" requires explicit package name at
/usr/local/bin/gdump.pl line 16.
Global symbol "$century" requires explicit package name at
/usr/local/bin/gdump.pl line 16.
Global symbol "$reportfile" requires explicit package name at
/usr/local/bin/gdump.pl line 21.
Global symbol "$mailto" requires explicit package name at
/usr/local/bin/gdump.pl line 22.
Global symbol "$program" requires explicit package name at
/usr/local/bin/gdump.pl line 23.
Global symbol "$dumplevel" requires explicit package name at
/usr/local/bin/gdump.pl line 24.
Global symbol "$dumpslice" requires explicit package name at
/usr/local/bin/gdump.pl line 25.
Global symbol "$dumpfile" requires explicit package name at
/usr/local/bin/gdump.pl line 26.
Global symbol "$datestring" requires explicit package name at
/usr/local/bin/gdump.pl line 28.
Execution of /usr/local/bin/gdump.pl aborted due to compilation
errors.
----------


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

Date: Thu, 22 Nov 2001 06:01:12 GMT
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Traversing directories
Message-Id: <slrn9vp1be.r6l.tadmc@tadmc26.august.net>

Rafal Konopka <rafalk@home.com> wrote:

>Tad's solution was
>informative and looked good and elegant.  Unfortunately, it exited with
>an error.


I expect you meant error _message_?

Care to share the error message text with us?

It gave me no errors (ie. functioned correctly) and also
no error messages, so I'm wondering what was wrong with
it. Don't leave me guessing.


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Thu, 22 Nov 2001 06:01:13 GMT
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Traversing directories
Message-Id: <slrn9vp1nd.r6l.tadmc@tadmc26.august.net>

Rafal Konopka <rafalk@home.com> wrote:

>Likely, the version of perl (5.003) that is running on my
>machine is to blame.


Yikes!

5.004 came out in 1997. That's a Long Time in computer years.

Even worse, there is a CERT advisory against 5.003.

Get with the times man!  :-)


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Thu, 22 Nov 2001 00:07:11 -0500
From: "Matt Garrish" <matthew.garrish@sympatico.ca>
Subject: Re: variable scope
Message-Id: <DK%K7.10853$op.2418132@news20.bellglobal.com>


"Uri Guttman" <uri@stemsystems.com> wrote in message
news:x7u1vnbejb.fsf@home.sysarch.com...
>
> there is no downside. only you seem to have this problem. a my var being
> used as a foreach index was a requested feature by many perl hackers. it
> saves declaring the var before the loop and it properly is scoped to the
> loop which is where you want it. it is part of the loop even if it
> appears before the {}. just get it, learn to use it properly and stop
> whinging about it. it ain't gonna change for you. it is correct. it is
> not invisible. it is documented. you are mistaken about it not doing the
> right thing.
>

Yeah, well your mother wears army boots! and my big brother can beat your
big brother up! And Perl damn well better change for me!

I'd hardly go so far as to say it does the right thing. In this case, more
like the half-assed thing; it's neither here nor there. A lot of fence
sitting must have gone into that decision.

And why shouldn't I whine? I didn't post for your reading enjoyment...

Matt




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

Date: Thu, 22 Nov 2001 05:10:48 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: variable scope
Message-Id: <x7r8qrbd9m.fsf@home.sysarch.com>

>>>>> "MG" == Matt Garrish <matthew.garrish@sympatico.ca> writes:

  MG> "Uri Guttman" <uri@stemsystems.com> wrote in message
  MG> news:x7wv0jbfwy.fsf@home.sysarch.com...
  >> you aren't getting the purpose of scope. scope in this case is for
  >> exactly the reason of allowing a variable's name to be reused. this is
  >> the case in all languages that support scoped names. the idea is that in
  >> a given sub or block you don't have to worry about clobbering some
  >> outer scoped variable with the same name.
  >> 

  MG> I'm not getting something, but I don't think you've hit on it,
  MG> since you've completely misunderstood my point. I don't remember
  MG> saying anywhere in any of my posts that you should only be allowed
  MG> to declare any variable name only once per script. When I add a
  MG> foreach block to a script, I instinctively only see *two* blocks:
  MG> the main and the foreach. Now I've learned that there's actually a
  MG> third, thereby allowing the value in the "foreach my $row (@rows)"
  MG> to exist simultaneously with an inner declaration.  My point is
  MG> that this intermediary scoped variable is quietly being clobbered
  MG> by virtue of the inner block. Let me give a little diagram using

you aren't listening. there is no hidden 3rd block. the my var is PART
of the foreach block. it just is declare before the {}. simple.

  MG> Ilya's model to help here:

  MG> my $row = 1;  # sets the variable in the main body
  MG> {
  MG>   my $row = '';   # okay, new scope
  MG>   foreach $row (@names)   # sets $row as expected
  MG>   {
  MG>     my $row = "2";   # "clobbers" the expected $row

no, it doesn't clobber it. it hides it. 

  MG>   }

now $row is the last element of @names. see, end of inner scope so it is
seen again.

  MG> }

now $row is 1 again.

  MG> What is the point of allowing the second "my $row" to silently
  MG> fall out of scope immediately after being initialized? At the very
  MG> least I would have expected a warning indicating that the value is
  MG> only used once. But maybe it's just me...

it is not falling out of scope. the inner scope masks it. 

and maybe it will be used AFTER the inner block? scopes nest for a
reason.  you have a very narrow view of what scopes are and how they can
be used. and i do think it is just you. 

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
-- Stem is an Open Source Network Development Toolkit and Application Suite -
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


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

Date: Thu, 22 Nov 2001 05:17:46 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: variable scope
Message-Id: <x7oflvbcy0.fsf@home.sysarch.com>

>>>>> "MG" == Matt Garrish <matthew.garrish@sympatico.ca> writes:

  MG> "Uri Guttman" <uri@stemsystems.com> wrote in message
  MG> news:x7u1vnbejb.fsf@home.sysarch.com...
  >> 
  >> there is no downside. only you seem to have this problem. a my var being
  >> used as a foreach index was a requested feature by many perl hackers. it
  >> saves declaring the var before the loop and it properly is scoped to the
  >> loop which is where you want it. it is part of the loop even if it
  >> appears before the {}. just get it, learn to use it properly and stop
  >> whinging about it. it ain't gonna change for you. it is correct. it is
  >> not invisible. it is documented. you are mistaken about it not doing the
  >> right thing.
  >> 

  MG> Yeah, well your mother wears army boots! and my big brother can beat your
  MG> big brother up! And Perl damn well better change for me!

well, well, well. at least the insults are better than moronzilla's :)

  MG> I'd hardly go so far as to say it does the right thing. In this
  MG> case, more like the half-assed thing; it's neither here nor
  MG> there. A lot of fence sitting must have gone into that decision.

your experience in language development tells you otherwise? haw many
languages do you know or have ever worked in? there was no fence
sitting. my variables are simply lexically scoped in the block they are
declared in. the case of a my variable as a foreach index means it is
scoped INSIDE the foreach block. that's it. no more rules. it is totally
here and not half-assed.

  MG> And why shouldn't I whine? I didn't post for your reading enjoyment...

why don't you go join the perl6 lists and complain there about it? or
send in a patch (surely to be laughed off) to p5p? the scoping rules
make perfect sense. the fact that you don't automatically grok them does
not make them poorly designed. 

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
-- Stem is an Open Source Network Development Toolkit and Application Suite -
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


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

Date: Thu, 22 Nov 2001 05:37:55 GMT
From: Martien Verbruggen <mgjv@tradingpost.com.au>
Subject: Re: variable scope
Message-Id: <slrn9vp3pk.meb.mgjv@verbruggen.comdyn.com.au>

On Wed, 21 Nov 2001 23:47:50 -0500,
	Matt Garrish <matthew.garrish@sympatico.ca> wrote:
> 
> "Uri Guttman" <uri@stemsystems.com> wrote in message
> news:x7wv0jbfwy.fsf@home.sysarch.com...
>> you aren't getting the purpose of scope. scope in this case is for
>> exactly the reason of allowing a variable's name to be reused. this is

> my $row = 1;  # sets the variable in the main body
> {
>   my $row = '';   # okay, new scope
>   foreach $row (@names)   # sets $row as expected
>   {
>     my $row = "2";   # "clobbers" the expected $row
>   }
> }
> 
> What is the point of allowing the second "my $row" to silently fall out of
> scope immediately after being initialized? At the very least I would have
> expected a warning indicating that the value is only used once. But maybe
> it's just me...

It doesn't immediately fall out of scope. You just have another
variable with the same name in an inner scope, blocking your access to
the one in the outer scope. Just like in:

my $var = "foo";
{
	my $var = "bar";
	# no access to outer $var here
}
# no access to inner $var here

This is how it should work, honestly. The word "immediately" in your
text above is a bit of a red herring. Would you be less upset if your
code was disallowed, but this was allowed:

my $row = 1;  # sets the variable in the main body
{
  my $row = '';   # okay, new scope
  foreach $row (@names)   # sets $row as expected
  {
    my $bar = "nothing";
    my $row = "2";   # "clobbers" the expected $row
  }
}

simply because it wasn't immediate? Or are you saying that the loop
variable should be used before another lexical variable is allowed
with the same name? if so, then what about this:

my $dir = "/some/path";
# lots of code that never use $dir

opendir(DIR, $otherdir) or die $!;
while (my $dir = readdir(DIR))
{
	# do something with loop variable $dir
}

# Do something with the outer $dir here

The outer $dir never gets before another lexical in another scope is
declared and used. Is that an error as well? Or are you advocating a
special case for the implicit scopes that these loops generate?

What I'm trying to get at is that the way things work do so for a
reason. The reason you don't get warnings or anything else is because
there is no real reason to warn. At some point a compiler and parser
have to trust that the programmer knows what they're doing.

I'd personally be very upset if perl started spouting the sort of
warnings that you seem to want in this case. I wouldn't trust it to be
able to work out correctly when I meant to create a new variable with
the same name, and when I didn't.

Martien
-- 
                                | 
Martien Verbruggen              | Unix is user friendly. It's just
Trading Post Australia Pty Ltd  | selective about its friends.
                                | 


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

Date: 22 Nov 2001 00:40:55 -0500
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: variable scope
Message-Id: <m3zo5f747s.fsf@mumonkan.sunstarsys.com>

"Andrew Hamm" <ahamm@programmer.net> writes:

> If we rewrite your sample to
> 
> my @rows = (1, 2, 3);
> my $row = 1;
> foreach my $x (@rows) {
>    my $x = 2;
>    print "$x\n";
> }
> 
> print join(" ", @rows), "\n";
> 
> Then you'll still smash the contents of @rows. $x IS the current
> element of @rows. It is _not_ a copy of the current element.
> 
> Does that make sense?

Not really- I expect @rows to be (1, 2, 3) in the final print
statement.  Are you suggesting it's not?

You are right about the aliasing effect of the foreach loop,
but I don't think you're right about the subsequent redeclaration/
reinitialization of $x- that doesn't touch the elements of @x at all.
The alias is simply forgotten by the new $x.

OTOH, I have no idea what sort of explanation might satisfy OP 
here.  What I think he's upset about is that the foreach loop
doesn't get parsed like

  FOREACH: {
    my $x = 1;
    my $x = 2;
  }

which produces a "masked declaration" warning.  It's actually 
a bit closer to

  { 
    my $x = 1;
    FOREACH: {
        my $x = 2;
    }
  }

which is warning-free.  It is also IMO preferable this 
(warning-free) way, but it's not important enough to me
to really care all that much.  I try to avoid reusing
variable names unless I *intend* to override/mask/localize 
them. By choosing decent variable names, it is nearly
impossible to override one by accident- and certainly 
not a throw-away loop variable.


-- 
Joe Schaefer        "Education consists mainly in what we have unlearned."
                                               --Mark Twain



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

Date: Thu, 22 Nov 2001 17:58:06 +1100
From: "Andrew Hamm" <ahamm@programmer.net>
Subject: Re: variable scope
Message-Id: <3bfca2ac$1_1@news.iprimus.com.au>

Joe Schaefer wrote in message ...
>
>> If we rewrite your sample to
>>
>> my @rows = (1, 2, 3);
>> my $row = 1;
>> foreach my $x (@rows) {
>>    my $x = 2;
>>    print "$x\n";
>> }
>>
>> print join(" ", @rows), "\n";
>>
>> Then you'll still smash the contents of @rows. $x IS the current
>> element of @rows. It is _not_ a copy of the current element.
>>
>> Does that make sense?
>
>Not really- I expect @rows to be (1, 2, 3) in the final print
>statement.  Are you suggesting it's not?
>
EXACTLY! It will print 2 2 2. Matt's problem is confusing in two ways. He's
declaring a variety of $rows in different scopes and then wondering why his
array is ending up with the values (2,2,2). Because of the re-use of $row,
including the unfortunate use of it as the foreach variable, people are
being distracted into an argument about scope. Which is why I rewrote to
show the same effect despite the removal of the scoping issues.

>You are right about the aliasing effect of the foreach loop,
>but I don't think you're right about the subsequent redeclaration/
>reinitialization of $x- that doesn't touch the elements of @x at all.
>The alias is simply forgotten by the new $x.
>
No, I didn't mention @x unless I made a silly typing mistake. Checking....
no typing mistake. That's a relief. I stand by my original message. Sure,
there's a scope issue and also the problem of understanding the scoping
rules, but to answer Matt's original question which was "why does the array
end up as (2,2,2)" I've given the answer.

Test my reply on the command line or a little script. It will print 2 2 2

>OTOH, I have no idea what sort of explanation might satisfy OP
>here.  What I think he's upset about is that the foreach loop
>doesn't get parsed like
>
[SNIP]

Yup. He's currently arguing with Ilya and/or Uri about scoping rules.

They are both sorta correct. I'll stand by Ilya, Uri and Perl's scoping
rules, including the implicit scope of foreach but I'll also agree with
Matt's pissed-off-state in the sense that he's discovering for himself a
piece of programming wisdom. Scope is nice and very very useful but don't
make life difficult for yourself by doing things like

{
    my $i;
    ....
    {
        ....
        my $i
        ....
        {
            ....
            my $i;
            ....
        }
    }
}

because it'll drive ya nuts and is almost as bad as languages where scope
didn't exist and all variables were of global scope. Which you can still do
in Perl (mostly) by merely NOT using local or my declarations. I say mostly
because contexts like foreach FORCE a scope upon you no matter whether you
like it or not. Try this:

    $i = 10;
    foreach $i (1, 2, 3) {
        print "$i\n";
    }
    print "$i\n";

What would you expect the final print to show? If you say 3 or 4 or even
empty you'd be wrong. It will print 10. What's happening is the value of $i
is being stashed away and then restored.  So you can't escape that scoping
effect in Perl.

Anyway, getting back to Matts original problem of the (2,2,2) array, it's
because the foreach variable IS, or represents, or stands in for, or shares
with, or is an ALIAS of (proper language) the CURRENT VALUE in the list
being looped over.

Sorry about all the CAPS but I want to emphasize the key words.
--
Space Corps Directive #723
Terraformers are expressly forbidden from recreating Swindon.
    -- Red Dwarf





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

Date: Thu, 22 Nov 2001 18:03:59 +1100
From: "Andrew Hamm" <ahamm@programmer.net>
Subject: Re: variable scope
Message-Id: <3bfca40d$1_2@news.iprimus.com.au>

Matt Garrish wrote in message ...
>
>"Uri Guttman" <uri@stemsystems.com> wrote in message
>news:x7wv0jbfwy.fsf@home.sysarch.com...
>> you aren't getting the purpose of scope. scope in this case is for
>> exactly the reason of allowing a variable's name to be reused. this is
>> the case in all languages that support scoped names. the idea is that in
>> a given sub or block you don't have to worry about clobbering some
>> outer scoped variable with the same name.
>>
>
>I'm not getting something, but I don't think you've hit on it, since you've
>completely misunderstood my point. I don't remember saying anywhere in any
>of my posts that you should only be allowed to declare any variable name
>only once per script. When I add a foreach block to a script, I
>instinctively only see *two* blocks: the main and the foreach. Now I've
>learned that there's actually a third, thereby allowing the value in the
>"foreach my $row (@rows)" to exist simultaneously with an inner
declaration.
>My point is that this intermediary scoped variable is quietly being
>clobbered by virtue of the inner block. Let me give a little diagram using
>Ilya's model to help here:
>
>my $row = 1;  # sets the variable in the main body
>{
>  my $row = '';   # okay, new scope
>  foreach $row (@names)   # sets $row as expected
>  {
>    my $row = "2";   # "clobbers" the expected $row
>  }
>}
>
>What is the point of allowing the second "my $row" to silently fall out of
>scope immediately after being initialized? At the very least I would have
>expected a warning indicating that the value is only used once. But maybe
>it's just me...
>
Good point. I would expect a warning too but it's not forthcoming. Perl does
issue warnings about variables only being used once but it appears maybe
that only works for globals. Try this:

perl -we '$i = 10;'
Name "main::i" used only once: possible typo at -e line 1.

So it works for globals but not scopies. That sounds like a legitimate call
for improvement in Perl. Maybe. Perhaps there are good reasons why it's not
done. Like possibly 90% of scripts will suddenly start screaming ;-)

Note that you need to put the -w flag to enable the warnings.

More modern way is a

use warnings;

statement 'cos that enables warnings at the compiler level - ie slightly
earlier than runtime.
--
Space Corps Directive #723
Terraformers are expressly forbidden from recreating Swindon.
    -- Red Dwarf





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

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


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