[30535] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1778 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Aug 7 21:09:46 2008

Date: Thu, 7 Aug 2008 18:09: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           Thu, 7 Aug 2008     Volume: 11 Number: 1778

Today's topics:
    Re: CLPM - a help group? <brian.d.foy@gmail.com>
    Re: CLPM - a help group? <worrall+unet@cs.bris.ac.uk>
    Re: CLPM - a help group? <worrall+unet@cs.bris.ac.uk>
    Re: How to check for filetype existence quickly <hjp-usenet2@hjp.at>
    Re: How to check for filetype existence quickly <fidokomik@gmail.com>
    Re: How to check for filetype existence quickly <ben@morrow.me.uk>
    Re: How to check for filetype existence quickly <jimsgibson@gmail.com>
    Re: Installing CPAN modules in production system <RedGrittyBrick@SpamWeary.foo>
    Re: Installing CPAN modules in production system <yogeshkagrawal@gmail.com>
    Re: make pipes hot (was: a weird problem) <zhilianghu@gmail.com>
    Re: Need help with perlxs and C strings xhoster@gmail.com
    Re: Need help with perlxs and C strings <ben@morrow.me.uk>
    Re: Need help with perlxs and C strings xhoster@gmail.com
    Re: nested strings <hjp-usenet2@hjp.at>
        OO Perl <mattj.morrison@gmail.com>
        Trying to understand a constructor with an array. <nowhere@nowhere.com>
    Re: Trying to understand a constructor with an array. <fawaka@gmail.com>
    Re: Trying to understand a constructor with an array. xhoster@gmail.com
    Re: Trying to understand a constructor with an array. <hjp-usenet2@hjp.at>
    Re: Trying to understand a constructor with an array. <fawaka@gmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Thu, 07 Aug 2008 09:06:07 -0500
From: brian d  foy <brian.d.foy@gmail.com>
Subject: Re: CLPM - a help group?
Message-Id: <070820080906078433%brian.d.foy@gmail.com>

In article <Girmk.19323$Ri.16261@flpi146.ffdc.sbc.com>, Adam Worrall
<worrall+unet@cs.bris.ac.uk> wrote:


> > This still isn't a help desk, but I'm usually happy to help people when
> > I can. :)
> 
> No disrespect intended, but I don't think you've ever spent any time on 
> a volunteer-run help desk before. Oh wait, you post in clpm :)

I've sat on volunteer help desks and paid help desks. You might say
that you mean no disrespect, but fuck you. You said it, you mean it,
and you're too much of a pussy to stand by what you typed and posted.
Take your smiley and shove it up you ass.

But no matter. You're just another shithead who won't be around in a
year when I'm still answering questions and helping people.


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

Date: Thu, 07 Aug 2008 08:33:33 -0700
From: Adam Worrall <worrall+unet@cs.bris.ac.uk>
Subject: Re: CLPM - a help group?
Message-Id: <iBEmk.18098$jI5.8326@flpi148.ffdc.sbc.com>

Tad J McClellan wrote:
> Adam Worrall <worrall+unet@cs.bris.ac.uk> wrote:
>> RedGrittyBrick wrote:
> 
>>> If I were to say "comp.lang.perl.misc is not a help desk" I would be 
>>> merely stating my opinion 
> 
> 
> Just as Adam is free to state his opinion that it _is_ a help desk.

Ok.

>>> on the consensus view (as observed by me) of 
>>> the main participants
> 
>> You're assuming the consensus (as in everyone's opinion) is in agreeance 
>> to what you are saying, and that is something you cannot really confirm. 
> 
> 
> And neither can you confirm that it is not the consensus.
> 
> So where does that leave us?

I leaves us with a charter that doesn't say it is or isn't a "help 
desk", and anyone attempting to make authoritative claims it no better 
than a DNS server giving responses for a domain it is not responsible for.

>> further more, stop perpetuating the the false view that all help desks 
>> are for paid organizations, as plenty are volunteer-run and many are 
>> non-profit.
> 
> 
> It is not the pay vs. no pay.

Actually it is, in terms of types of help desks out there.

> It is the "obligation to help" that most people associate with
> a "help desk". I would expect that even volunteers at a help desk
> are obligated to deliver help.

How can a _volunteer_ service be _obligated_ to do anything? They are 
volunteers...

> There is no obligation to help in clpmisc, which is why people
> say that it is not a help desk.

Which is wrong, because clpmisc functions as a volunteer help service.

   - Adam


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

Date: Thu, 07 Aug 2008 10:31:37 -0700
From: Adam Worrall <worrall+unet@cs.bris.ac.uk>
Subject: Re: CLPM - a help group?
Message-Id: <%jGmk.18387$89.2741@nlpi069.nbdc.sbc.com>

RedGrittyBrick wrote:
> Adam Worrall wrote:
>> RedGrittyBrick wrote:
>>> Adam Worrall wrote:
>>
>>> When I say "the sky is blue" it is an observation, I am not 
>>> legislating what the sky may or may not do.
>>
>> But it is a known fact that the sky is blue, 
> 
> Today it is grey.

The clouds are grey, the sky is still blue.

>> which is different than a handful of people telling you that a group 
>> shouldn't be used in a why _they_ don't happen to approve of.
> 
> You miss my point. I can observe that CLPM does not behave like a help 
> desk. That does not mean I am "legislating" what CLPM participants may 
> do. Whether my observation is right or wrong has no bearing on this.

but in saying that "CLPM does not behave like a help desk" you are wrong 
in that it depends on what kind of help desk. It is in fact comparable 
to a volunteer based help service, where no one is obligated to help, 
but do so out of their own goodness and willingness to help.

> Tad probably has it right, some posters presume that readers are under 
> some obligation to answer their questions by supplying ready-made 
> solutions.

While it may seem _some_ readers may think that way (and that that might 
be just a mistaken perception) but it doesn't change that this group, 
like many others, functions as a volunteer help service.

> You often see "if you can't answer my question just shut up" from 
> posters new to a newsgroup when faced with a pointer to a FAQ or a 
> criticism of their posting style.

I don't see much of that at all. You seem to be taking what is actually 
a minority case and posing it as a majority example. More often than 
not, what I see is some level of gratitude, even if it's just a simple 
"thanks." I'm sorry that you do not see this.

   - Adam


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

Date: Thu, 7 Aug 2008 20:20:12 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: How to check for filetype existence quickly
Message-Id: <slrng9mf6t.sso.hjp-usenet2@hrunkner.hjp.at>

On 2008-08-06 10:18, Justin C <justin.0805@purestblue.com> wrote:
> On 2008-08-06, fidokomik <fidokomik@gmail.com> wrote:
>> I have directory, say "c:\documents" on Windows or "/home/petr/
>> documents" on Linux. In this directory many files are stored with many
>> filetypes (extensions), say *.doc, *.txt, *.zip. I need to find fastes
>> way how to check if some filetype exist. Now I use routine where I use
>> readdir() and return true if passed filetype is first time found but
>> when I have huge number of files but passed filetype is not found then
>> routine is very slow before return false.
>> Any idea?
>
> ls | egrep doc\|txt\|zip

Please not that the OP is passed *one* filetype. So that would be 

    ls | egrep '\.doc$'

or

    ls | egrep '\.txt$'

or 

    ls | egrep '\.zip$'

instead. Which can be simplified to "ls *.doc", etc. Which can be
simplified to a call to glob().


> (you need to escape the 'or' operator, and use egrep instead of grep)
>
> I know you want to use perl, but perl won't be as fast as this... unless
> there are a very, very large[1] number of files.

Perl is also likely to be faster for a small number of files - spawning
a shell which then spawns two other programs is not exactly a cheap
operation.

	hp


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

Date: Thu, 7 Aug 2008 17:23:49 -0700 (PDT)
From: fidokomik <fidokomik@gmail.com>
Subject: Re: How to check for filetype existence quickly
Message-Id: <491ff6d8-af23-432f-897f-8b32ba36fef9@34g2000hsh.googlegroups.com>

On Aug 6, 12:02=A0pm, Lars Eighner <use...@larseighner.com> wrote:
> Did you try globbing to see if it is any faster:
>
> $found =3D 0;
> if (</usr/home/lars/saves/*.cgi>){
> $found =3D 1;
>
> }
>
Hmm, easy and quick. Thank you Lars. But how to pass variable to this
but avoid eval()? Is it possible?

I thinked up this only:

my $searchfor =3D 'c:/images/*.jpg';
if(checkit($searchfor)) {do_something}
else {do_other}

sub checkit {
return 1 if( eval('<' . shift . '>') );
return 0;
}



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

Date: Fri, 8 Aug 2008 01:46:06 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: How to check for filetype existence quickly
Message-Id: <ejhsm5-ftg.ln1@osiris.mauzo.dyndns.org>


Quoth fidokomik <fidokomik@gmail.com>:
> On Aug 6, 12:02 pm, Lars Eighner <use...@larseighner.com> wrote:
> > Did you try globbing to see if it is any faster:
> >
> > $found = 0;
> > if (</usr/home/lars/saves/*.cgi>){
> > $found = 1;
> >
> > }
> >
> Hmm, easy and quick. Thank you Lars. But how to pass variable to this
> but avoid eval()? Is it possible?

perldoc -f glob

glob is the function that underlies this meaning of <>, and it's
probably cleanest to simply call it directly. If you carefully read the
section on the <> operator in perldoc perlop, you will see that it is
possible to use variables in the glob form of <>, but rather tricky.

You may also want to look at the File::Glob or (as you appear to be on
Win32) the File::DosGlob extension, which implement other forms of
globbing.

Ben

-- 
'Deserve [death]? I daresay he did. Many live that deserve death. And some die
that deserve life. Can you give it to them? Then do not be too eager to deal
out death in judgement. For even the very wise cannot see all ends.'
                                                               ben@morrow.me.uk


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

Date: Thu, 07 Aug 2008 17:56:42 -0700
From: Jim Gibson <jimsgibson@gmail.com>
Subject: Re: How to check for filetype existence quickly
Message-Id: <070820081756428903%jimsgibson@gmail.com>

In article
<491ff6d8-af23-432f-897f-8b32ba36fef9@34g2000hsh.googlegroups.com>,
fidokomik <fidokomik@gmail.com> wrote:

> On Aug 6, 12:02 pm, Lars Eighner <use...@larseighner.com> wrote:
> > Did you try globbing to see if it is any faster:
> >
> > $found = 0;
> > if (</usr/home/lars/saves/*.cgi>){
> > $found = 1;
> >
> > }
> >
> Hmm, easy and quick. Thank you Lars. But how to pass variable to this
> but avoid eval()? Is it possible?
> 
> I thinked up this only:
> 
> my $searchfor = 'c:/images/*.jpg';
> if(checkit($searchfor)) {do_something}
> else {do_other}
> 
> sub checkit {
> return 1 if( eval('<' . shift . '>') );
> return 0;
> }
> 

Use the 'glob' function (untested):

  return 1 if glob shift;
  return 0;

Or possibly

  return scalar glob shift;

See 'perldoc -f glob'

-- 
Jim Gibson


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

Date: Thu, 07 Aug 2008 14:51:38 +0100
From: RedGrittyBrick <RedGrittyBrick@SpamWeary.foo>
Subject: Re: Installing CPAN modules in production system
Message-Id: <489afdeb$0$2520$da0feed9@news.zen.co.uk>

Yogi wrote:
> Hi All,
> 
> I am a Perl developer writing some programs to read mails using IMAP/
> POP3 modules (and some other modules as well).  I have installed these
> modules in our development machine (after trying hard).  Now we have
> to ship this product to our client.  My question is, how to make
> depedent modules available in production envt?  Merely copying of
> these modules to @INC location will work or sysadmin will have to use
> CPAN interface to install these modules?
> 
> My worry is our client might not be willing to install something
> directly to production system (Linux in this case).  Any help as how
> these kind of scenarios are dealt?
> 

If you can install a Perl script, you can probably install most perl 
modules in any subdirectory you have write access to, without needing 
privileged/root/administrator access.

There's probably an answer in perlfaq*. I can't find it, but this is close:

perldoc -q "add a directory to my include path"

-- 
RGB


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

Date: Thu, 7 Aug 2008 07:44:57 -0700 (PDT)
From: Yogi <yogeshkagrawal@gmail.com>
Subject: Re: Installing CPAN modules in production system
Message-Id: <bfa17f4f-b9d1-4826-baa4-2975a09a48c3@m36g2000hse.googlegroups.com>

On Aug 7, 6:51=A0pm, RedGrittyBrick <RedGrittyBr...@SpamWeary.foo>
wrote:
> Yogi wrote:
> > Hi All,
>
> > I am a Perl developer writing some programs to read mails using IMAP/
> > POP3 modules (and some other modules as well). =A0I have installed thes=
e
> > modules in our development machine (after trying hard). =A0Now we have
> > to ship this product to our client. =A0My question is, how to make
> > depedent modules available in production envt? =A0Merely copying of
> > these modules to @INC location will work or sysadmin will have to use
> > CPAN interface to install these modules?
>
> > My worry is our client might not be willing to install something
> > directly to production system (Linux in this case). =A0Any help as how
> > these kind of scenarios are dealt?
>
> If you can install a Perl script, you can probably install most perl
> modules in any subdirectory you have write access to, without needing
> privileged/root/administrator access.
>
> There's probably an answer in perlfaq*. I can't find it, but this is clos=
e:
>
> perldoc -q "add a directory to my include path"
>
> --
> RGB

Yeah.  I was too thinking about PAR to ship required modules (I
checked for document for win32 envt) but was little worried as that
document was talking something about DLLs. As RBG pointed out, I might
use PERL5LIB envt variable to include other lib directories.  Pl
correct me if I am wrong.

Thanks again.


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

Date: Thu, 7 Aug 2008 15:13:06 -0700 (PDT)
From: Zhiliang Hu <zhilianghu@gmail.com>
Subject: Re: make pipes hot (was: a weird problem)
Message-Id: <04124b80-c2e0-4f11-baa0-7dceff069282@w1g2000prk.googlegroups.com>

Many thanks to all for replies, which opened something new to me.

Best regards,

Zhiliang


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

Date: 07 Aug 2008 17:59:27 GMT
From: xhoster@gmail.com
Subject: Re: Need help with perlxs and C strings
Message-Id: <20080807135929.707$sP@newsreader.com>

Thomas <th@example.invalid> wrote:

> > Before discovering the terrifying simplicity of Inline::C, I followed
> > the examples given in, ah, some tutorial or another that involved h2xs.
> > It worked to get me a working example.  If it didn't work for you, what
> > can I say?  You haven't given us enough details.  If the docs are
> > wrong, we might be able to fix them, but not if you hide the errors
> > behind "X" and "Y".
>
> I want to interface with existing C code which is not supposed to be
> copied into the Perl code.

With Inline::C, you can #include C files without copying them into the Perl
code.  You can't call functions defined in those #includes directly from
Perl (as far as I know) but you can write Inline::C stub subs that call
those external functions.  That is the way I do it, easier than learning XS
when I don't need all the complexities and flexibilities that XS
introduces.




> But you're right that I wasn't specific
> enough. Here's what I have so far. I just want to get that small example
> running (the set_data code will do something more complicated, but
> that's not the point here):
>
 ....
>
> == typemap
> char* T_PV
> ==
>
 ...

> xsubpp setdata.xs > setdata.xsc
>
> I get the following output:
> ==
> Error: No INPUT definition for type 'char *', typekind 'T_PV' found in
> setdata.xs, line 10
> Error: No OUTPUT definition for type 'char *', typekind 'T_PV' found in
> setdata.xs, line 12

A general typemap has three section, but you only have one in yours.

From the typemap, it knows to translate char * to T_PV, but it doesn't
know how to handle a T_PV.  If I take the system typemap, and eliminate
everything not related to T_PV, I get the below typemap, with which
xsubpp can run:

==typemap
# basic C types
char *                  T_PV

##########################################
INPUT
T_PV
        $var = ($type)SvPV_nolen($arg)
##########################################
OUTPUT
T_PV
        sv_setpv((SV*)$arg, $var);
==

But it seems like it would be best to just tell xsubpp to use the system
typemap in the first place, instead of creating your own which adds nothing
new to the system one.


> If it works, what would I have to do with the resulting .xsc file?

I don't know.  Who's instructions were you following to get to this
point in the first place?  perlxstut doesn't refer to xsc files.

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, 7 Aug 2008 19:09:50 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Need help with perlxs and C strings
Message-Id: <ecqrm5-jb4.ln1@osiris.mauzo.dyndns.org>


Quoth xhoster@gmail.com:
> Thomas <th@example.invalid> wrote:
> 
> > I want to interface with existing C code which is not supposed to be
> > copied into the Perl code.
> 
> With Inline::C, you can #include C files without copying them into the Perl
> code.  You can't call functions defined in those #includes directly from
> Perl (as far as I know) but you can write Inline::C stub subs that call
> those external functions.  That is the way I do it, easier than learning XS
> when I don't need all the complexities and flexibilities that XS
> introduces.

See also the AUTOWRAP option. (I really should see if I can't get FFI.pm
to work properly: it's *such* a better way to connect to C libs.)

> > xsubpp setdata.xs > setdata.xsc
<snip>
> > If it works, what would I have to do with the resulting .xsc file?
> 
> I don't know.  Who's instructions were you following to get to this
> point in the first place?  perlxstut doesn't refer to xsc files.

The Makefile generated by EU::MM does something like

    xsubpp Foo.xs > Foo.xsc && mv Foo.xsc Foo.c
    cc Foo.c -o Foo.o

so the correct answer is: don't invoke xsubpp directly, use
ExtUtils::MakeMaker, or Module::Install, or Module::Build.

Ben

-- 
  Joy and Woe are woven fine,
  A Clothing for the Soul divine       William Blake
  Under every grief and pine          'Auguries of Innocence'
  Runs a joy with silken twine.                                ben@morrow.me.uk


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

Date: 07 Aug 2008 18:59:05 GMT
From: xhoster@gmail.com
Subject: Re: Need help with perlxs and C strings
Message-Id: <20080807145907.886$xv@newsreader.com>

Ben Morrow <ben@morrow.me.uk> wrote:
> Quoth xhoster@gmail.com:
> > Thomas <th@example.invalid> wrote:
> >
> > > I want to interface with existing C code which is not supposed to be
> > > copied into the Perl code.
> >
> > With Inline::C, you can #include C files without copying them into the
> > Perl code.  You can't call functions defined in those #includes
> > directly from Perl (as far as I know) but you can write Inline::C stub
> > subs that call those external functions.  That is the way I do it,
> > easier than learning XS when I don't need all the complexities and
> > flexibilities that XS introduces.
>
> See also the AUTOWRAP option.

Yep, I was just exploring that option.  In my first attempt I had to copy
the .h file into the __C__ section.  But then I got it to "source" the .h
directly:

use Inline 'C' => qq{#include "setdata.c"\n} . `cat setdata.h`
               => ENABLE => 'AUTOWRAP';

And it works fine.

Other than the fact that the OP is treating Perl strings as if they were C
strings.  \000 is not a string terminator in Perl, and Perl stores the
length of its strings rather than computing on the fly.  When you pass an
SV as if it were a char *, you lose the ability to change those things.  I
don't know of any automatic fix for people not knowing what they are doing.
That's why XS is a pain and Inline::C is terrifying.


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, 7 Aug 2008 18:57:00 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: nested strings
Message-Id: <slrng9maau.sso.hjp-usenet2@hrunkner.hjp.at>

On 2008-08-07 12:59, Ted Zlatanov <tzz@lifelogs.com> wrote:
> On Thu, 7 Aug 2008 11:54:23 +0200 "Peter J. Holzer" <hjp-usenet2@hjp.at> wrote: 
>
> PJH> On 2008-08-06 16:23, Ted Zlatanov <tzz@lifelogs.com> wrote:
>>> On Tue, 5 Aug 2008 14:51:27 -0700 (PDT) sausenet@gmail.com wrote: 
> s> Given: a list of words sorted first in length order, then
> s> alphabetically:
> PJH> [...]
> s> Desired output: the same set of words showing nested substrings
> s> FORMATION [DEFORMATION [DEFORMATIONAL] INFORMATION [INFORMATIONAL]
[...]
>>> 
>>> You probably want a trie.
>
> PJH> Isn't a trie defined as a tree where the key of the parent node is a
> PJH> prefix of the keys of its children? Here it's an infix.
>
> The OP said it was OK to have just prefix matching, though infix
> matching would be nice.

You mean "as a compromise, sacrificing the subnesting would be fine"? I
understood that to mean that flattening the tree to just two levels
would be acceptable, not that prefix matching would be ok.


> You can also just iterate over the substrings of the target, e.g. for
> INFORMATION you check for I-N-F-O-R-M-A-T-I-O-N and then
> N-F-O-R-M-A-T-I-O-N and then F-O-R-M-A-T-I-O-N, etc. in the trie.

It would be quite inefficient, though, since you have to search the trie
for a lot of substrings. I don't see a reason to do that, given that the
correct solution is so easy (see my answer to Ben).

	hp


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

Date: Thu, 7 Aug 2008 17:49:36 -0700 (PDT)
From: Matt <mattj.morrison@gmail.com>
Subject: OO Perl
Message-Id: <c5c123fe-7f92-4c9d-9ea4-7e24066bfdae@d1g2000hsg.googlegroups.com>

Hello everyone,
    I've been using perl for the last 5 or 6 years and in the past 2
years I've been doing things in a much more object oriented style.
I'm just wondering how many people out there in the Perl community are
using Perl as an OO solution rather than a different OO language, like
Ruby.  I know Ruby's popularity has really taken off in the past few
years with the growth of Rails.  I'm curious to see if people are
opting to use a language like Ruby with Rails over using Perl....since
Perl's OOness is a bit...forced.

Just wondering.  Also, over the years I've developed sort of my own
Perl OO style, if anyone is interested in using some of the Perl OO
helper methods I've developed (or reviewing/critiquing )....let me
know.

Thanks!
Matt


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

Date: Thu, 7 Aug 2008 17:59:56 +0200 (CEST)
From: Overt <nowhere@nowhere.com>
Subject: Trying to understand a constructor with an array.
Message-Id: <g7f65r$snk$1@aioe.org>

Don't anybody laugh.  I am trying to learn Object Perl and modifying
examples in the books just to understand what is going on.

Here is a simple object creation where I am using an array rather than a
hash.  

Called with this -
my @tarray;
	$tarray[0][0] = 1;
	$tarray[0][1] = 2;
	$tarray[1][0] = 3;
	$tarray[1][1] = 4;
my $displayobj = Display->new(\@tarray);

Here is the constructor -
sub new {
 	my $class = shift;
	my $self = [];
	$self->[@_] = shift;
  	bless($self, $class);
	$DB::single = 1;   #this is where I examine $self
  	return $self;
}

It works ok, but $self contains an array of an array, rather than just a 2
dimentional array.  Like so 
$self[0][0][0] = [1]
$self[0][0][1] = [2]
$self[0][1][0] = [3]
$self[0][1][1] = [4]

Why an array of an array, rather than just an array?

Again, this isn't a problem I am trying to fix, just something I am trying
to understand.

Thanx anybody
Overt


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

Date: 07 Aug 2008 16:34:11 GMT
From: Leon Timmermans <fawaka@gmail.com>
Subject: Re: Trying to understand a constructor with an array.
Message-Id: <489b2403$0$49844$e4fe514c@news.xs4all.nl>

On Thu, 07 Aug 2008 17:59:56 +0200, Overt wrote:

> Don't anybody laugh.  I am trying to learn Object Perl and modifying
> examples in the books just to understand what is going on.
> 
> Here is a simple object creation where I am using an array rather than a
> hash.
> 
> Called with this -
> my @tarray;
> 	$tarray[0][0] = 1;
> 	$tarray[0][1] = 2;
> 	$tarray[1][0] = 3;
> 	$tarray[1][1] = 4;
> my $displayobj = Display->new(\@tarray);
> 

How about

my $displayobj = Display->new([ [1, 2], [3, 4] ]);

> 	$self->[@_] = shift;

This line doesn't make sense to me at all.
 
> It works ok, but $self contains an array of an array, rather than just a
> 2 dimentional array.  Like so
> $self[0][0][0] = [1]
> $self[0][0][1] = [2]
> $self[0][1][0] = [3]
> $self[0][1][1] = [4]
> 
> Why an array of an array, rather than just an array?
> 

I think what you want to do is:

my $self = [ @{+shift} ];

Regards,

Leon Timmermans


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

Date: 07 Aug 2008 18:25:02 GMT
From: xhoster@gmail.com
Subject: Re: Trying to understand a constructor with an array.
Message-Id: <20080807142504.361$Z9@newsreader.com>

Overt <nowhere@nowhere.com> wrote:
> sub new {
>         my $class = shift;
>         my $self = [];
>         $self->[@_] = shift;

>
> It works ok, but $self contains an array of an array, rather than just a
> 2 dimentional array.  Like so
> $self[0][0][0] = [1]
> $self[0][0][1] = [2]
> $self[0][1][0] = [3]
> $self[0][1][1] = [4]

Surely not.  $self->[0][0][0]=1;

>
> Why an array of an array, rather than just an array?

new is called with @_ containing two things.  Both of those things
get shifted off, leaving @_ with zero things.  Array subscripts,
like the @_ in "$self->[@_]", are evaluated in scalar context, and in a
scalar context arrays yield the number of elements, which is zero.  So your
code is equivalent to:

$self->[0] = shift;

(The reason you get 0 rather than 1 is that the right hand side of the
assignment is evaluated first, so by the time @_ is evaluated in scalar
context on the left hand side, it has already been shifted.)

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, 7 Aug 2008 20:36:28 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Trying to understand a constructor with an array.
Message-Id: <slrng9mg5c.sso.hjp-usenet2@hrunkner.hjp.at>

On 2008-08-07 16:34, Leon Timmermans <fawaka@gmail.com> wrote:
> On Thu, 07 Aug 2008 17:59:56 +0200, Overt wrote:
>
>> Here is a simple object creation where I am using an array rather than a
>> hash.
>> 
>> Called with this -
>> my @tarray;
>> 	$tarray[0][0] = 1;
>> 	$tarray[0][1] = 2;
>> 	$tarray[1][0] = 3;
>> 	$tarray[1][1] = 4;
>> my $displayobj = Display->new(\@tarray);
>> 
>
> How about
>
> my $displayobj = Display->new([ [1, 2], [3, 4] ]);
>
>> 	$self->[@_] = shift;
>
> This line doesn't make sense to me at all.

This is tricky: shift modifies @_, so is the length of @_, which is used
as an index, taken before or after the shift? We know that the length is
1 before and 0 after the shift, so that might be equivalent to

    $self->[0] = shift;

or to

    $self->[1] = shift;

or it might even be completely undefined. Apparently, the shift is
evaluated first, so it's equivalent to $self->[0] = shift; But if the OP
meant that he should have written it instead of relying on an
unspecified evaluation order, and anyway I doubt that he meant it. But I
wonder what the thought that this would do.


>> It works ok, but $self contains an array of an array, rather than just a
>> 2 dimentional array.  Like so
>> $self[0][0][0] = [1]
>> $self[0][0][1] = [2]
>> $self[0][1][0] = [3]
>> $self[0][1][1] = [4]
>> 
>> Why an array of an array, rather than just an array?
>> 
>
> I think what you want to do is:
>
> my $self = [ @{+shift} ];

Or

    $self = shift;

which is subtly different.

	hp


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

Date: 07 Aug 2008 19:23:07 GMT
From: Leon Timmermans <fawaka@gmail.com>
Subject: Re: Trying to understand a constructor with an array.
Message-Id: <489b4b9b$0$49844$e4fe514c@news.xs4all.nl>

On Thu, 07 Aug 2008 20:36:28 +0200, Peter J. Holzer wrote:

> On 2008-08-07 16:34, Leon Timmermans <fawaka@gmail.com> wrote:
> But I wonder what the thought that this would do.

Yeah, that's what I meant.

> Or
> 
>     $self = shift;
> 
> which is subtly different.
> 

Yeah, but personally I'd by wary of blessing anything I didn't create 
myself, that can bite one in unexpected ways.

Leon Timmermans


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

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


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