[30617] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1862 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Sep 18 14:09:51 2008

Date: Thu, 18 Sep 2008 11:09:17 -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           Thu, 18 Sep 2008     Volume: 11 Number: 1862

Today's topics:
        Accessing an Array Stored in a File hamidrezah@yahoo.com
    Re: Accessing an Array Stored in a File xhoster@gmail.com
    Re: Accessing an Array Stored in a File <jurgenex@hotmail.com>
        editor in perl <lipun4u@gmail.com>
    Re: editor in perl <joost@zeekat.nl>
    Re: editor in perl <sbryce@scottbryce.com>
    Re: editor in perl <ajcrm125@gmail.com>
    Re: editor in perl <john@castleamber.com>
    Re: editor in perl (Steven M. O'Neill)
    Re: editor in perl <RedGrittyBrick@spamweary.invalid>
    Re: editor in perl <jurgenex@hotmail.com>
        eval, "adding up" words to create a variable? <tch@nospam.wpkg.org>
    Re: eval, "adding up" words to create a variable? <josef.moellers@fujitsu-siemens.com>
    Re: eval, "adding up" words to create a variable? <RedGrittyBrick@spamweary.invalid>
    Re: eval, "adding up" words to create a variable? <ben@morrow.me.uk>
    Re: eval, "adding up" words to create a variable? <ben@morrow.me.uk>
    Re: eval, "adding up" words to create a variable? <tch@nospam.wpkg.org>
    Re: eval, "adding up" words to create a variable? <tch@nospam.wpkg.org>
    Re: eval, "adding up" words to create a variable? <jurgenex@hotmail.com>
    Re: eval, "adding up" words to create a variable? <joost@zeekat.nl>
    Re: eval, "adding up" words to create a variable? <tch@nospam.wpkg.org>
    Re: eval, "adding up" words to create a variable? <RedGrittyBrick@spamweary.invalid>
    Re: eval, "adding up" words to create a variable? <jurgenex@hotmail.com>
        IPC:Shareable <clauskick@hotmail.com>
    Re: IPC:Shareable <tzz@lifelogs.com>
    Re: IPC:Shareable <ben@morrow.me.uk>
    Re: pack and byte length. xhoster@gmail.com
        Problem using Data::Translate to convert hex to decimal <ajcrm125@gmail.com>
    Re: Problem using Data::Translate to convert hex to dec <ben@morrow.me.uk>
    Re: Segmentation fault: problem with perl threads xhoster@gmail.com
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Thu, 18 Sep 2008 06:50:30 -0700 (PDT)
From: hamidrezah@yahoo.com
Subject: Accessing an Array Stored in a File
Message-Id: <a2917054-f4d6-461b-b1d6-5a8079248ffb@d45g2000hsc.googlegroups.com>

Hi all:

I have a BIG array. I store it in a file using Storable::store.

I want to use it in another Perl program. The BIG array cannot be
loaded into memory since it is just too BIG.

Any trick to use its copy in the file as an object and access it? It
might be very slow but I don't care about speed. I just want to get
over this memory problem.

In otherwords, can Perl access an object stored in a file as if it
were in memory?

NOTE: I know I can load it in memory. I don't want to. I want to
access it while it sits in the file.

Thanks for any hint.

Regards,

Hrh


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

Date: 18 Sep 2008 16:05:52 GMT
From: xhoster@gmail.com
Subject: Re: Accessing an Array Stored in a File
Message-Id: <20080918120554.062$l6@newsreader.com>

hamidrezah@yahoo.com wrote:
> Hi all:
>
> I have a BIG array. I store it in a file using Storable::store.

Is Storable::store negotiable?

> I want to use it in another Perl program. The BIG array cannot be
> loaded into memory since it is just too BIG.

But then how did it get stored in Storable in the first place?

>
> Any trick to use its copy in the file as an object and access it? It
> might be very slow but I don't care about speed. I just want to get
> over this memory problem.

There are many options for tying an array to disk.  Many of them are not
very good, at least not for the general case.  My favorite would be to
store the array to disk using DBM::Deep instead of Storable.

> In otherwords, can Perl access an object stored in a file as if it
> were in memory?

Yes, there are many ways.  I think DBM::Deep is a very good one.  It has
some gotchas, but less so than other modules I've tried to use to do the
same thing.

If you use DBM::Deep, I'd stick with version 0.983 of it, as after that
point the code takes a turn into trying to be a transactional database,
rather than a nifty way to tie Perl arrays and hashes to disk.


Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.


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

Date: Thu, 18 Sep 2008 09:29:38 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Accessing an Array Stored in a File
Message-Id: <i605d497r9lvl9crdbofcu78bfm5fk6af4@4ax.com>

hamidrezah@yahoo.com wrote:
>I have a BIG array. I store it in a file using Storable::store.
>
>I want to use it in another Perl program. The BIG array cannot be
>loaded into memory since it is just too BIG.
>In otherwords, can Perl access an object stored in a file as if it
>were in memory?
>
>NOTE: I know I can load it in memory. I don't want to. I want to
>access it while it sits in the file.

Honestly I don't know. However one simple workaroud might be use virtual
memory by enlarging your swap space. 

A real solution would be to use a database. Or maybe (depending on your
needs) to upgrade to a 64-bit OS such that you can address more than 4GB
of RAM.

jue


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

Date: Thu, 18 Sep 2008 06:12:04 -0700 (PDT)
From: asit <lipun4u@gmail.com>
Subject: editor in perl
Message-Id: <90880f28-a49f-418e-bd1a-325ce42330ba@k36g2000pri.googlegroups.com>

I am a newbie in perl. I mostly write scripts in windows platform.

Can anyone tell me which is the best editor in perl ????


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

Date: Thu, 18 Sep 2008 15:15:33 +0200
From: Joost Diepenmaat <joost@zeekat.nl>
Subject: Re: editor in perl
Message-Id: <878wtpwq6i.fsf@zeekat.nl>

asit <lipun4u@gmail.com> writes:

> I am a newbie in perl. I mostly write scripts in windows platform.
>
> Can anyone tell me which is the best editor in perl ????

My best editor is the one I use.

See perlfaq3

-- 
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/


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

Date: Thu, 18 Sep 2008 07:33:55 -0600
From: Scott Bryce <sbryce@scottbryce.com>
Subject: Re: editor in perl
Message-Id: <zYKdnYs3gq5LxU_VnZ2dnUVZ_sPinZ2d@comcast.com>

asit wrote:
> I am a newbie in perl. I mostly write scripts in windows platform.
> 
> Can anyone tell me which is the best editor in perl ????

There are a variety of good editors available for the Windows platform.
Which is the best is a matter of personal preference. I like UltraEdit.


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

Date: Thu, 18 Sep 2008 06:51:04 -0700 (PDT)
From: ajcrm125 <ajcrm125@gmail.com>
Subject: Re: editor in perl
Message-Id: <f27b7c94-33ec-442a-91f3-4552859ffbc8@m36g2000hse.googlegroups.com>

On Sep 18, 9:12=A0am, asit <lipu...@gmail.com> wrote:
> I am a newbie in perl. I mostly write scripts in windows platform.
>
> Can anyone tell me which is the best editor in perl ????

Programmers Notepad is the best as far as I'm concerned.  It supports
any language and has a ton of features.
http://www.pnotepad.org/


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

Date: 18 Sep 2008 14:05:58 GMT
From: John Bokma <john@castleamber.com>
Subject: Re: editor in perl
Message-Id: <Xns9B1D5C9084155castleamber@130.133.1.4>

asit <lipun4u@gmail.com> wrote:

> I am a newbie in perl. I mostly write scripts in windows platform.
> 
> Can anyone tell me which is the best editor in perl ????

<http://johnbokma.com/mexit/2008/04/16/which-editor-for-perl-hacking.html>

-- 
John    http://johnbokma.com/ - Hacking & Hiking in Mexico

Perl help in exchange for a gift:
http://johnbokma.com/perl/help-in-exchange-for-a-gift.html


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

Date: Thu, 18 Sep 2008 15:44:04 +0000 (UTC)
From: steveo@panix.com (Steven M. O'Neill)
Subject: Re: editor in perl
Message-Id: <gatt04$npk$1@reader1.panix.com>

Andrew DeFaria  <Andrew@DeFaria.com> wrote:
>-=-=-=-=-=-
>
>John Bokma wrote:
>> asit <lipun4u@gmail.com> wrote:
>>
>>> I am a newbie in perl. I mostly write scripts in windows platform.
>>>
>>> Can anyone tell me which is the best editor in perl ????
>>
>> <http://johnbokma.com/mexit/2008/04/16/which-editor-for-perl-hacking.html>
>Failed to Connect
>
>Firefox can't establish a connection to the server at johnbokma.com.

This works (at least for) now:

http://google.com/search?q=cache:37qZxzVC92kJ:johnbokma.com/mexit/2008/04/16/which-editor-for-perl-hacking.html

(FWIW)
-- 
Steven O'Neill                                  steveo@panix.com
Brooklyn, NY                        http://www.panix.com/~steveo


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

Date: Thu, 18 Sep 2008 16:48:28 +0100
From: RedGrittyBrick <RedGrittyBrick@spamweary.invalid>
Subject: Re: editor in perl
Message-Id: <48d2784c$0$26080$db0fefd9@news.zen.co.uk>


Andrew DeFaria wrote:
> John Bokma wrote:
>> asit <lipun4u@gmail.com> wrote:
>>
>>> I am a newbie in perl. I mostly write scripts in windows platform.
>>>
>>> Can anyone tell me which is the best editor in perl ????
>>
>> <http://johnbokma.com/mexit/2008/04/16/which-editor-for-perl-hacking.html>
> Failed to Connect
> 
> Firefox can't establish a connection to the server at johnbokma.com.
> 
> Though the site seems valid, the browser was unable to establish a 
> connection.
> 
>     * Could the site be temporarily unavailable? Try again later.
>     * Are you unable to browse other sites?  Check the computer's 
> network connection.
>     * Is your computer or network protected by a firewall or proxy? 
> Incorrect settings can interfere with Web browsing.
> 

Worked for me a while ago, works for me now.
Firefox.

-- 
RGB


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

Date: Thu, 18 Sep 2008 09:34:37 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: editor in perl
Message-Id: <6g05d4h6vunp8hjs332etdjbq0ifukqqtc@4ax.com>

asit <lipun4u@gmail.com> wrote:
>I am a newbie in perl. I mostly write scripts in windows platform.
>Can anyone tell me which is the best editor in perl ????

I don't know of any editor in Perl. If you are asking for the best
editor for programming Perl, then please see the FAQ "perldoc -q editor"
	
	"Is there an IDE or Windows Perl Editor?"

As usual Emacs has a powerful Perl mode (see "perldoc -q emacs"). I
suppose other editors do, too.

jue


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

Date: Thu, 18 Sep 2008 17:01:13 +0200
From: Tomasz Chmielewski <tch@nospam.wpkg.org>
Subject: eval, "adding up" words to create a variable?
Message-Id: <gatqfp$ccm$1@online.de>

I have a couple of variables, like:

my $thing_added = "test1";
my $otherthing_added = "test2";


I also have an array:

@array = ("thing", "otherthing", "no_such_variable");


Now, I would like to go through every element in the array, and check if 
there is a corresponding "<array_element>_added" variable.


For example, for "thing", I would like to check if there is a 
$thing_added variable. This should evaluate true, as I defined that 
variable earlier.

Similarly, for "otherthing", I would like to check if there is a 
$otherthing_added variable. This one should also evaluate true.

For "no_such_variable" element, a check for $no_such_variable_added 
should evaluate false, as this variable was not defined.


I tried using "eval" to construct a variable out of yet another variable 
value and a "_added" word, but haven't been successful.

Any hints?


-- 
Tomasz Chmielewski
http://wpkg.org


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

Date: Thu, 18 Sep 2008 17:18:02 +0200
From: Josef Moellers <josef.moellers@fujitsu-siemens.com>
Subject: Re: eval, "adding up" words to create a variable?
Message-Id: <gatrf6$bql$1@nntp.fujitsu-siemens.com>

Tomasz Chmielewski wrote:
> I have a couple of variables, like:
> 
> my $thing_added = "test1";
> my $otherthing_added = "test2";
> 
> 
> I also have an array:
> 
> @array = ("thing", "otherthing", "no_such_variable");
> 
> 
> Now, I would like to go through every element in the array, and check if 
> there is a corresponding "<array_element>_added" variable.
> 
> 
> For example, for "thing", I would like to check if there is a 
> $thing_added variable. This should evaluate true, as I defined that 
> variable earlier.
> 
> Similarly, for "otherthing", I would like to check if there is a 
> $otherthing_added variable. This one should also evaluate true.
> 
> For "no_such_variable" element, a check for $no_such_variable_added 
> should evaluate false, as this variable was not defined.
> 
> 
> I tried using "eval" to construct a variable out of yet another variable 
> value and a "_added" word, but haven't been successful.
> 
> Any hints?

Every time you want to use variables as part of variable names, you're 
better off using a hash.

Usually you save your life by using a hash as you usually get stoned to 
death by asking these kinds of questions here.

Usually,

Josef

-- 
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
	If failure had no penalty success would not be a prize (T.  Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html


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

Date: Thu, 18 Sep 2008 16:27:47 +0100
From: RedGrittyBrick <RedGrittyBrick@spamweary.invalid>
Subject: Re: eval, "adding up" words to create a variable?
Message-Id: <48d27378$0$26078$db0fefd9@news.zen.co.uk>


Tomasz Chmielewski wrote:
> I have a couple of variables, like:
> 
> my $thing_added = "test1";
> my $otherthing_added = "test2";
> 
> 
> I also have an array:
> 
> @array = ("thing", "otherthing", "no_such_variable");
> 
> 
> Now, I would like to go through every element in the array, and check if 
> there is a corresponding "<array_element>_added" variable.
> 
> 
> For example, for "thing", I would like to check if there is a 
> $thing_added variable. This should evaluate true, as I defined that 
> variable earlier.
> 
> Similarly, for "otherthing", I would like to check if there is a 
> $otherthing_added variable. This one should also evaluate true.
> 
> For "no_such_variable" element, a check for $no_such_variable_added 
> should evaluate false, as this variable was not defined.
> 
> 
> I tried using "eval" to construct a variable out of yet another variable 
> value and a "_added" word, but haven't been successful.
> 
> Any hints?
> 
> 
$ perl -e '$x=foo; $foo=1; print defined($$x)?"Yes\n":"No\n"'
Yes

$ perl -e '$x=foo; $foo=0; print defined($$x)?"Yes\n":"No\n"'
Yes

$ perl -e '$x=foo; $bar=0; print defined($$x)?"Yes\n":"No\n"'
No

But IMHO you really really should be using hashes. Something not 
entirely unlike:

   my $thingies{thing_added} = "test1";
   my $thingies{otherthing_added} = "test2";
   ...
   for (@array) {
      if (defined($thingies{$_})) {
         ...
      }
   }
(untested - caveat emptor)

I'm sure there's a FAQ that advises against the $$x kind of crazy 
foolishness.

-- 
RGB


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

Date: Thu, 18 Sep 2008 16:20:05 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: eval, "adding up" words to create a variable?
Message-Id: <568aq5-l03.ln1@osiris.mauzo.dyndns.org>


Quoth Tomasz Chmielewski <tch@nospam.wpkg.org>:
> I have a couple of variables, like:
> 
> my $thing_added = "test1";
> my $otherthing_added = "test2";
> 
> 
> I also have an array:
> 
> @array = ("thing", "otherthing", "no_such_variable");
> 
> 
> Now, I would like to go through every element in the array, and check if 
> there is a corresponding "<array_element>_added" variable.

perldoc -q "variable name" explains why this is a bad idea (it's
actually impossible for 'my' variables, short of using deep magic like
PadWalker). What are you actually trying to do?

Ben

-- 
               We do not stop playing because we grow old; 
                  we grow old because we stop playing.
                            ben@morrow.me.uk


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

Date: Thu, 18 Sep 2008 16:39:45 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: eval, "adding up" words to create a variable?
Message-Id: <1b9aq5-vi4.ln1@osiris.mauzo.dyndns.org>


Quoth RedGrittyBrick <RedGrittyBrick@spamweary.invalid>:
> 
> $ perl -e '$x=foo; $foo=1; print defined($$x)?"Yes\n":"No\n"'
> Yes

    ~% perl -le'
        use strict;
        my $x = "foo"; 
        my $foo = 1; 
        print defined($$x) ? "Yes" : "No";'
    Can't use string ("foo") as a SCALAR ref while "strict refs" in use
    at -e line 5

    ~% perl -le'
        my $x = "foo";
        my $foo = 1;
        print defined($$x) ? "Yes" : "No";'
    No

> I'm sure there's a FAQ that advises against the $$x kind of crazy 
> foolishness.

perldoc -q "variable name"

There's also 'use strict', which forbids it entirely.

Ben

-- 
I've seen things you people wouldn't believe: attack ships on fire off
the shoulder of Orion; I watched C-beams glitter in the dark near the
Tannhauser Gate. All these moments will be lost, in time, like tears in rain.
Time to die.                                                   ben@morrow.me.uk


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

Date: Thu, 18 Sep 2008 18:08:59 +0200
From: Tomasz Chmielewski <tch@nospam.wpkg.org>
Subject: Re: eval, "adding up" words to create a variable?
Message-Id: <gatuer$j5k$1@online.de>

Josef Moellers schrieb:
> Tomasz Chmielewski wrote:

(...)

>> Any hints?
> 
> Every time you want to use variables as part of variable names, you're 
> better off using a hash.
> 
> Usually you save your life by using a hash as you usually get stoned to 
> death by asking these kinds of questions here.

Ha ha, I take it as a good advice for the future then :)


-- 
Tomasz Chmielewski
http://wpkg.org



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

Date: Thu, 18 Sep 2008 18:15:31 +0200
From: Tomasz Chmielewski <tch@nospam.wpkg.org>
Subject: Re: eval, "adding up" words to create a variable?
Message-Id: <gatur3$abo$1@online.de>

Ben Morrow schrieb:
> Quoth Tomasz Chmielewski <tch@nospam.wpkg.org>:
>> I have a couple of variables, like:
>>
>> my $thing_added = "test1";
>> my $otherthing_added = "test2";
>>
>>
>> I also have an array:
>>
>> @array = ("thing", "otherthing", "no_such_variable");
>>
>>
>> Now, I would like to go through every element in the array, and check if 
>> there is a corresponding "<array_element>_added" variable.
> 
> perldoc -q "variable name" explains why this is a bad idea (it's
> actually impossible for 'my' variables, short of using deep magic like
> PadWalker). What are you actually trying to do?

I have a couple of such "ifs", and they differ only slightly:


if ($product_id_added == 0 && length $product_id) {
	&add_params("product_id", $product_id, $lun, $driver);
}


if ($vendor_id_added == 0 && length $vendor_id) {
	&add_params("vendor_id", $vendor_id, $lun, $driver);
}



I thought of replacing these all "ifs" which differ only slightly by
going through array elements, and replacing each part.

my @array = ("product_id", "vendor_id", ...)

But it looks like I have to rethink that approach.


-- 
Tomasz Chmielewski
http://wpkg.org


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

Date: Thu, 18 Sep 2008 09:43:06 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: eval, "adding up" words to create a variable?
Message-Id: <dq05d49874patm3j1o87e13fao1psomfa4@4ax.com>

Tomasz Chmielewski <tch@nospam.wpkg.org> wrote:
>I have a couple of variables, like:
>my $thing_added = "test1";
>my $otherthing_added = "test2";
>
>I also have an array:
>@array = ("thing", "otherthing", "no_such_variable");
>
>Now, I would like to go through every element in the array, and check if 
>there is a corresponding "<array_element>_added" variable.
[...]
>Any hints?

Short answer: don't do that, it is A Bad Idea (TM). 
Longer answer: you are looking for symbolic references. Please see the
history of this NG as well as the FAQ ("variable as a variable name")
why that is not a good idea.
Yet longer answer: Use a hash, dude, that's what they are for.

my %added;
$added{'thing'} = 'test1';
$added{'otherthing'} = 'test2';
 ....
if (exists($added{'thing'})) {
	print "Someone has added 'thing' to \%added";
}

jue


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

Date: Thu, 18 Sep 2008 19:10:18 +0200
From: Joost Diepenmaat <joost@zeekat.nl>
Subject: Re: eval, "adding up" words to create a variable?
Message-Id: <87zlm5v0qt.fsf@zeekat.nl>

Tomasz Chmielewski <tch@nospam.wpkg.org> writes:

> I have a couple of such "ifs", and they differ only slightly:
>
>
> if ($product_id_added == 0 && length $product_id) {
> 	&add_params("product_id", $product_id, $lun, $driver);
> }
>
>
> if ($vendor_id_added == 0 && length $vendor_id) {
> 	&add_params("vendor_id", $vendor_id, $lun, $driver);
> }
>
>
>
> I thought of replacing these all "ifs" which differ only slightly by
> going through array elements, and replacing each part.
>
> my @array = ("product_id", "vendor_id", ...)
>
> But it looks like I have to rethink that approach.

You should. If you choose the right datastructure, you can probably
reduce the amount of code a lot more than if you "just" use symbolic
references here and there.

Also, it looks like you're using 2 flags to mark if something was
added. You won't need to if you're using a hash:

my %added;

# add a vendor id
$added{vendor} = 2;

# don't do anything with products

for my $key (qw(vendor products foo)) {
    next unless exists $added{$key};
    add_params("${key}_id",$added{$key},...)
}

also: IMHO it's bad style to use the &foo() construct to call
functions. foo() is a little shorted and has less potential problems.


-- 
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/


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

Date: Thu, 18 Sep 2008 19:17:16 +0200
From: Tomasz Chmielewski <tch@nospam.wpkg.org>
Subject: Re: eval, "adding up" words to create a variable?
Message-Id: <gau2es$brd$1@online.de>

Joost Diepenmaat schrieb:
> Tomasz Chmielewski <tch@nospam.wpkg.org> writes:
> 
>> I have a couple of such "ifs", and they differ only slightly:
>>
>>
>> if ($product_id_added == 0 && length $product_id) {
>> 	&add_params("product_id", $product_id, $lun, $driver);
>> }
>>
>>
>> if ($vendor_id_added == 0 && length $vendor_id) {
>> 	&add_params("vendor_id", $vendor_id, $lun, $driver);
>> }
>>
>>
>>
>> I thought of replacing these all "ifs" which differ only slightly by
>> going through array elements, and replacing each part.
>>
>> my @array = ("product_id", "vendor_id", ...)
>>
>> But it looks like I have to rethink that approach.
> 
> You should. If you choose the right datastructure, you can probably
> reduce the amount of code a lot more than if you "just" use symbolic
> references here and there.
> 
> Also, it looks like you're using 2 flags to mark if something was
> added. You won't need to if you're using a hash:
> 
> my %added;
> 
> # add a vendor id
> $added{vendor} = 2;
> 
> # don't do anything with products
> 
> for my $key (qw(vendor products foo)) {
>     next unless exists $added{$key};
>     add_params("${key}_id",$added{$key},...)
> }

Thanks for the tip.


> also: IMHO it's bad style to use the &foo() construct to call
> functions. foo() is a little shorted and has less potential problems.

Besides being "bad style", what potential problems can it make? (code 
readability? something else?)


-- 
Tomasz Chmielewski
http://wpkg.org


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

Date: Thu, 18 Sep 2008 18:38:00 +0100
From: RedGrittyBrick <RedGrittyBrick@spamweary.invalid>
Subject: Re: eval, "adding up" words to create a variable?
Message-Id: <gau3ls$a1s$1@registered.motzarella.org>


Tomasz Chmielewski wrote:
> Joost Diepenmaat schrieb:
>> also: IMHO it's bad style to use the &foo() construct to call
>> functions. foo() is a little shorted and has less potential problems.
> 
> Besides being "bad style", what potential problems can it make? (code 
> readability? something else?)
> 

See perldoc perlsub. It circumvents prototypes and causes current @_ to 
be visible to the called subroutine. In 99% of cases you don't want this.

Using the &sub call notation was common in Perl 4 days but IIRC that was 
superseded back in 1993, more than fifteen years ago.

Presumably you are learning Perl from something that is 15 years out of 
date.


-- 
RGB


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

Date: Thu, 18 Sep 2008 10:37:58 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: eval, "adding up" words to create a variable?
Message-Id: <7m35d4d92lghk9k9rce9birfq38f455qck@4ax.com>

Tomasz Chmielewski <tch@nospam.wpkg.org> wrote:
>Joost Diepenmaat schrieb:
>> also: IMHO it's bad style to use the &foo() construct to call
>> functions. foo() is a little shorted and has less potential problems.
>
>Besides being "bad style", what potential problems can it make? (code 
>readability? something else?)

Do you know what the & does in a function call? It is not for decoration
but it does have an actual functionality, after all.

If you don't want to use prototypes then just don't use them. Most
people don't use them.
But declaring prototypes (don't know if you did, you didn't show us the
declaration of add_params) and then explicitely surpressing the
prototype check for every single function call is just plain stupid.

It's like installing a new electrical pump at your well but still using
the old bucket, only now you have to work your way around the suction
hose which is in the way.

jue


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

Date: Thu, 18 Sep 2008 06:55:15 -0700 (PDT)
From: Snorik <clauskick@hotmail.com>
Subject: IPC:Shareable
Message-Id: <bed8ff4e-0d4a-4e70-aed7-2a2bf31aa69d@j22g2000hsf.googlegroups.com>

Hello everyone,

I am trying to speed up a few perl scripts by forking them.
Unfortunately, I need to pass back to the parent.
I am using named pipes at other places, but this time, I wanted to use
shared memory (this being on *nix).

The first case is basically traversing a HUGE directory tree, looking
for certain files and returning them.

The idea is to fork finds from a specific point of the directory tree,
gather all the files in an array for each child process and then store
that array as reference as value of the hash.


For this, I have done:

sub get_fbas_for_rg
  {
         my $rg = shift;
          my @children;
          use IPC::Shareable;
          use Data::Dumper;
         use constant MYGLUE => 'Test';
          my %fba_hash;
       my $handle = tie (%fba_hash, IPC::Shareable, MYGLUE, {create =>
1, mode => 0666}) or die "cannot tie to shared memory: $! \n";

     my @ggs = qx (ls /default/main/www/$rg | grep -v STAGING | grep -
v WORKAREA | grep -v EDITION);

  foreach  my $gg (@ggs)
  {
                  chomp $gg;
                  my $gg_fba = $gg."_FBAs";

                 my $pid = fork();

                  if ($pid)
                  {
                          push(@children, $pid);
                  }
                  elsif ($pid == 0)
                 {
                          my @fbas = (qx (/usr/bin/find /default/main/
www/$rg/$gg/WORKAREA/workarea/$gg_fba -type f ));
                        $handle->shlock();
                        push (@{$fba_hash{$gg}}, @fbas);
                        $handle->shunlock();
                         exit (0);
                }
                else
                {
                        print STDERR "\nERROR: fork failed: $!\n";
                }
}

          foreach (@children)
          {
                  waitpid($_, 0);
          }
          return %fba_hash;
 }

Now, If I call this function, it seems to work fine, only the hash
values contain only the scalars of the array, at least that is what
Data Dumper tells me:

$VAR1 = {
          'dir1' => 3858,
          'dir2' => 2394,
          'dir3' => 2075
        };


This is what I do in the script:

my %fbas = TestPackage::get_fbas_for_rg("test");

print Dumper \%fbas;

foreach my $gg (keys %fbas)
{
        print $gg."\n";
        foreach my $fba (sort @{$fbas{$gg}})
        {
               print $fba."\n";
        }
}


The foreach loop does not return anything (understandable since the
hash value only contains the scalar of the array).
Again, my question is: how do I manage to receive the actual array in
the calling script instead of just a hash containing my designated
keys and the sizes of the arrays as values?

I would be very grateful for some help.


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

Date: Thu, 18 Sep 2008 10:19:31 -0500
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: IPC:Shareable
Message-Id: <863ajxmqgs.fsf@lifelogs.com>

On Thu, 18 Sep 2008 06:55:15 -0700 (PDT) Snorik <clauskick@hotmail.com> wrote: 

S>           use IPC::Shareable;

Try IPC::ShareLite or even Tie::ShareLite (easiest, hash interface).
They work better for me.

S> Now, If I call this function, it seems to work fine, only the hash
S> values contain only the scalars of the array, at least that is what
S> Data Dumper tells me:

S> $VAR1 = {
S>           'dir1' => 3858,
S>           'dir2' => 2394,
S>           'dir3' => 2075
S>         };

You're assigning @array to the hash value; the value can only be a
scalar so you get the size of the array instead of its contents.

See the Tie::ShareLite docs, especially section 'REFERENCES,' for a
better solution.

Ted


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

Date: Thu, 18 Sep 2008 16:18:18 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: IPC:Shareable
Message-Id: <q28aq5-l03.ln1@osiris.mauzo.dyndns.org>


Quoth Snorik <clauskick@hotmail.com>:
> Hello everyone,
> 
> I am trying to speed up a few perl scripts by forking them.
> Unfortunately, I need to pass back to the parent.
> I am using named pipes at other places, but this time, I wanted to use
> shared memory (this being on *nix).
> 
> The first case is basically traversing a HUGE directory tree, looking
> for certain files and returning them.
> 
> The idea is to fork finds from a specific point of the directory tree,
> gather all the files in an array for each child process and then store
> that array as reference as value of the hash.
> 
> 
> For this, I have done:
> 
> sub get_fbas_for_rg
>   {
>          my $rg = shift;
>           my @children;
>           use IPC::Shareable;
>           use Data::Dumper;

It's best not to 'use' modules inside a sub (except for lowercase
pragmata which have lexical effect). It gives the false impression that
the exporter subs are only available in that sub.

>          use constant MYGLUE => 'Test';
>           my %fba_hash;
>        my $handle = tie (%fba_hash, IPC::Shareable, MYGLUE, {create =>
> 1, mode => 0666}) or die "cannot tie to shared memory: $! \n";
> 
>      my @ggs = qx (ls /default/main/www/$rg | grep -v STAGING | grep -
> v WORKAREA | grep -v EDITION);

    use File::Slurp qw/read_dir/;

    my @ggs =
        grep !/EDITION/,
        grep !/WORKAREA/,
        grep !/STAGING/,
        read_dir "/default/main/www/$rg";

or

        grep !/EDITION|WORKAREA|STAGING/,

of course.

>                           my @fbas = (qx (/usr/bin/find /default/main/
> www/$rg/$gg/WORKAREA/workarea/$gg_fba -type f ));

I would use File::Find::Rule for this.

    my @fbas = File::Find::Rule->file
        ->in("/default/main/www/$rg/$gg/WORKAREA/workarea/$gg_fba");

>                         $handle->shlock();
>                         push (@{$fba_hash{$gg}}, @fbas);

You cannot assign a ref to an IPC::Shareable tied hash. The other
process has no way of following that ref: it refers to data structures
that aren't in shared memory. I would suggest using Storable:

    use Storable qw/freeze/;

    $fba_hash{$gg} = freeze \@fbas;

and then retrieve it with

    use Storable qw/thaw/;

    my @fbas = @{ thaw $fba_hash{$gg} };

Ben

-- 
   Although few may originate a policy, we are all able to judge it.
                                               Pericles of Athens, c.430 B.C.
  ben@morrow.me.uk


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

Date: 18 Sep 2008 16:13:51 GMT
From: xhoster@gmail.com
Subject: Re: pack and byte length.
Message-Id: <20080918121352.592$uE@newsreader.com>

Fabrice <fabrice.gautier@gmail.com> wrote:
> Hi,
>
> Whats the proper way to figure out the length , in bytes, of the
> result of a pack command ?

What are you packing?

perl -le 'print "length of float is ", length pack "f",0;'

If you are packing unicode, things might be different.


> That is: I use the pack command then write the result in a file, I
> want to know the actual number of bytes written ?

If you use the "syswrite" to write to disk, it will tell you how many bytes
it wrote.  Or you can do a "tell" before and after and do the math.

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.


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

Date: Thu, 18 Sep 2008 06:32:08 -0700 (PDT)
From: ajcrm125 <ajcrm125@gmail.com>
Subject: Problem using Data::Translate to convert hex to decimal
Message-Id: <7b863983-7523-457f-8448-90ad43ec5d99@x35g2000hsb.googlegroups.com>

For whatever reason... the h2d is not converting some characters.
Here's an example trying to convert "09":

===================================================
use lib "/afs/btv.ibm.com/u/adamc/usr/lib/perl5/site_perl/5.8.5";
use Data::Translate;
$data = new Translate;

($status,$result) = $data->h2d("09");
print "result is $result\n";
===================================================

$ h2d_example.pl
result is 0



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

Date: Thu, 18 Sep 2008 15:58:25 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Problem using Data::Translate to convert hex to decimal
Message-Id: <ht6aq5-l03.ln1@osiris.mauzo.dyndns.org>


Quoth ajcrm125 <ajcrm125@gmail.com>:
> For whatever reason... the h2d is not converting some characters.
> Here's an example trying to convert "09":
> 
> ===================================================
> use lib "/afs/btv.ibm.com/u/adamc/usr/lib/perl5/site_perl/5.8.5";
> use Data::Translate;
> $data = new Translate;
> 
> ($status,$result) = $data->h2d("09");
> print "result is $result\n";
> ===================================================

Why not just use hex()?

    ~% perl -le'print hex("0f")
    15

Ben

-- 
Heracles: Vulture! Here's a titbit for you / A few dried molecules of the gall
   From the liver of a friend of yours. / Excuse the arrow but I have no spoon.
(Ted Hughes,        [ Heracles shoots Vulture with arrow. Vulture bursts into ]
 'Alcestis')        [ flame, and falls out of sight. ]         ben@morrow.me.uk


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

Date: 18 Sep 2008 17:02:05 GMT
From: xhoster@gmail.com
Subject: Re: Segmentation fault: problem with perl threads
Message-Id: <20080918130207.343$mn@newsreader.com>

kath <nitte.sudhir@gmail.com> wrote:

Hi Kath,

The data structure you posted only has one sc_name section and that sc_name
only has one elements section.  Is that enough to reliably replicate the
seg fault?

I've stripped most parts of your code that seem to be irrelevant
to Perl's threading and are relevant only to your specific use case.  It
is just not feasible to work with unsimplified code.  The extra stuff is
too distracting, and the line-wrapping as we post back and forth make
it far to confusing.

use strict;
use warnings;
use threads;
my $VAR1 = [1..25];
_replicate($VAR1);

sub _replicate {
        my $ref = shift;
        my @thr_arr = ();
        print "Creating parallel threads", $/;

        foreach my $robj(@$ref){
        my $th = threads->create(\&worker, $robj);
                push @thr_arr, $th;
        }
        print "\twaiting for threads to finish its job...", $/;
        foreach my $t(@thr_arr){
                if (defined $t){
                        my $k = $t->join();
                }
        }
        print "\tFinished replicating dependencies", $/;
}

sub worker{
        my $robj = shift;

        print "Thread-",threads->self->tid," executing ", $robj, "\n";
        my $x=int rand(10);
        system "sleep $x";
}
__END__

It does not segfault for me on 5.8.8.  Does it segfault for you?


> Firstly, thanks so much.
> Below is the same set of functions im posting as earlier. Because i
> think the problem is lies here. I tried running my script on perl5.8.8
> and still i get 'Segementation fault' even if i actually execute the
> rsync command in thread or just print rsync command in thread and
> return.
> I strongly believe, i might be calling join() method on thread object
> which might have died after finishing its job. Hence i try to
> dereference a reference which is deallocated(may be or ..?).
>
> This happens because, when 10 threads are running parallely and i wait
> for a 2nd thread, suppose, to join. Meanwhile 3rd or 4th or
> 8th(anything till 10) might have finished running.

When they finish running, they should wait patiently to be joined.
Ensuring this happens is Perl's job.  Adding your own code to try to ensure
it will just complicate the issue.  For use help, you need to make your
code simpler, not more complicated.


Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.


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

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 V11 Issue 1862
***************************************


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