[26399] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 8570 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Oct 26 03:05:25 2005

Date: Wed, 26 Oct 2005 00:05:06 -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, 26 Oct 2005     Volume: 10 Number: 8570

Today's topics:
        FAQ 7.15 How do I create a static variable? <comdog@pair.com>
    Re: getElementsByTagName, tag does not exist robic0@yahoo.com
        How to detect memory usage in hash? <sonet.all@msa.hinet.net>
    Re: How to detect memory usage in hash? <nobody@bigpond.com>
    Re: Moving data structure around better than globals? <tadmc@augustmail.com>
        Timelocal input, post good Date filters (from - to) robic0@yahoo.com
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 26 Oct 2005 04:03:01 +0000 (UTC)
From: PerlFAQ Server <comdog@pair.com>
Subject: FAQ 7.15 How do I create a static variable?
Message-Id: <djmv5l$p93$1@reader2.panix.com>

This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with Perl.

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

7.15: How do I create a static variable?

    (contributed by brian d foy)

    Perl doesn't have "static" variables, which can only be accessed from
    the function in which they are declared. You can get the same effect
    with lexical variables, though.

    You can fake a static variable by using a lexical variable which goes of
    scope. In this example, you define the subroutine "counter", and it uses
    the lexical variable $count. Since you wrap this in a BEGIN block,
    $count is defined at compile-time, but also goes out of scope at the end
    of the BEGIN block. The BEGIN block also ensures that the subroutine and
    the value it uses is defined at compile-time so the subroutine is ready
    to use just like any other subroutine, and you can put this code in the
    same place as other subroutines in the program text (i.e. at the end of
    the code, typically). The subroutine "counter" still has a reference to
    the data, and is the only way you can access the value (and each time
    you do, you increment the value). The data in chunk of memory defined by
    $count is private to "counter".

        BEGIN {
            my $count = 1;
            sub counter { $count++ }
        }

        my $start = count();

        .... # code that calls count();

        my $end = count();

    In the previous example, you created a function-private variable because
    only one function remembered its reference. You could define multiple
    functions while the variable is in scope, and each function can share
    the "private" variable. It's not really "static" because you can access
    it outside the function while the lexical variable is in scope, and even
    create references to it. In this example, "increment_count" and
    "return_count" share the variable. One function adds to the value and
    the other simply returns the value. They can both access $count, and
    since it has gone out of scope, there is no other way to access it.

        BEGIN {
            my $count = 1;
            sub increment_count { $count++ }
            sub return_count    { $count }
        }

    To declare a file-private variable, you still use a lexical variable. A
    file is also a scope, so a lexical variable defined in the file cannot
    be seen from any other file.

    See "Persistent Private Variables" in perlsub for more information. The
    discussion of closures in perlref may help you even though we did not
    use anonymous subroutines in this answer. See "Persistent Private
    Variables" in perlsub for details.



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

Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short.  They represent an important
part of the Usenet tradition.  They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.

If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile.  If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.

Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release.  It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.

The perlfaq manual page contains the following copyright notice.

  AUTHOR AND COPYRIGHT

    Copyright (c) 1997-2002 Tom Christiansen and Nathan
    Torkington, and other contributors as noted. All rights 
    reserved.

This posting is provided in the hope that it will be useful but
does not represent a commitment or contract of any kind on the part
of the contributers, authors or their agents.


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

Date: Tue, 25 Oct 2005 22:31:17 -0700
From: robic0@yahoo.com
Subject: Re: getElementsByTagName, tag does not exist
Message-Id: <jr4ul15rtli2rsnnfg3k3lgddnm4uu0r4f@4ax.com>

On Sun, 23 Oct 2005 15:04:54 -0700, robic0@yahoo.com wrote:
Actually, DOM is exponentionaly irrelavent in modern xml parsing.
Its use is fadinding like an old 59 Buick in 1965.
>On Sat, 22 Oct 2005 09:57:21 -0400, "Matt Garrish"
><matthew.garrish@sympatico.ca> wrote:
>Your right Matt. I sometimes make those comments on
>Friday nights after the pub. Dissregard those,
>and sorry for them. But I am usually coherent on here
>and am interrested in technical development.
>I mean no harm. - thanks!
>>
>><robic0@yahoo.com> wrote in message 
>>news:e5ojl1hdas8utf88tt1mknpsullko6gi29@4ax.com...
>>> On Thu, 20 Oct 2005 08:04:45 -0400, "Matt Garrish"
>>> <matthew.garrish@sympatico.ca> wrote:
>>>>Simplistic statements like sax for everything aren't helpful.
>>>
>>> I didn't getcha on this statement..
>>>
>>
>>You only talk about DOM and SAX in your original post (and being the two 
>>most common and supported parsers, that's not surprising). By following up 
>>and saying you'll never use nodes again you're implying that SAX is the only 
>>way to go. That's all that was meant.
>>
>>Matt 
>>



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

Date: Wed, 26 Oct 2005 11:23:19 +0800
From: "sonet" <sonet.all@msa.hinet.net>
Subject: How to detect memory usage in hash?
Message-Id: <djmssr$muv$1@netnews.hinet.net>

How to know how many memory usage in a hash??
I just can use top to see the memory usage?

-----How to rough estimate ?---

my %hash;
for (my $i=0;$i<=10000;$i++){
$hash{abc}{$i}='testing';
}





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

Date: Wed, 26 Oct 2005 04:01:55 GMT
From: Gregory Toomey <nobody@bigpond.com>
Subject: Re: How to detect memory usage in hash?
Message-Id: <435effb2@news.comindico.com.au>

sonet wrote:

> How to know how many memory usage in a hash??
> I just can use top to see the memory usage?
> 
> -----How to rough estimate ?---
> 
> my %hash;
> for (my $i=0;$i<=10000;$i++){
> $hash{abc}{$i}='testing';
> }

For a non-portable linux solution, read /proc/$$/status, and look for a line
with 'VmSize'.

gtoomey



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

Date: Tue, 25 Oct 2005 21:01:00 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Moving data structure around better than globals?
Message-Id: <slrndltoqs.hcj.tadmc@magna.augustmail.com>

Gunnar Hjalmarsson <noreply@gunnar.cc> wrote:
> I have accepted that package globals as well as file scoped lexicals 
> should be avoided if possible,


I'd say "if practicable" rather than "if possible".

It is the _abuse_ of global variables that is often warned against,
they do have their place.

My personal red flag is when there are "many" (over 10?) globals.
When I see that developing, I recast them to a single global hash.


>      $d->{param} - hash ref to CGI parameters
>      $d->{user}  - hash ref to certain data about the current user,
>                    grabbed at authentication
>      $d->{error} - array ref to alerts to be displayed on screen
> 
> I'm passing $d around to just about all the functions, 


So then, it would hit my "impracticable" limit.  :-)


> and it struck me 
> that this approach actually is very similar to using global variables. 


I very often have a single global hash with "configuration"
type values in it.


> So my question is: Why would this be better (if you think it is)?


I wouldn't say that passing the same arg in every function call
is better.

Judicious use of globals is good practice (it's just that the most
common uses are not judicious, so a knee-jerk "don't use globals"
is more often right than it is wrong).


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


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

Date: Tue, 25 Oct 2005 21:46:32 -0700
From: robic0@yahoo.com
Subject: Timelocal input, post good Date filters (from - to)
Message-Id: <t72ul1htar3krqf6aiflt9mr96ive71kbn@4ax.com>

I get some weird concat warnings on print if enabled so its commented.
Post any you have (or make some up).
Here's mine ..

use strict;
#use warnings;

my @dates = ('2/30/5-7/2005', '2/5', '-3/29.2000', '4-12/05',
'-12/01-1/03', '10/2002,8/31/2005', '1/2-asdvf');

for (@dates) {
	my $date = $_;
	print '-'x40,"\n$date\n";
	$date =~ /.*/;
	$date =~
/^(?:\/*)([0-9]+)?(?:\/*)([0-9]+)?(?:\/*)([0-9]+)?(?:\/*)([,-]*)(?:\/*)([0-9]+)?(?:\/*)([0-9]+)?(?:\/*)([0-9]+)?(?:\/*)$/;
	print "date = ($1)($2)($3)($4)($5)($6)($7)\n";
}

__DATA__

----------------------------------------
2/30/5-7/2005
date = (2)(30)(5)(-)(7)(2005)()
----------------------------------------
2/5
date = (2)(5)()()()()()
----------------------------------------
-3/29.2000
date = ()()()()()()()
----------------------------------------
4-12/05
date = (4)()()(-)(12)(05)()
----------------------------------------
-12/01-1/03
date = ()()()()()()()
----------------------------------------
10/2002,8/31/2005
date = (10)(2002)()(,)(8)(31)(2005)
----------------------------------------
1/2-asdvf
date = ()()()()()()()



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

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.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

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


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