[22729] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4950 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed May 7 06:05:45 2003

Date: Wed, 7 May 2003 03:05:09 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Wed, 7 May 2003     Volume: 10 Number: 4950

Today's topics:
    Re: @Array - making it null <sammie@greatergreen.com>
    Re: @Array - making it null <uri@stemsystems.com>
    Re: @Array - making it null <sammie@greatergreen.com>
    Re: @Array - making it null (Helgi Briem)
    Re: Can't Create Text Files, Config problem (Helgi Briem)
    Re: Can't Create Text Files, Config problem <jonnyr9@r9corporation.fsnet.co.uk>
    Re: Can't Create Text Files, Config problem <flavell@mail.cern.ch>
    Re: Can't Create Text Files, Config problem <REMOVEsdnCAPS@comcast.net>
    Re: Design Opinions - Dealing with Constants <ubl@schaffhausen.de>
    Re: escape sequencing problem in Perl (Prakash)
    Re: Extracting part of the contents of one var into ano <bigus AT creationfactor DOT net>
    Re: Extracting part of the contents of one var into ano <tassilo.parseval@rwth-aachen.de>
    Re: Extracting part of the contents of one var into ano <bigus AT creationfactor DOT net>
    Re: foolproof requires (Analysis&Solutions)
    Re: Forcing an rvalue context on a scalar expression (Anno Siegel)
    Re: Help: Create Hash On The Fly? <REMOVEsdnCAPS@comcast.net>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 07 May 2003 04:35:03 GMT
From: "Brad Walton" <sammie@greatergreen.com>
Subject: Re: @Array - making it null
Message-Id: <X10ua.756629$3D1.417611@sccrnsc01>

Yes, I should have done that. I found:

undef @Array;

Which seems to work fine. What is the difference between the two methods?

Brad


"Uri Guttman" <uri@stemsystems.com> wrote in message
news:x78ytjqvp2.fsf@mail.sysarch.com...
> >>>>> "BW" == Brad Walton <sammie@greatergreen.com> writes:
>
> <rearranged as he top posted himself>
>
>   >> I have an @Array which is defined. I process the info. Now I want the
>   BW> @Array
>   >> to be null, so I may do the process all over again. What is the
proper
>   >> syntax to make an @Array disappear?
>
>   BW> nevermind... found it.
>
> so why don't you post what you found? you may have actually found a bad
> method and will have problems with it. did you solve it with undef? i am
> guessing that because above you mentioned the term defined. if you did
> that, then it you did not find the correct way which is to assign an
> empty list to the array:
>
> @array = () ;
>
> uri
>
> -- 
> Uri Guttman  ------  uri@stemsystems.com  -------- 
http://www.stemsystems.com
> --Perl Consulting, Stem Development, Systems Architecture, Design and
Coding-
> Search or Offer Perl Jobs  ---------------------------- 
http://jobs.perl.org




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

Date: Wed, 07 May 2003 05:08:47 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: @Array - making it null
Message-Id: <x7bryfpcm8.fsf@mail.sysarch.com>

>>>>> "BW" == Brad Walton <sammie@greatergreen.com> writes:

  BW> Yes, I should have done that. I found:
  BW> undef @Array;

  BW> Which seems to work fine. What is the difference between the two methods?

undef @array removes the entire array including all the elements and the
internal array structures. assigning () to the array only removes the
elements so it doesn't have to reallocate the array structure the next
time you assign to it. 

but even more insidious is that undef @Array implies that

	defined( @array ) 

is meaningful and it is a bug. the docs specifically cover why this is
so. the defined function checks to see if that array structure is
allocated or not. it does NOT check to see if there are any elements in
the array which is what you would probably want.

so undef @array is both slower (frees more than needed and forces more
allocation later) and can lead you on the path to a very nasty bug.

so assign an empty list which is correct, clear and what all perl
hackers use.

i am working on an 'undef is evil' presentation. it is a nasty little
function that is rarely needed. that is not saying the undefined value
is not useful - it is EXTREMELY useful. the undef function itself is
just not needed much, and it is very bad to use it on aggregates for the
above mentioned reasons.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


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

Date: Wed, 07 May 2003 05:41:34 GMT
From: "Brad Walton" <sammie@greatergreen.com>
Subject: Re: @Array - making it null
Message-Id: <i01ua.519132$Zo.112864@sccrnsc03>

Thanks Uri... I will change the script based on your recommendation.

Cheers,
Brad


"Uri Guttman" <uri@stemsystems.com> wrote in message
news:x7bryfpcm8.fsf@mail.sysarch.com...
> >>>>> "BW" == Brad Walton <sammie@greatergreen.com> writes:
>
>   BW> Yes, I should have done that. I found:
>   BW> undef @Array;
>
>   BW> Which seems to work fine. What is the difference between the two
methods?
>
> undef @array removes the entire array including all the elements and the
> internal array structures. assigning () to the array only removes the
> elements so it doesn't have to reallocate the array structure the next
> time you assign to it.
>
> but even more insidious is that undef @Array implies that
>
> defined( @array )
>
> is meaningful and it is a bug. the docs specifically cover why this is
> so. the defined function checks to see if that array structure is
> allocated or not. it does NOT check to see if there are any elements in
> the array which is what you would probably want.
>
> so undef @array is both slower (frees more than needed and forces more
> allocation later) and can lead you on the path to a very nasty bug.
>
> so assign an empty list which is correct, clear and what all perl
> hackers use.
>
> i am working on an 'undef is evil' presentation. it is a nasty little
> function that is rarely needed. that is not saying the undefined value
> is not useful - it is EXTREMELY useful. the undef function itself is
> just not needed much, and it is very bad to use it on aggregates for the
> above mentioned reasons.
>
> uri
>
> -- 
> Uri Guttman  ------  uri@stemsystems.com  -------- 
http://www.stemsystems.com
> --Perl Consulting, Stem Development, Systems Architecture, Design and
Coding-
> Search or Offer Perl Jobs  ---------------------------- 
http://jobs.perl.org




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

Date: Wed, 07 May 2003 09:38:25 GMT
From: helgi@decode.is (Helgi Briem)
Subject: Re: @Array - making it null
Message-Id: <3eb8d238.2313544010@news.cis.dfn.de>

On Wed, 07 May 2003 05:08:47 GMT, Uri Guttman
<uri@stemsystems.com> wrote:

>i am working on an 'undef is evil' presentation. it is a nasty little
>function that is rarely needed. that is not saying the undefined value
>is not useful - it is EXTREMELY useful. the undef function itself is
>just not needed much, and it is very bad to use it on aggregates for the
>above mentioned reasons.

I have done a lot of parsing of text files in my time and
I have sometimes used undef to do this sort of thing:

my ($name,$length,$position,$quality) = split;

undef $length;
undef $quality;
print "name $position";

Mainly to emphasise to later maintainers exactly
what is contained in $length and $quality if they ever
need those.  The undefs avoid "variable used only
once" warnings.

If I simply said:

my ($name,undef,$position,undef) = split;
print "name $position";

Later maintainer is unclear on what is contained
in the undefined fields of the input file, although
I could put it in comments, I suppose.

What is your opinion of these uses of undef?
Which is preferrable or is some other solution
better?
-- 
Regards, Helgi Briem
helgi DOT briem AT decode DOT is


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

Date: Wed, 07 May 2003 09:04:09 GMT
From: helgi@decode.is (Helgi Briem)
Subject: Re: Can't Create Text Files, Config problem
Message-Id: <3eb8cbf6.2311941225@news.cis.dfn.de>

On 6 May 2003 12:10:46 -0700, csdude@hotmail.com (Mike)
wrote:

>When I write a program designed to create a database (any type), if I
>run it directly from Unix it will create the database no problem. But
>if I run it from the web browser first, I just get an Internal Server
>Error. If I manually create the text file and chmod it to 666, though,
>it will run from the browser just fine.

perldoc -q 500
-- 
Regards, Helgi Briem
helgi DOT briem AT decode DOT is


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

Date: Wed, 7 May 2003 10:24:13 +0100
From: "Jon Rees" <jonnyr9@r9corporation.fsnet.co.uk>
Subject: Re: Can't Create Text Files, Config problem
Message-Id: <b9ajge$o4s$1@newsg3.svr.pol.co.uk>

I have a similar problem -  my perl script will create a directory
and give it permissions, it will create a file and write to it, close it -
but then it throws a 500 Config error.

Even somthing as simple as this - produces the same error...I rent the
server
and don't think I have access to error logs...any ideas?

#!/bin/perl

use warnings;
open (BOB, ">example.txt");
close (BOB);

Best wishes

Jon Rees


"Eric J. Roode" <REMOVEsdnCAPS@comcast.net> wrote in message
news:Xns9373E6AEF79E5sdn.comcast@216.166.71.239...
> -----BEGIN xxx SIGNED MESSAGE-----
> Hash: SHA1
>
> csdude@hotmail.com (Mike) wrote in
> news:46cdc619.0305061110.13863d23@posting.google.com:
>
> > #!/usr/bin/perl
> > $name=Mike;
> >
> > open(OUTFILE, ">>test.txt") or die "Error opening test.txt. $!,
> > stopped";
> > print OUTFILE "$name\n";
> > close(OUTFILE);
> >
> > print "Location: http://www.myurl.com\n\n";
> > exit;
>
> Don't use "die" in CGI scripts; that's the source of your 500 error.
> The "Error opening...." message was put into your Apache log.  If you
> look there, you'll get a hint from $!.
>
> Almost certainly the problem is that the user the web server runs as
> (usually "nobody" or "apache") doesn't have permission to create
> files in whatever directory the cgi program is located in.  This is a
> Good Thing.  Don't change that, as someone else in this thread has
> suggested.  Go create a data directory somewhere else for the CGI
> programs to create their data in; make THAT directory writeable by
> the web server user.
>
> - --
> Eric
> print scalar reverse sort qw p ekca lre reh
> ts uJ p, $/.r, map $_.$", qw e p h tona e;
> -----BEGIN xxx SIGNATURE-----
> Version: GnuPG v1.2.1 (MingW32) - WinPT 0.5.13
>
> iD8DBQE+uHIjY96i4h5M0egRApqdAKCF1veyrFZRv8gNEkxz3ldakGC/6ACg3l42
> j4tjq8kDq56TLjDfhmMEufE=
> =xW5/
> -----END PGP SIGNATURE-----




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

Date: Wed, 7 May 2003 11:06:37 +0200
From: "Alan J. Flavell" <flavell@mail.cern.ch>
Subject: Re: Can't Create Text Files, Config problem
Message-Id: <Pine.LNX.4.53.0305071058530.22309@lxplus013.cern.ch>

On Tue, May 6, Eric J. Roode inscribed on the eternal scroll:

> Don't use "die" in CGI scripts;

I see your point, but that's a bit of a broad-brush statement.

> that's the source of your 500 error.
> The "Error opening...." message was put into your Apache log.

Exactly.  And for bizarre error situations which are expected to be
rare, that might be a tolerable solution for a production CGI script,
though preferably with a custom error-500 page to make it look nice
for the user.  die() is certainly better than just not bothering to
test for errors, and letting the script blunder on until something
just happens to go fatally wrong later.  IMHO and YMMV.

While debugging, on the other hand, one could recommend CGI::Carp with
fatalsToBrowser (whereas that might not be such a good idea in a
production script, as it can expose details of script internals to a
possibly-malicious intruder).

> Almost certainly the problem is that the user the web server runs as
> (usually "nobody" or "apache") doesn't have permission to create
> files in whatever directory the cgi program is located in.  This is a
> Good Thing.  Don't change that, as someone else in this thread has
> suggested.  Go create a data directory somewhere else for the CGI
> programs to create their data in; make THAT directory writeable by
> the web server user.

Good advice, for sure.


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

Date: Wed, 07 May 2003 04:51:29 -0500
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: Can't Create Text Files, Config problem
Message-Id: <Xns93743B885F38Csdn.comcast@216.166.71.239>

-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1

"Jon Rees" <jonnyr9@r9corporation.fsnet.co.uk> wrote in
news:b9ajge$o4s$1@newsg3.svr.pol.co.uk: 

> I have a similar problem -  my perl script will create a directory
> and give it permissions, it will create a file and write to it,
close
> it - but then it throws a 500 Config error.
> 
> Even somthing as simple as this - produces the same error...I rent
the
> server
> and don't think I have access to error logs...any ideas?

I am sure you have access to the error logs and access logs.  If not,
switch to a better hosting company.

 
> #!/bin/perl
> 
> use warnings;
> open (BOB, ">example.txt");
> close (BOB);


Never open a file without checking whether the open succeeded or not.
 For simple command-line scripts, you use die.  For a web app, you
have to do a little more.  Something like this:

    unless (open BOB, '>example.txt')
    {
        print CGI::header, CGI::start_html;
        print "Can't open file example.txt: $!";
        print "</body></html>";
        exit;
    }

For mod_perl and FastCGI scripts, you have to do even more.  But I
don't think you're there yet... :-)

Also, you don't really want to be opening files in the same directory
as your CGI scripts, do you?  No, I didn't think so.

- -- 
Eric
print scalar reverse sort qw p ekca lre reh 
ts uJ p, $/.r, map $_.$", qw e p h tona e;
-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32) - WinPT 0.5.13

iD8DBQE+uNcJY96i4h5M0egRAhz7AKDJuJlYkPOotfwpN7kVD2qKY+KF+gCggCDp
YXgSkbyWZC01UpcUorFOqnU=
=e8CR
-----END PGP SIGNATURE-----


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

Date: Wed, 07 May 2003 11:46:06 +0200
From: Malte Ubl <ubl@schaffhausen.de>
Subject: Re: Design Opinions - Dealing with Constants
Message-Id: <b9anpq$obt$1@news.dtag.de>

Kevin Vaughn wrote:
> I've created a large number of scripts that reference a single file
> containing all of my constants.  I named the file environment.pl.  The file
> mainly contains paths.  I include environment.pl using a "required"
> statement.

I use constants for constants. Excerpt from my current project:

package SC::Globals;
use strict;

use constant root          => "/home/folder";
use constant base_path     => root."/htdocs/in";

use constant content_path  => base_path."/managed";
use constant image_path    => base_path."/images";
use constant movie_path    => base_path."/movies";

All constants are lower case, because I know they are constant from the 
package name, so I don't need a special convention.

This catches spelling mistakes at compile time and I don't have to worry 
about namespace conflicts.

I just say C<use SC::Globals> once and can put C<SC::Globals::root> 
everywhere in my program afterwards.


malte.exe



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

Date: 7 May 2003 02:01:41 -0700
From: prakashpms@hotmail.com (Prakash)
Subject: Re: escape sequencing problem in Perl
Message-Id: <afc6e663.0305070101.1e24e86d@posting.google.com>

"Eric J. Roode" <REMOVEsdnCAPS@comcast.net> wrote in message news:<Xns93733A118DF6Asdn.comcast@216.166.71.239>...
> -----BEGIN xxx SIGNED MESSAGE-----
> Hash: SHA1
> 
> prakashpms@hotmail.com (Prakash) wrote in
> news:afc6e663.0305052143.2946247f@posting.google.com: 
> 
> > "Kasp" <kasp@epatra.com> wrote in message
> > news:<b966db$afq$1@newsreader.mailgate.org>... 
> >> Yes, there is more than one way of doing it.
> >> 
> >> May I point the OP to quotemeta function which I find
>  particularly
> >> useful but less talked about.
> > 
> > 
> > The problem I am facing is that one of the programs returns
>  something
> > like this string with lots of "\" characters. I want to store them
>  in
> > a variable and process it as such. But, when I store and use the
> > variable, I get only half the number of "\"es what the program
> > returned. I do not want perl to escape sequence the variable.
> 
> Perl does not do that.  You are either doing something strange and
> overly complex (like eval) or you are misinterpreting what's going
> on.
> 
> Can you post a short bit of code that shows this
> disappearing-backslash problem?
> 
> - -- 
> Eric
> print scalar reverse sort qw p ekca lre reh 
> ts uJ p, $/.r, map $_.$", qw e p h tona e;
> -----BEGIN xxx SIGNATURE-----
> Version: GnuPG v1.2.1 (MingW32) - WinPT 0.5.13
> 
> iD8DBQE+t4OAY96i4h5M0egRAtJ7AJ0V6V8N7cBQ3nOgsj26cS3u1HrOgACg3JPq
> AIbtgJ2odRIQ6uLV/4O1Nb8=
> =SLP4
> -----END PGP SIGNATURE-----

Yes, you are right. There is some problem with the program or perl
could not handle it properly..

$var = `myprogram -export`;

When I run the program I was getting properly, but when I assign to a
perl variable like above, it was removing the "\"es. Since my program
sometimes prints multiple lines of strings, I used array and perl
array is working properly as I need.

Thank you all for the help.
Prakash


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

Date: Wed, 7 May 2003 09:07:03 +0100
From: "Bigus" <bigus AT creationfactor DOT net>
Subject: Re: Extracting part of the contents of one var into another
Message-Id: <b9aer8$vuc@newton.cc.rl.ac.uk>


"Abigail" <abigail@abigail.nl> wrote in message
news:slrnbbfmt4.tib.abigail@alexandra.abigail.nl...
> Bigus () wrote on MMMDXXXV September MCMXCIII in
> <URL:news:b98khu$udk@newton.cc.rl.ac.uk>:
> `'  When you have a string, such as:
> `'
> `'  $sentence = "my name is bob"
> `'
> `'  and you want to extract say the persons name into another variable,
then I
> `'  would normally do something like this:
> `'
> `'  $name = $sentence;
> `'  $name =~ s/^.* //;
> `'
> `'  or
> `'
> `'  @temp = $sentence =~ /(\S+)$/;
> `'  $name = $temp[0];
> `'
> `'  There's a one-line way of doing this, isn't there - but can you tell
me what
> `'  it is?
>
>
>     ($name) = $sentence =~ /(\S+)$/;
>
> Or:
>
>     ($name = $sentence) =~ s/.* //;

That's the one! Thanks. I thought I'd seen it on here before, but couldn't
remember the syntax :-)

Regards
Bigus




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

Date: 7 May 2003 08:48:52 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: Extracting part of the contents of one var into another
Message-Id: <b9ah9k$3ne$1@nets3.rz.RWTH-Aachen.DE>

Also sprach Bigus:

> "Abigail" <abigail@abigail.nl> wrote in message
> news:slrnbbfmt4.tib.abigail@alexandra.abigail.nl...
>> Bigus () wrote on MMMDXXXV September MCMXCIII in

>> `'  $name = $sentence;
>> `'  $name =~ s/^.* //;
>> `'
>> `'  or
>> `'
>> `'  @temp = $sentence =~ /(\S+)$/;
>> `'  $name = $temp[0];
>> `'
>> `'  There's a one-line way of doing this, isn't there - but can you tell
> me what
>> `'  it is?
>>
>>
>>     ($name) = $sentence =~ /(\S+)$/;
>>
>> Or:
>>
>>     ($name = $sentence) =~ s/.* //;
> 
> That's the one! Thanks. I thought I'd seen it on here before, but couldn't
> remember the syntax :-)

Actually there is nothing special about this syntax. You can generalize
it:

    chomp ($name = $sentence);

will do the same thing: first do the assignment and then modify what was
assigned. In Perl an assignments returns the value assigned.

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


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

Date: Wed, 7 May 2003 10:39:04 +0100
From: "Bigus" <bigus AT creationfactor DOT net>
Subject: Re: Extracting part of the contents of one var into another
Message-Id: <b9ak7o$pr4@newton.cc.rl.ac.uk>


"Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de> wrote in message
[..]
> >>     ($name) = $sentence =~ /(\S+)$/;
> >>
> >> Or:
> >>
> >>     ($name = $sentence) =~ s/.* //;
> >
> > That's the one! Thanks. I thought I'd seen it on here before, but
couldn't
> > remember the syntax :-)
>
> Actually there is nothing special about this syntax. You can generalize
> it:
>
>     chomp ($name = $sentence);
>
> will do the same thing: first do the assignment and then modify what was
> assigned. In Perl an assignments returns the value assigned.

I guess it's one of those fundamentals that has escaped me in my piece-meal
acquisition of Perl knowledge. v.useful though.. thanks.

Regards

Bigus




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

Date: Wed, 7 May 2003 05:00:09 +0000 (UTC)
From: info@analysisandsolutions.com (Analysis&Solutions)
Subject: Re: foolproof requires
Message-Id: <b9a3sp$pao$1@reader1.panix.com>

Hi Eric:

In <Xns9373E88ADF5E9sdn.comcast@216.166.71.239> "Eric J. Roode" <REMOVEsdnCAPS@comcast.net> writes:

>That's one ugly-ass prototype.  You sure you know what you're doing?

Nope!  Well, not when it comes to Perl, at least.


>Your approach sounds overly complex.  Why not create auxiliary
>modules that would act as wrappers, one for each language?

This sounds more complex to me.


>That way,
>if someone needed to use two or more languages, they could specify it
>in their program, rather than having to install two or more copies of
>your module on their site.

No need to install multiple instances of my module.  One installation can 
run multiple languages.  Plus, people can install new languages very 
easily.  Here's the present file structure:
   ./ccvs-perl/ccvs.pm
   ./ccvs-perl/language/ccvs_de.properties
   ./ccvs-perl/language/ccvs_en.properties
   ./ccvs-perl/language/ccvs_es.properties
   ./ccvs-perl/language/ccvs_fr.properties

This is how things are done in Java.  Plus it's a simple
internationalization technique in PHP too.  So, I wanted to port things
over to Perl in a similar manner.

The language file included can be determined by programmers by changing 
the Language parameter, 'en' in this case, to some other language code.  
Similarly, the programmer can use the language code from the browser to 
further customize things.

    if ( !$Form->validateCreditCard(param('Number'), 'en', \@Accepted, 'Y',
                                    param('Month'), param('Year')) ) {
        print "  <p>PROBLEM: $Form->{CCVSError}</p>\n";
    }



>Something like:  (warning, untested.  Just a blue-sky brainstorm)

>    # Caller's program:
>    use CreditCardValidationSolution::EN;
>    ....

>    # CreditCardValidationSolution/EN.pm
>    use CreditCardValidationSolution;
>    $err01 = "You are a dolt.";
>    $err02 = "Invalid checksum":   # whatever

Wouldn't this necessitate my module being in a location acceptable to 
%INC?  Also, this seems to eliminate the ability to dynamically call 
different languages on the fly.

If you want to check out the full system, it's on line at 
http://www.analysisandsolutions.com/software/ccvs/ccvs.htm

Thanks,

--Dan

-- 
     FREE scripts that make web and database programming easier
           http://www.analysisandsolutions.com/software/
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
 4015 7th Ave #4AJ, Brooklyn NY    v: 718-854-0335   f: 718-854-0409


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

Date: 7 May 2003 09:46:20 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Forcing an rvalue context on a scalar expression
Message-Id: <b9aklc$16g$1@mamenchi.zrz.TU-Berlin.DE>

Uri Guttman  <uri@stemsystems.com> wrote in comp.lang.perl.misc:
> >>>>> "AS" == Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> writes:
> 
>   AS> Uri Guttman  <uri@stemsystems.com> wrote in comp.lang.perl.misc:
>   >> >>>>> "AS" == Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> writes:
>   >> 
>   AS> Abigail  <abigail@abigail.nl> wrote in comp.lang.perl.misc:
>   >> >> 
>   >> >> Consider this:
>   >> >> 
>   >> >> sub foo {$_ [0] = 1}
>   >> >> my @q;
>   >> >> foo $q [2];
>   >> >> print scalar @q;
>   >> >> 
>   >> >> This prints 3 because you created $q [2]. But suppose you had called
>   >> >> 'foo $q [-1]', how large should @q be afterwards? Which element was
>   >> >> created? 
>   >> 
>   AS> All true, but the error shouldn't happen just because foo( $q[ -1]) is
>   AS> called, only when foo() actually tries to assign to its argument.
>   >> 
>   >> but a sub call creates an alias to that element which doesn't exist. it
>   >> can be undef since there is no slot to create alias to. 
> 
> 
>   AS> ...but $q[ 12] doesn't exist either, but is legal in places where $q[ -1]
>   AS> isn't.  The difference would be that (with an empty array) $q[ -1] can
>   AS> never be allocated.
> 
>   AS> I don't understand the second sentence.
> 
> i said the same thing but in a different way. $q[12] can be allocated
> since it can count from the 0 slot. $q[-1] can't be located nor
> allocated since it must count from the end and the array is empty. -1
> would then mean the 'real' -1 slot which can't be accessed. 
> 
> simple math here. the negative numbers are based on the array size. if
> the array has 1 element, a -1 index refers to the 0 slot (1 - 1). so if
> the array is empty it refers to the (0 - 1) -1 slot which cannot exist.

Sure.  All I'm saying is the effect isn't bound to empty arrays but
happens whenever a negative index exceeds the array bound.

>   AS> Hmm... referencing and aliasing seems to be the key.  "1 for $q[ -1]"
>   AS> also triggers it.  Still, the behavior is unexpected, considering that
>   AS> read-access is usually possible with any array and any index, even
>   AS> large negatives.  Also, how can a prepended "scalar", or a "do{}"
>   AS> wrapped around heal it?  Something is flaky here and needs attention.
> 
> dunno how they 'fix' it. i still don't consider it a bug as my math
> above shows. an empty array doesn't have a -1 slot.
> 
>   AS> Ah, c'mon, 0 is a respectable end too.  Emptiness has nothing to do
>   AS> with it, the same happens with a non-empty array for large negative
>   AS> indices, once they point past (err, before) the beginning of the array.
> 
> it is the size and not the end index that is used in the negative index
> calculation. look at the above.
> 
> 
>   AS> I still think there's a bug involved, but the solution may be less
>   AS> straight-forward than I was hoping for.
> 
> i think the scalar and do{} fixes may be bugs. :)
> 
> i haven't looked into their behavior and i may later.

There is clearly a bug involved and it is independent from the "$q{ -1}"
behavior.

    my $x;
    scalar( $x) = 123;

Can't modify scalar in scalar assignment at ...

    $_ = 123 for scalar( $x);
    print "$x\n";

123

Similar behavior with "sub assign { $_[ 0] = 123 }; assign( scalar( $x))"
and with "do {}" instead of "scalar()".   That can't be all correct.

But I think this is only indirectly related to the behavior of "$q[ -1]"
(with empty @q).

As to that, I still believe if it can be fixed with reasonable expense
it should be.  Since read access to $q[ -1] is legal elsewhere, it
comes as a surprise that it can't be used in "aliasing context" (like
a sub parameter, or an element in a "foreach" list).

I may be naive, but I'd try to supplant an alias to a read-only
location with undefined content for the un-creatable alias.  That
way an error would only occur if you try to assign to the location,
the read behavior would be as it should be.

Anno


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

Date: Wed, 07 May 2003 04:45:04 -0500
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: Help: Create Hash On The Fly?
Message-Id: <Xns93743A739DB26sdn.comcast@216.166.71.239>

-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1

email_entropy123@yahoo.com (entropy123) wrote in
news:90cdce37.0305061701.7914aa6c@posting.google.com:

> \> > 
>> You want a hash of hashes:
>> 
>> my %hoh = ();
>> 
>> $hoh{A0} = {B1=>2, A0=>1, A3=>3};
>> 
>> To get the values out:
>> 
>> my $val = $hoh{A0}{B1};
>>
> This is pretty damned great. One question:
> 
> How would I do this - 
> 
> while ( ($key, $value) = each %hash) {
>      print "$key = $value\n";
>     }  
> 
> in the hash of hashes?


while ($key,$value) = each %{ $hoh{A0} })


$hoh{A0} is a reference to a hash.  "each" requires an actual hash
(with an actual % character).  %{} dereferences the hash.

- -- 
Eric
print scalar reverse sort qw p ekca lre reh 
ts uJ p, $/.r, map $_.$", qw e p h tona e;
-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32) - WinPT 0.5.13

iD8DBQE+uNWJY96i4h5M0egRAoZ3AJ4nR5+4oVKmmcUWeLQJgKAwplzM/gCgtd41
4xTnCGaPYmrynPT/eFRdebk=
=2z5y
-----END PGP SIGNATURE-----


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

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


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