[29053] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 297 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Apr 3 16:14:32 2007

Date: Tue, 3 Apr 2007 13:14:21 -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           Tue, 3 Apr 2007     Volume: 11 Number: 297

Today's topics:
        Internalisation support and dictionaries (Broke)
    Re: Internalisation support and dictionaries anno4000@radom.zrz.tu-berlin.de
    Re: Internalisation support and dictionaries (Broke)
    Re: Internalisation support and dictionaries (Broke)
    Re: Internalisation support and dictionaries <paduille.4060.mumia.w+nospam@earthlink.net>
    Re: Isolate lines in a text file and perform replacemen <paduille.4060.mumia.w+nospam@earthlink.net>
        parsing text file into array <hamanjam@hotmail.com>
    Re: parsing text file into array <purlgurl@purlgurl.net>
    Re: parsing text file into array <someone@example.com>
    Re: parsing text file into array usenet@DavidFilmer.com
    Re: parsing text file into array <wahab-mail@gmx.de>
        perl OOP <a@mail.com>
    Re: perl OOP (Marc Espie)
    Re: perl OOP <bik.mido@tiscalinet.it>
    Re: perl OOP <please@nospam.net>
    Re: perl OOP <uri@stemsystems.com>
    Re: perl OOP <abigail@abigail.be>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Tue, 3 Apr 2007 09:16:21 +0200
From: nospam@tele2.fr (Broke)
Subject: Internalisation support and dictionaries
Message-Id: <1hvzl61.ocq5ouzs8ax3N%nospam@tele2.fr>

Hello,

I am a beginner so please be indulgent.
I wanted to make a sort of dictionary given a text french file.
So I wrote the following script.
Everything is OK but the ordered list comes in US ASCII encoding.
How to make it work for accented letters?
Any help will be appreciated.
Here is my humble script.
=======
#!/usr/bin/perl -w
use warnings;
local $/;
use locale;
use utf8;
$file = '/Users/Broke/Desktop/data.txt';
open (IN, $file) or die "$file not found\n : $!\n";
@data = ();
%seen = ();
while (<IN>) {
foreach $word (m/(\b.+?\b)/gi) {
unless ($seen{$word}) {
$seen{$word} = 1;
    push(@data, $word);
}
}
}
close (IN) or die "Can't close $file : $!\n";
@data = sort(@data);
@data = map $_ . "\n", @data;
open (OUT, ">/Users/Broke/Desktop/out.txt") or die "Can't create\n :
$!\n";
select (OUT);
print @data;
close (OUT);
========
B.


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

Date: 3 Apr 2007 09:00:17 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: Internalisation support and dictionaries
Message-Id: <57ejd1F2cojqvU1@mid.dfncis.de>

Broke <nospam@tele2.fr> wrote in comp.lang.perl.misc:
> Hello,
> 
> I am a beginner so please be indulgent.
> I wanted to make a sort of dictionary given a text french file.
> So I wrote the following script.
> Everything is OK but the ordered list comes in US ASCII encoding.
> How to make it work for accented letters?

Use the locale pragma.  See perldoc perllocale for a general description
and perldoc locale for specifics.

Anno


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

Date: Tue, 3 Apr 2007 19:18:49 +0200
From: nospam@tele2.fr (Broke)
Subject: Re: Internalisation support and dictionaries
Message-Id: <1hw0cu5.1yl7gt27r5xbnN%nospam@tele2.fr>

Michele Dondi <bik.mido@tiscalinet.it> wrote:

Many thanks to you Michele for your help.
Thank you also for pointing out that the
dot will capture also space. That's true.
it's better written with the \w+ or
the [[:alnum:]]+
instead of the dot.
Thank you also for the other hints.

Please don't forget that my problem is that
I want to extract french words with
diacritics and that I get only
words without diacritics amongs the other
possible words that would like to extract.

As Anno points out this is the problem
of the locale pragma.

I will reinstall the operating system.
It seems that I forgot that I choosed
US languge as my defaut language when
installing the operating system.
Very fortunately with "Apple" I am not
forced to reformat.

Many thanks again and have a nice day!
--
B.


> On Tue, 3 Apr 2007 09:16:21 +0200, nospam@tele2.fr (Broke) wrote:
> 
> >#!/usr/bin/perl -w
> >use warnings;
> 
> either
> 
>   -w
> 
> or
> 
>   use warnings;  # and the latter is better!
> 
> and
>   
>   use strict;  # as well!
> 
> >local $/;
> >use locale;
> >use utf8;
> >$file = '/Users/Broke/Desktop/data.txt';
> 
>   my $file = ...  # the same for all other variables.
> 
> >open (IN, $file) or die "$file not found\n : $!\n";
> 
>   open my $in, '<', $file or die "$file not found\n : $!\n";
> 
> >@data = ();
> >%seen = ();
> 
>   my (@data, %seen);
> 
> (If not under strict.pm, you don't need that at all.)
> 
> >while (<IN>) {
> >foreach $word (m/(\b.+?\b)/gi) {
> 
> Are you aware that this will capture whitespace too?
> 
> >unless ($seen{$word}) {
> >$seen{$word} = 1;
> >    push(@data, $word);
> 
>   push @data, $word unless $seen{$word}++;
> 
> Or even
> 
>   !$seen{$_}++ and push @data, $_ for /\b.+?\b/g;
> 
> >}
> >}
> >}
> >close (IN) or die "Can't close $file : $!\n";
> >@data = sort(@data);
> >@data = map $_ . "\n", @data;
> 
> You can use $, and $\;
> 
> 
> Michele


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

Date: Tue, 3 Apr 2007 19:18:50 +0200
From: nospam@tele2.fr (Broke)
Subject: Re: Internalisation support and dictionaries
Message-Id: <1hw0dkt.1ckl6tcm12q2rN%nospam@tele2.fr>

Many thanks to you Anno.
You said the truth.
I will investigate this problem.
Thanks again!
-
B.
<anno4000@radom.zrz.tu-berlin.de> wrote:

> Broke <nospam@tele2.fr> wrote in comp.lang.perl.misc:
> > Hello,
> > 
> > I am a beginner so please be indulgent.
> > I wanted to make a sort of dictionary given a text french file.
> > So I wrote the following script.
> > Everything is OK but the ordered list comes in US ASCII encoding.
> > How to make it work for accented letters?
> 
> Use the locale pragma.  See perldoc perllocale for a general description
> and perldoc locale for specifics.
> 
> Anno


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

Date: Tue, 03 Apr 2007 18:10:05 GMT
From: "Mumia W." <paduille.4060.mumia.w+nospam@earthlink.net>
Subject: Re: Internalisation support and dictionaries
Message-Id: <1MwQh.17887$PL.4056@newsread4.news.pas.earthlink.net>

On 04/03/2007 02:16 AM, Broke wrote:
> Hello,
> 
> I am a beginner so please be indulgent.
> I wanted to make a sort of dictionary given a text french file.
> So I wrote the following script.
> Everything is OK but the ordered list comes in US ASCII encoding.
> How to make it work for accented letters?
> Any help will be appreciated.
> Here is my humble script.
> =======
> #!/usr/bin/perl -w
> use warnings;
> local $/;
> use locale;
> use utf8;
> $file = '/Users/Broke/Desktop/data.txt';
> open (IN, $file) or die "$file not found\n : $!\n";
> [...]

You can set an encoding for the 'open' command:

open (IN, '<:utf8', $file) or die (...

Read about the 'open' command and Perl:

Start->Run->"perldoc -f open"
Start->Run->"perldoc perl"






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

Date: Tue, 03 Apr 2007 18:10:07 GMT
From: "Mumia W." <paduille.4060.mumia.w+nospam@earthlink.net>
Subject: Re: Isolate lines in a text file and perform replacements
Message-Id: <3MwQh.17888$PL.16439@newsread4.news.pas.earthlink.net>

On 04/02/2007 09:43 PM, Robert Neville wrote:
> On Fri, 30 Mar 2007 20:53:45 GMT, "Mumia W."
> <paduille.4060.mumia.w+nospam@earthlink.net> wrote:
> 
>> On 03/30/2007 01:16 PM, Robert Neville wrote:
>>> [...] script below performs this task (yet has not been thorough tested). It 
>>> has a major shortcoming that the Perl line works on the entire file 
>>> when it should only replace the lines with mp3 pointers. Here's the 
>>> call for assistance since I am having code block. Maybe, someone could 
>>> help me with different logic or a traditional grep solution.  
>>>
>>> find ./ -regex ".*\(m3u\|sfv\|xml\)$" -type f -print | while read FILE
>>> 	do 
>>> 		while read -r REGEX REPLACE line
>>> 		do
>>> 			CODE="$CODE; s/$REGEX/$REPLACE/g"
>>> 		done < "$PRESET"
>>> 		echo "$CODE"
>>> 		echo "perl -pi.bak -e "s/$REGEX/$REPLACE/g" $FILE"
>> Is this what you're looking for?
>>
>> echo "perl -pi.bak -e \"s/$REGEX/$REPLACE/g if /\.mp3\b/\" $FILE"
>>
>>
>>> 		#PRESET may contain over twenty five regex patterns
>>> done
>>>
>>> btw I am doing this script in Bash, because Perl is foreign territory.
> Thanks Mumia
> 
> This approach looks like a strong possibility. How would I search for
> more information on this construct? The perl man page is extensive and
> the "if" keyword is too general. Please let me know if you have links
> or examples where this construct has been used. Here's the pseudo code
> from your suggestion. 
> 
> 
> find ./ -regex ".*\(m3u\|sfv\|xml\)$" -type f -print | while read FILE
>   do 
>     while read -r REGEX REPLACE line
>       do
>         CODE="$CODE; s/$REGEX/$REPLACE/g"
>       done < "$PRESET"
>       #PRESET may contain over twenty five regex patterns
>       perl -pi.bak -e \""$CODE"  if /\.mp3\b/\" "$FILE"
> done

The information is located in several places. Perlretut talks about 
"\b". /Somewhere/ in the documentation the need to backslash 
double-quotes within double-quoted strings is discussed; however, that 
is not needed for the "perl" command above.

I would write it like so:

perl -pi.bak -e "$CODE if /\.mp3\b/" "$FILE"





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

Date: 3 Apr 2007 09:33:16 -0700
From: "Hamanjam" <hamanjam@hotmail.com>
Subject: parsing text file into array
Message-Id: <1175617996.842129.213430@n76g2000hsh.googlegroups.com>

Hello all.  I have a file that has multiple lines set up in blocks
that I need to parse out into a file.  Is there an easy to get a file
like the one below parsed out.  I only need the Slot Address and
Volume Tag info.  I would RTFM, but I don't know which to read or how
to decipher what I want to do.  I also posted this in the "awk" forum
because I don't know which would be easier:

SlotAddress 4096
SlotState.....................Normal
ASC/ASCQ.......................0000
MediaPresent..................Yes
RobotAccessAllowed...........Yes
SourceElementAddress.........4096
MediaInverted.................No
VolumeTag.....................DET028L3


SlotAddress 4097
SlotState.....................Normal
ASC/ASCQ.......................0000
MediaPresent..................Yes
RobotAccessAllowed...........Yes
SourceElementAddress.........4097
MediaInverted.................No
VolumeTag.....................DET029L3


Thanks in advance.
Jim



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

Date: Tue, 03 Apr 2007 09:57:29 -0700
From: Purl Gurl <purlgurl@purlgurl.net>
Subject: Re: parsing text file into array
Message-Id: <_a6dna1Dn7zqGo_bnZ2dnUVZ_hCdnZ2d@giganews.com>

Hamanjam wrote:

(snipped)

> that I need to parse out into a file.  Is there an easy to get a file
> like the one below parsed out.  I only need the Slot Address and
> Volume Tag info.

> SlotAddress 4096
> SlotState.....................Normal
> ASC/ASCQ.......................0000
> MediaPresent..................Yes
> RobotAccessAllowed...........Yes
> SourceElementAddress.........4096
> MediaInverted.................No
> VolumeTag.....................DET028L3

(snipped remaining data)


#!perl

{
  local ($/) = "\n\n\n";

  while (<DATA>)
   {
    $_ =~ tr/\n//d;
    push (@Array, substr ($_, 12, 4));
    push (@Array, substr ($_, -8));
   }
}

for (@Array)
  { print "$_\n"; }

__DATA__
SlotAddress 4096
SlotState.....................Normal
ASC/ASCQ.......................0000
MediaPresent..................Yes
RobotAccessAllowed...........Yes
SourceElementAddress.........4096
MediaInverted.................No
VolumeTag.....................DET028L3


SlotAddress 4097
SlotState.....................Normal
ASC/ASCQ.......................0000
MediaPresent..................Yes
RobotAccessAllowed...........Yes
SourceElementAddress.........4097
MediaInverted.................No
VolumeTag.....................DET029L3


PRINTED RESULTS:

4096
DET028L3
4097
DET029L3


Purl Gurl


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

Date: Tue, 03 Apr 2007 17:19:26 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: parsing text file into array
Message-Id: <y0wQh.33468$x9.8568@edtnps89>

Hamanjam wrote:
> Hello all.  I have a file that has multiple lines set up in blocks
> that I need to parse out into a file.  Is there an easy to get a file
> like the one below parsed out.  I only need the Slot Address and
> Volume Tag info.  I would RTFM, but I don't know which to read or how
> to decipher what I want to do.  I also posted this in the "awk" forum
> because I don't know which would be easier:
> 
> SlotAddress 4096
> SlotState.....................Normal
> ASC/ASCQ.......................0000
> MediaPresent..................Yes
> RobotAccessAllowed...........Yes
> SourceElementAddress.........4096
> MediaInverted.................No
> VolumeTag.....................DET028L3
> 
> 
> SlotAddress 4097
> SlotState.....................Normal
> ASC/ASCQ.......................0000
> MediaPresent..................Yes
> RobotAccessAllowed...........Yes
> SourceElementAddress.........4097
> MediaInverted.................No
> VolumeTag.....................DET029L3


$ echo "SlotAddress 4096
SlotState.....................Normal
ASC/ASCQ.......................0000
MediaPresent..................Yes
RobotAccessAllowed...........Yes
SourceElementAddress.........4096
MediaInverted.................No
VolumeTag.....................DET028L3


SlotAddress 4097
SlotState.....................Normal
ASC/ASCQ.......................0000
MediaPresent..................Yes
RobotAccessAllowed...........Yes
SourceElementAddress.........4097
MediaInverted.................No
VolumeTag.....................DET029L3" | \
perl -ln00e'print for /SlotAddress\W+(\d+)/, /VolumeTag\W+(\w+)/'
4096
DET028L3
4097
DET029L3




John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall


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

Date: 3 Apr 2007 10:25:08 -0700
From: usenet@DavidFilmer.com
Subject: Re: parsing text file into array
Message-Id: <1175621108.200070.312350@d57g2000hsg.googlegroups.com>

On Apr 3, 9:33 am, "Hamanjam" <haman...@hotmail.com> wrote:
> Hello all.  I have a file that has multiple lines set up in blocks
> that I need to parse out into a file.  Is there an easy to get a file
> like the one below parsed out.

You don't say what you want your output to look like.  If you want one
line per "section", tab separated, you could do something like this:

#!/usr/bin/perl
   use strict ;
   use warnings ;

   while (<DATA>) {
      print "$1\t" if /^SlotAddress\w+(\d*)/;
      print "$1\n" if /^VolumeTag\.*(\w*)/;
   }

__DATA__
SlotAddress 4096
SlotState.....................Normal
ASC/ASCQ.......................0000
MediaPresent..................Yes
RobotAccessAllowed...........Yes
SourceElementAddress.........4096
MediaInverted.................No
VolumeTag.....................DET028L3

SlotAddress 4097
SlotState.....................Normal
ASC/ASCQ.......................0000
MediaPresent..................Yes
RobotAccessAllowed...........Yes
SourceElementAddress.........4097
MediaInverted.................No
VolumeTag.....................DET029L3

Result:
4096    DET028L3
4097    DET029L3


--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)




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

Date: Tue, 03 Apr 2007 19:25:28 +0200
From: Mirco Wahab <wahab-mail@gmx.de>
Subject: Re: parsing text file into array
Message-Id: <euu5k3$5hd$1@mlucom4.urz.uni-halle.de>

Hamanjam wrote:
> Hello all.  I have a file that has multiple lines set up in blocks
> that I need to parse out into a file.  Is there an easy to get a file
> like the one below parsed out.  I only need the Slot Address and
> Volume Tag info.  I would RTFM, but I don't know which to read or how
> to decipher what I want to do.  I also posted this in the "awk" forum
> because I don't know which would be easier:

What do you mean with 'parse into array' (subject)
and 'parse into file' (here)?

> SlotAddress 4096
> SlotState.....................Normal
> ASC/ASCQ.......................0000
> MediaPresent..................Yes
> RobotAccessAllowed...........Yes
> SourceElementAddress.........4096
> MediaInverted.................No
> VolumeTag.....................DET028L3
> 
> 
> SlotAddress 4097
> SlotState.....................Normal
> ASC/ASCQ.......................0000
> MediaPresent..................Yes
> RobotAccessAllowed...........Yes
> SourceElementAddress.........4097
> MediaInverted.................No
> VolumeTag.....................DET029L3

if it's just the cutting out the inner lines, do:

  perl -0777pe 's/(?<=\w\n)^.+?\n(?=\w)//gm && s/\.+|\n\n/ /g' hamanjam.txt > new.txt

 ... if your data is in hamanjam.txt and
the 'parsed out' stuff gous into 'new.txt'.

But probably you meant something else.

Regards

M.













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

Date: Tue, 03 Apr 2007 09:23:56 GMT
From: "a" <a@mail.com>
Subject: perl OOP
Message-Id: <M2pQh.20836$aG1.15750@pd7urf3no>

Hi
I dont understand when we declare a new in a package. It is always use
new($class, %args).
For method, ususally method($self, %args) to pass input parameter. So, we
dont use the $class and $self anyway, what is the purpose putting there?

I have read the tutorials already but still not understand.

Thanks




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

Date: Tue, 3 Apr 2007 10:01:08 +0000 (UTC)
From: espie@lain.home (Marc Espie)
Subject: Re: perl OOP
Message-Id: <eut8l4$1goo$1@biggoron.nerim.net>

In article <M2pQh.20836$aG1.15750@pd7urf3no>, a <a@mail.com> wrote:
>Hi
>I dont understand when we declare a new in a package. It is always use
>new($class, %args).

A typical new will create an object at some point, like

my $self = bless {}, $class;
 ...
return $self;

In the presence of inheritance, you don't necessarily know what the class
value is, because you might inherit your constructor unchanged from a base
class.

>For method, ususally method($self, %args) to pass input parameter. So, we
>dont use the $class and $self anyway, what is the purpose putting there?

The object very often has state, and you use that state in your methods.
One methods might set $self->{p} = value; and another one will use
$self->{p}.

>I have read the tutorials already but still not understand.

You have to stop thinking in procedural terms and reorganise what you're
doing in terms of `objects', or you will never understand.

Try to find actual code that's already using objects correctly, try to redo
what it does with your usual techniques, and notice the difference.

OOP is deceptively simple: indeed, it will look like `what's the point' at
first, until you start writing simpler and clearer code thanks to it.


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

Date: Tue, 03 Apr 2007 16:08:10 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: perl OOP
Message-Id: <fln413ptc876onu1c11m74bvgsdghtooel@4ax.com>

On Tue, 03 Apr 2007 09:23:56 GMT, "a" <a@mail.com> wrote:

>Subject: perl OOP

And the question is? (Bottom line is: put the subject of your post in
the Subject!)

>I dont understand when we declare a new in a package. It is always use

Typically, whenever you want, but once only. And BTW: you do know that
C<new> is not a reserved word, don't you?

>new($class, %args).

Huh?!? I can't understand if you're referring to the *definition* of a
constructor or to its use. In both cases, that $class first parameter
seems strange.

>For method, ususally method($self, %args) to pass input parameter. So, we

Really?

>dont use the $class and $self anyway, what is the purpose putting there?

I don't know. You *generally* do *not* put them there when calling
*methods*. They're passed implicitly: it's a bit of syntactical sugar.

>I have read the tutorials already but still not understand.

It seems so. However, with due respect, it's hard even to tell *what*
you do not understand...


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: Tue, 03 Apr 2007 10:16:24 -0700
From: Xiong Changnian <please@nospam.net>
Subject: Re: perl OOP
Message-Id: <please-9A3ECC.10162403042007@free.teranews.com>

In article <M2pQh.20836$aG1.15750@pd7urf3no>, "a" <a@mail.com> wrote:

> I dont understand when we declare a new in a package. It is always use
> new($class, %args).
> For method, ususally method($self, %args) to pass input parameter. So, we
> dont use the $class and $self anyway, what is the purpose putting there?

Sorry, but I think we don't really understand what you're asking. If 
you're having trouble expressing yourself, you might want to find 
someone fluent in both English and your native language to translate for 
you. 

I'll take a stab at it. Please note that the following examples are 
extremely verbose -- plain but not particularly efficient. 

Most methods are object methods. They are expect params of the form: 

    package Bottle; 

    sub name_me {
        my $self = shift; 
        my $name = shift; 

        $self->{NAME} = $name;
        return 1;
    };

Object methods are called via: 

    $object-> name_me ($newname);

 ... and Perl considerately passes $object as the first param in @_, 
later to be shifted into $self. Note that the return value can be thrown 
away. The method acts on the $object passed in. 

BUT, new -- an object constructor -- is, by definition, used to make a 
new object. So, an object of the desired class may not be available yet. 
So, new is generally a class method, expecting different params: 

    package Bottle; 

    sub new {
        my $class = shift; 
        my $name = shift; 

        my $self = {};
        $self->{NAME} = $name;
        bless ($self, $class);
        return $self; 
    };

 ... which is called with a completely different syntax, though they may 
look similar: 

    my $object = Bottle->new($name); 

It may be more clear to use this alternate syntax, with the same effect: 

    my $object = new Bottle ($name);

 ... or even: 

    my $object = new Bottle $name;

In any case, the effect is the same. The $class (Bottle) is what is 
passed as the first param in @_, to be used to bless the new object. The 
object is what is returned by the method. 

Two key points of OO technique bear underlining: 

* The $object is (usually) a reference to an anonymous hash. Inside our 
methods above, we may assign this reference to $self but the reference 
still points to a single "thing". With conventional subs, we generally 
pass a param and act on its value only; the original param is untouched. 
The intent of OO technique is to act on the object directly. 

* It all hinges on bless(). The purpose of the new method is to bless 
$self, the reference to the anon hash -- to tell Perl to associate 
$class with the underlying object. Future calls, say, to name_me or any 
other method depend on Perl looking at $object, determining $class, and 
going to get the appropriate method. 

This may or may not answer your question. If not, don't hesitate to try 
again. However, bear in mind my advice about finding a translator. It's 
difficult enough for two native speakers of English, both proficient in 
Perl, to agree on the exact terms used to describe or explain it.
-- 
Xiong Changnian
xiong102ATxuefangDOTcom

-- 
Posted via a free Usenet account from http://www.teranews.com



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

Date: Tue, 03 Apr 2007 13:38:43 -0400
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: perl OOP
Message-Id: <x7vegdnysc.fsf@mail.sysarch.com>

>>>>> "XC" == Xiong Changnian <please@nospam.net> writes:


  XC> Most methods are object methods. They are expect params of the form: 

  XC>     package Bottle; 

  XC>     sub name_me {
  XC>         my $self = shift; 
  XC>         my $name = shift; 

  XC> ... and Perl considerately passes $object as the first param in @_, 
  XC> later to be shifted into $self. Note that the return value can be thrown 
  XC> away. The method acts on the $object passed in. 

  XC>     sub new {
  XC>         my $class = shift; 
  XC>         my $name = shift; 

it is much simpler and more consistant to say that the class or object
is always passed to any method as its first argument. it is the act of
making a method call (class or object doesn't matter) that does this.

  XC> It may be more clear to use this alternate syntax, with the same effect: 

  XC>     my $object = new Bottle ($name);

no, that is considered a bad idea. it is deprecated in the docs and was
even mentioned recently here. i always forget which doc discusses why
indirect method calls are bad. 

  XC> In any case, the effect is the same. The $class (Bottle) is what is 
  XC> passed as the first param in @_, to be used to bless the new object. The 
  XC> object is what is returned by the method. 

if you always use a direct call then the rule is even easier. the part
before the -> is ALWAYS passed as the first argument. it can be either
an object or a class name. simple.

  XC> Two key points of OO technique bear underlining: 

  XC> * The $object is (usually) a reference to an anonymous hash. Inside our 

no. it is always a blessed reference. what reference type is independent
and there are reasons to use types other than hashes. inside-out objects
usually use blessed scalar refs which actually have no data in
them. again this topic has been discussed here many times.

  XC> methods above, we may assign this reference to $self but the reference 
  XC> still points to a single "thing". With conventional subs, we generally 
  XC> pass a param and act on its value only; the original param is untouched. 
  XC> The intent of OO technique is to act on the object directly. 

not if you pass a ref param to a sub. even you discussed this in the
threads about @_, aliasing and pass by ref/value. the real diff is that
objects are always passed as a ref since only refs can be blessed. so in
general method operate on data in the object and can modify it. plain
subs can be passed anything and they may or may not modify its args
depending on the API and such.

  XC> * It all hinges on bless(). The purpose of the new method is to bless 
  XC> $self, the reference to the anon hash -- to tell Perl to associate 
  XC> $class with the underlying object. Future calls, say, to name_me or any 

s/object/class/. blessing a ref makes it know what class it belongs too
and the ref is now an object.

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: 03 Apr 2007 18:17:43 GMT
From: Abigail <abigail@abigail.be>
Subject: Re: perl OOP
Message-Id: <slrnf156i7.s2c.abigail@alexandra.abigail.be>

Xiong Changnian (please@nospam.net) wrote on MMMMCMLXIII September
MCMXCIII in <URL:news:please-9A3ECC.10162403042007@free.teranews.com>:
||  
||  BUT, new -- an object constructor -- is, by definition, used to make a 
||  new object.

No. 'new' doesn't so by definition. On the Perl level, there's nothing
special to 'new'. It's *bless* that makes an object - by definition.
That many people call bless in 'new' is a convention. 



Abigail
-- 
perl  -e '$_ = q *4a75737420616e6f74686572205065726c204861636b65720a*;
          for ($*=******;$**=******;$**=******) {$**=*******s*..*qq}
          print chr 0x$& and q
          qq}*excess********}'


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

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


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