[28220] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 9584 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Aug 9 21:05:38 2006

Date: Wed, 9 Aug 2006 18:05:05 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Wed, 9 Aug 2006     Volume: 10 Number: 9584

Today's topics:
    Re: @_ deprecated? <justin.0607@purestblue.com>
    Re: @_ deprecated? <benmorrow@tiscali.co.uk>
    Re: @_ deprecated? <john@castleamber.com>
    Re: @_ deprecated? <john@castleamber.com>
    Re: Control characters - regex to match/lose these? <justin.0607@purestblue.com>
    Re: Control characters - regex to match/lose these? <benmorrow@tiscali.co.uk>
    Re: Encrypt Windows Password <benmorrow@tiscali.co.uk>
    Re: How to send command line options into test scripts? <mgarrish@gmail.com>
    Re: How to send command line options into test scripts? <DJStunks@gmail.com>
    Re: How to send command line options into test scripts? <yusufm@gmail.com>
        Need help with parsing data <Shani718@gmail.com>
    Re: Need help with parsing data <DJStunks@gmail.com>
    Re: Need help with parsing data <1usa@llenroc.ude.invalid>
    Re: Need help with parsing data <john@castleamber.com>
        OO Perl : Struggling with hash data members in my Class Brett.R.Davis@gmail.com
    Re: OO Perl : Struggling with hash data members in my C <1usa@llenroc.ude.invalid>
    Re: Question about Arrays <No_4@dsl.pipex.com>
    Re: Read socket using both <> and sysread() <benmorrow@tiscali.co.uk>
    Re: Read socket using both <> and sysread() xhoster@gmail.com
    Re: Read socket using both <> and sysread() xhoster@gmail.com
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 09 Aug 2006 22:03:55 -0000
From: Justin C <justin.0607@purestblue.com>
Subject: Re: @_ deprecated?
Message-Id: <slrnedkmu4.46p.justin.0607@moonlight.purestblue.com>

On 2006-08-09, broeisito@gmail.com <broeisito@gmail.com> wrote:
> Hello,
>
> I wrote the following code to read what processor is in a linux box.
> But I get the following error:
>
> Use of implicit split to @_ is deprecated at cpu.pl line 17.
> Can someone possibly explain to me what I'm doing wrong here?

You're not doing anything wrong, it'll work, but in some situations
using split like this will break your code. From perldoc -f split: 

       In scalar context, returns the number of fields found and
       splits into the @_ array.  Use of split in scalar context is
       deprecated, however, because it clobbers your subroutine argu‐
       ments.

You may want to read "perldoc perlsub" in conjunction with the above 
if it's not immediately clear to you why you are getting the warning.

	Justin.

-- 
Justin C, by the sea. 


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

Date: Wed, 9 Aug 2006 22:54:46 +0100
From: Ben Morrow <benmorrow@tiscali.co.uk>
Subject: Re: @_ deprecated?
Message-Id: <662qq3-6kt.ln1@osiris.mauzo.dyndns.org>


Quoth John Bokma <john@castleamber.com>:
> "broeisito@gmail.com" <broeisito@gmail.com> wrote:
> >
> > my $CPU_FILE = "/proc/cpuinfo";
> > 
> > if(-e $CPU_FILE && -r $CPU_FILE && -f $CPU_FILE)
> > {
> >      open CPU, $CPU_FILE;
> > }
> 
> You *don't* check what open returns. Moreover, de rest of your code
> assumes it always works.
> 
> replace the if {} with:
> 
> -f $CPU_FILE or die "'$CPU_FILE' is not a regular file";
> open my $fh, $CPU_FILE or die "Can't open '$CPU_FILE' for reading: $!";

It's better in general to open the file first and then filetest the
filehandle. Then you can be *sure* of what you've got.

In this case, of course, it doesn't matter: all you need is something
you can read. If Linux 3.18 should replace /proc/cpuinfo with a named
pipe, or some other type of magic file, your script should continue to
work. So skip the filetest altogether.

Ben

-- 
        I must not fear. Fear is the mind-killer. I will face my fear and
        I will let it pass through me. When the fear is gone there will be 
        nothing. Only I will remain.
benmorrow@tiscali.co.uk                                   Frank Herbert, 'Dune'


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

Date: 9 Aug 2006 23:06:29 GMT
From: John Bokma <john@castleamber.com>
Subject: Re: @_ deprecated?
Message-Id: <Xns981AB832E1C96castleamber@130.133.1.4>

"broeisito@gmail.com" <broeisito@gmail.com> wrote:

> Guys,
> 
> Thanks a lot for your answers.

When replying, write your reply *under* the part you're replying to and 
remove everything else that is no longer needed in such a way that the 
message you are posting can be read stand alone without too much effort 
(top down, left to right, and it's clear who wrote what).


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

Date: 9 Aug 2006 23:09:25 GMT
From: John Bokma <john@castleamber.com>
Subject: Re: @_ deprecated?
Message-Id: <Xns981AB8B214466castleamber@130.133.1.4>

Ben Morrow <benmorrow@tiscali.co.uk> wrote:

> 
> Quoth John Bokma <john@castleamber.com>:
>> "broeisito@gmail.com" <broeisito@gmail.com> wrote:
>> >
>> > my $CPU_FILE = "/proc/cpuinfo";
>> > 
>> > if(-e $CPU_FILE && -r $CPU_FILE && -f $CPU_FILE)
>> > {
>> >      open CPU, $CPU_FILE;
>> > }
>> 
>> You *don't* check what open returns. Moreover, de rest of your code
>> assumes it always works.
>> 
>> replace the if {} with:
>> 
>> -f $CPU_FILE or die "'$CPU_FILE' is not a regular file";
>> open my $fh, $CPU_FILE or die "Can't open '$CPU_FILE' for reading: $!";
> 
> It's better in general to open the file first and then filetest the
> filehandle. Then you can be *sure* of what you've got.

Thanks, you're right, my order has the risk of a race condition. (I guess 
that's why you suggest to swap them).

> In this case, of course, it doesn't matter: all you need is something
> you can read.

Yeah, I would not do the -f test if this was my own script :-)

-- 
John Bokma          Freelance software developer
                                &
                    Experienced Perl programmer: http://castleamber.com/


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

Date: Wed, 09 Aug 2006 21:43:29 -0000
From: Justin C <justin.0607@purestblue.com>
Subject: Re: Control characters - regex to match/lose these?
Message-Id: <slrnedklnq.46p.justin.0607@moonlight.purestblue.com>

On 2006-08-08, Dr.Ruud <rvtol+news@isolution.nl> wrote:
> Justin C schreef:
>> Dr.Ruud:
>>> Justin C:
>
>>>> Viewed in vim they look like: ^[(s12H  ^[&16D  ^[(16H  ^[&18D
>>>
>>>    s/\e(?:[^@A-Z]*[@A-Z])|[=9]//g
>>
>> Gonna have to learn more about REs to understand this one!
>>
>> I'm reading through perlre, the ?: is doing my head in a bit, may be
>> it's been a long day: ``it groups subexpressions like "()" but doesn't
>> make backreferences'' ... ``This is for clustering, not capturing''
>
> Concentrate on the "clustering, not capturing".
>
> You can just as well do it step by step:
>
>   s/\e[=9]//g ;

Why [=9]? I've read, and re-read, this thread and tried to understand
what you and Ben are trying to explain to me (I'm really not stupid,
honest), but I can't see, given the examples I gave in the OP, why the
[=9].

>   s/\e[^@A-Z]*[@A-Z]//g ;
I also don't understand how the "clustering" use of ?: is enabling these
two lines to be made into one (it maybe that if I understand one part of
this the other falls into place). I'm not seeing what effect the ?: is
having, the one line without the ?: looks, to me, like it'd do the same
as the two.

I'm very grateful for the time you've both spent on this and don't
intend make this difficult but, if you could try one last time, I really
would like to get this.
>
> google: HP escape
That's very interesting. I can see that's something that needs
bookmarking for when I want to take some of the more plain reports and
"tart" them up for printing.
>
> An extensive list:
> http://printers.necsam.com/public/printers/pclcodes/pcl5hp.htm
And that's even more so!

I'm sure I could be quite good at this coding thing if only my job
allowed me to do more of it.


	Justin.

-- 
Justin C, by the sea. 


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

Date: Thu, 10 Aug 2006 00:01:03 +0100
From: Ben Morrow <benmorrow@tiscali.co.uk>
Subject: Re: Control characters - regex to match/lose these?
Message-Id: <f26qq3-t7u.ln1@osiris.mauzo.dyndns.org>


Quoth justin.news@purestblue.com:
> On 2006-08-08, Dr.Ruud <rvtol+news@isolution.nl> wrote:
> > Justin C schreef:
> >> Dr.Ruud:
> >>> Justin C:
> >
> >>>> Viewed in vim they look like: ^[(s12H  ^[&16D  ^[(16H  ^[&18D
> >>>
> >>>    s/\e(?:[^@A-Z]*[@A-Z])|[=9]//g

Note there are two bugs here (as I mentioned earlier). This should read

    s/\e(?:[^\@A-Z]*[\@A-Z]|[=9])//g;

that is, the [=9] should be *inside* the parens, and the @s should be
backwhacked.

> >> Gonna have to learn more about REs to understand this one!
> >>
> >> I'm reading through perlre, the ?: is doing my head in a bit, may be
> >> it's been a long day: ``it groups subexpressions like "()" but doesn't
> >> make backreferences'' ... ``This is for clustering, not capturing''
> >
> > Concentrate on the "clustering, not capturing".
> >
> > You can just as well do it step by step:
> >
> >   s/\e[=9]//g ;
> 
> Why [=9]? I've read, and re-read, this thread and tried to understand
> what you and Ben are trying to explain to me (I'm really not stupid,
> honest), but I can't see, given the examples I gave in the OP, why the
> [=9].

Dr.Ruud is assuming that what you are in fact trying to do is remove HP
PCL escape sequences. 'ESC 9' and 'ESC =' are valid sequences, in
addition to the 'ESC ( s 1 S'-type sequence you mentioned.

> >   s/\e[^@A-Z]*[@A-Z]//g ;
> I also don't understand how the "clustering" use of ?: is enabling these
> two lines to be made into one (it maybe that if I understand one part of
> this the other falls into place). I'm not seeing what effect the ?: is
> having, the one line without the ?: looks, to me, like it'd do the same
> as the two.

The (?:) (when corrected) is causing the RE to match 'escape, followed
by (either a multi-char escape sequence or one of the single-char
sequences'. It is necessary as without the grouping the | alternation
would apply to the whole regex, and any '=' would be stripped. Compare

    s/                          # either
            \e                  #   escape
            [^\@A-Z]*[\@A-Z]    #   multi-char sequence
        |                       # or
            [=9]                #   '=' or '9;
    //gx;

with

    s/
        \e                      # escape
        (?:                     # followed by either
            [^\@A-Z]*[\@A-Z]    #   multi-char sequence
        |                       # or
            [=9]                #   '=' or '9'
        )
    //gx;

The parens are what make the escape always required for a match. (You
are right that in the example as given, the parens are useless. This is
what made me sure there was a bug :).)

Ben

-- 
"If a book is worth reading when you are six,         * benmorrow@tiscali.co.uk
it is worth reading when you are sixty."  [C.S.Lewis]


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

Date: Wed, 9 Aug 2006 22:45:49 +0100
From: Ben Morrow <benmorrow@tiscali.co.uk>
Subject: Re: Encrypt Windows Password
Message-Id: <dl1qq3-6kt.ln1@osiris.mauzo.dyndns.org>


Quoth rpirpag@aon.at:
> Hello,
> 
> Peter.Kramer wrote:
> > Hello,
> > I want to write a perl module that encrypts a clear password to a windows 
> > hash.
> > I didnt find any algorithm that builds the hash expect copypwd.exe which 
> > works, but is an external program that does not run as scheduled task.
> > 
> > "cleartextpassword " ->
> > "52616e646f6d4956dc110786f80987daa99d164016ba7d72b24eb2a2931e0ff7"
> 
> Copypwd uses the functions from the "Windows Management API" (MS
> Platform SDK) and from the samsrv.dll.

 ...in which case if you *really* want to you can use Win32::API or XS to
get at them from Perl.

Ben

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


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

Date: 9 Aug 2006 16:41:37 -0700
From: "Matt Garrish" <mgarrish@gmail.com>
Subject: Re: How to send command line options into test scripts?
Message-Id: <1155166897.583346.76260@h48g2000cwc.googlegroups.com>


yusuf wrote:
[context restored]
> Matt wrote:
> > yusuf wrote:
> >
> > >
> > > Does anyone know how to send command line options into test scripts
> > > from prove?
> >
> > Then it's not a question to post to a perl coding group, is it?
>
> But its a perl question right? Which group should it go to?

It's no more relevant than asking how ppm works. I suspect there's a
Perl question you're trying to ask, but since you refuse outright to
post any code (or even give an explanation of what you're doing) I
doubt you'll find anyone inclined to help you get to it.

Why out of curiosity would you post one vague snippet of code and then
claim it's not a code question?

Matt



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

Date: 9 Aug 2006 16:47:08 -0700
From: "DJ Stunks" <DJStunks@gmail.com>
Subject: Re: How to send command line options into test scripts?
Message-Id: <1155167228.310497.79120@i42g2000cwa.googlegroups.com>

yusuf wrote:
> Hi,
>
> Does anyone know how to send command line options into test scripts
> from prove?
>
> For instance, I want to call the test file "t" with the options:
>
> --log somefile --output=true
>
> but I can't seem to find out how to do it from prove. I tried using the
> $Test::Harness::switches var, but that does not seem to do it.
>
> I didn't post any code because this is not a code question.

Perl does not exist independent of code; Perl IS code.

-jp

PS - There is no spoon.



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

Date: 9 Aug 2006 17:28:17 -0700
From: "yusuf" <yusufm@gmail.com>
Subject: Re: How to send command line options into test scripts?
Message-Id: <1155169697.668811.42370@n13g2000cwa.googlegroups.com>

> Why out of curiosity would you post one vague snippet of code and then
> claim it's not a code question?

I didn't post any code. They were command line argument examples. I
can't post any code because I don't know how to code the thing I need.
The thing I need is to:

- From 'prove' which uses Test::Harness, when I call the different test
files in runtests(). How do I send in command line arguments to those
test files.



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

Date: 9 Aug 2006 15:06:57 -0700
From: "Shan" <Shani718@gmail.com>
Subject: Need help with parsing data
Message-Id: <1155161217.511809.285450@m73g2000cwd.googlegroups.com>

So I need code that will go through a list of URLs (formatted as
http://www.google.com) and for each url get the following information:

1. The url after the href= within the following tags <link
rel="alternate"   and   />

So if there is <link rel="alternate" type="application/atom+xml"
title="Atom" href="http://hello.typepad.com/hello/atom.xml" />  I want
the http://hello.typepad.com/hello/atom.xml


2. everything bewtween the following tags <title> and </title>
so if there is <title>hello, typepad</title> I want hello, typepad

3. everything between the tags <h2 id="banner-description"> and </h2>


4. Finally i would like the results to be saved to a delimited file in
the following format:

column 1: original url
column 2: data obtained from step 1
column 3: data obtained from step 2
column 4: data obtained from step 3

if there is no result for any one of the steps a null should be saved.


I would like to thank whoever can provide me with the code in advance,
Thank you.



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

Date: 9 Aug 2006 15:19:45 -0700
From: "DJ Stunks" <DJStunks@gmail.com>
Subject: Re: Need help with parsing data
Message-Id: <1155161985.818405.312890@h48g2000cwc.googlegroups.com>

Shan wrote:
> So I need code that will go through a list of URLs (formatted as
> http://www.google.com) and for each url get the following information:
>
> 1. The url after the href= within the following tags <link
> rel="alternate"   and   />
>
> So if there is <link rel="alternate" type="application/atom+xml"
> title="Atom" href="http://hello.typepad.com/hello/atom.xml" />  I want
> the http://hello.typepad.com/hello/atom.xml
>
>
> 2. everything bewtween the following tags <title> and </title>
> so if there is <title>hello, typepad</title> I want hello, typepad
>
> 3. everything between the tags <h2 id="banner-description"> and </h2>
>
>
> 4. Finally i would like the results to be saved to a delimited file in
> the following format:
>
> column 1: original url
> column 2: data obtained from step 1
> column 3: data obtained from step 2
> column 4: data obtained from step 3
>
> if there is no result for any one of the steps a null should be saved.
>
>
> I would like to thank whoever can provide me with the code in advance,
> Thank you.

it is highly unlikely that anyone will do so for a simple "thanks".
check out jobs.perl.org for someone willing to follow orders in return
for compensation.

-jp



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

Date: Wed, 09 Aug 2006 22:41:38 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Need help with parsing data
Message-Id: <Xns981ABE4289B7Aasu1cornelledu@127.0.0.1>

"Shan" <Shani718@gmail.com> wrote in news:1155161217.511809.285450@m73g2000cwd.googlegroups.com:

> So I need code that will go through a list of URLs (formatted as
> http://www.google.com) and for each url get the following information:
> 
> 1. The url after the href= within the following tags <link
> rel="alternate"   and   />

That's just one tag. 
 
> So if there is <link rel="alternate" type="application/atom+xml"
> title="Atom" href="http://hello.typepad.com/hello/atom.xml" />  I want
> the http://hello.typepad.com/hello/atom.xml

use HTML::TokeParser::Simple;

http://search.cpan.org/~ovid/HTML-TokeParser-Simple-3.15/

> 2. everything bewtween the following tags <title> and </title>
> so if there is <title>hello, typepad</title> I want hello, typepad

Ditto.

> 3. everything between the tags <h2 id="banner-description"> and </h2>

Ditto.

> 4. Finally i would like the results to be saved to a delimited file in
> the following format:
> 
> column 1: original url
> column 2: data obtained from step 1
> column 3: data obtained from step 2
> column 4: data obtained from step 3

Trivial.

> if there is no result for any one of the steps a null should be saved.
> 
> 
> I would like to thank whoever can provide me with the code in advance,

That's not how it works here. Feel free to compose a proper post showing us 
what you have tried - after having read and followed the posting guidelines 
- and help will flow.

If you don't want to be bothered with that, you might be able to generate 
enough "warm glow" to motivate me to help by making a donation of $1000 or 
more to the Perl foundation:

http://donate.perl-foundation.org/index.pl?node=Fund+Drive+Details&selfund=2

If you don't want to bother with either of those, then try:

http://jobs.perl.org/

Sinan

PS: For the record, I am in no way affiliated with the Perl Foundation. 
I have not yet donated. I should. Oh, the guilt. 

-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html



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

Date: 9 Aug 2006 23:17:50 GMT
From: John Bokma <john@castleamber.com>
Subject: Re: Need help with parsing data
Message-Id: <Xns981ABA1FFC265castleamber@130.133.1.4>

"Shan" <Shani718@gmail.com> wrote:

> So I need code that will go through a list of URLs (formatted as
> http://www.google.com) and for each url get the following information:
> 
> 1. The url after the href= within the following tags <link
> rel="alternate"   and   />
> 
> So if there is <link rel="alternate" type="application/atom+xml"
> title="Atom" href="http://hello.typepad.com/hello/atom.xml" />  I want
> the http://hello.typepad.com/hello/atom.xml
> 
> 
> 2. everything bewtween the following tags <title> and </title>
> so if there is <title>hello, typepad</title> I want hello, typepad
> 
> 3. everything between the tags <h2 id="banner-description"> and </h2>


I use HTML::TreeBuilder for this, since it makes life really easy. See 
http://johnbokma.com/perl/ for several examples (Web automation).

For example 3. can be done as:

my $root = HTML::TreeBuilder->new_from_content( $content );

:
:

my @column4;
push @column4, $_->as_trimmed_text
    	for $root->look_down( _tag => h2, id =>'banner-description' );

> I would like to thank whoever can provide me with the code in advance,
> Thank you.

I can provide the code, and forms to thank me are here:
    	http://johnbokma.com/wish-list.html

Either Object Oriented Perl or Perl Best Practices would be fine with me 
since directly and indirectly you will contribute back to the Perl 
community.

-- 
John Bokma          Freelance software developer
                                &
                    Experienced Perl programmer: http://castleamber.com/


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

Date: 9 Aug 2006 15:36:42 -0700
From: Brett.R.Davis@gmail.com
Subject: OO Perl : Struggling with hash data members in my Class
Message-Id: <1155163002.760990.177090@m79g2000cwm.googlegroups.com>

I am writing my first Perl Class and have hit a brick wall.

What I need to do is have a class method copy a hash into the data
member %MY_HASH.

I have coded a constructor and a method my_hash which is *supposed* to
either:

1. return the HASH reference or
2. write a copy the contents of a hash into the MY_COLORS data member.

Then I wanted a second data member that uppon being written to with a
symbolic name, would use the MY_COLORS hash to provide the numberical
code for that COLOR as shown:

my $composite_colors={'green'=>1, 'blue'=>2, 'red'=>3 };
my $color_set=Chash->new();
$chash->my_colors($composite_colors);
$chash->color_code('blue');
print $chash{'color_code'};

The output should be:

2

However, I am never able to get the object $chash to take in the
$composite_colors reference, and copy it into the data member
MY_COLORS.  I get comments like "odd number of elements in anonymous
hash".  If I data dump the contents of the data member MY_COLORS it
shows that the $composite_colors hash is being copied in as a key to
the has instead of essentially becoming the MY_COLORS hash.

Any assistance would be greatly appreciated!!!!

Brett


Here is the code.

package Chash;

sub new {
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my $self  = {};
    $self->{MY_COLORS}  = {};
    $self->{COLOR_CODE}  = undef;
    bless($self, $class);
    return $self;
}

sub my_colors {
    my $self = shift;
    if (@_) { $self->{MY_COLORS} = $_[0]  }
    return  { $self->{MY_COLORS} };
}


sub color_code {
    my $self = shift;
    if (@_) {
        if (ref $self->{MY_COLORS} eq "HASH") {
            $self->{COLOR_CODE} = $self->{MY_COLORS}->{$_[0]};
        }
        else {
            $self->{COLOR_CODE} = shift;
        }
    }
    return $self->{COLOR_CODE};
}



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

Date: Wed, 09 Aug 2006 23:39:22 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: OO Perl : Struggling with hash data members in my Class
Message-Id: <Xns981AC80C0735Basu1cornelledu@127.0.0.1>

Brett.R.Davis@gmail.com wrote in news:1155163002.760990.177090
@m79g2000cwm.googlegroups.com:

> I am writing my first Perl Class and have hit a brick wall.

OK. It happens.

> What I need to do is have a class method copy a hash into the data
> member %MY_HASH.

Just a suggestion. It is always easier for us to read Perl than 
someone's description of it in English.

> I have coded a constructor and a method my_hash which is *supposed* to
> either:
> 
> 1. return the HASH reference or
> 2. write a copy the contents of a hash into the MY_COLORS data member.

For greater safety, I would recommend separate accessors for each job.

> Then I wanted a second data member that uppon being written to with a
> symbolic name, would use the MY_COLORS hash to provide the numberical

s/numberical/numerical/

> code for that COLOR as shown:
> 
> my $composite_colors={'green'=>1, 'blue'=>2, 'red'=>3 };
> my $color_set=Chash->new();
> $chash->my_colors($composite_colors);
> $chash->color_code('blue');
> print $chash{'color_code'};

Do not use the name of the data structure you use in the implementation 
of your class as the name of the class. In this case, something like 
Color::RGB seems appropriate.

Now, when playing with your own classes, it is important to make sure 
there is now possibility of a name clash with other modules. If I am 
just experimenting, I generally prefix my modules with My as

My::Color::RGB

> package Chash;

You are not using strict or warnings. Please make sure to put

use strict;
use warnings;

> 
> sub new {
>     my $proto = shift;
>     my $class = ref($proto) || $proto;

I know you are just copying from some book or even perldoc perltoot, but 
I find calling new on instances to be an awful thing. Others have 
different opinions. 


>     my $self  = {};
>     $self->{MY_COLORS}  = {};
>     $self->{COLOR_CODE}  = undef;

I don't know why you need a COLOR_CODE entry here. After all, all you 
want to do is look up the color by name and return the number 
assosicated with it.

>     bless($self, $class);
>     return $self;
> }
> 
> sub my_colors {
>     my $self = shift;
>     if (@_) { $self->{MY_COLORS} = $_[0]  }
>     return  { $self->{MY_COLORS} };
> }

Magical dual purpose accessors have a way of biting you in the rear end 
when you least expect it.


> sub color_code {
>     my $self = shift;
>     if (@_) {
>         if (ref $self->{MY_COLORS} eq "HASH") {
>             $self->{COLOR_CODE} = $self->{MY_COLORS}->{$_[0]};
>         }
>         else {
>             $self->{COLOR_CODE} = shift;
>         }
>     }
>     return $self->{COLOR_CODE};
> }

Again, I would consider having individual named accessors for each color 
component. 

The code below indirectly answers your question, and also has a full set 
of accessors. Note that there are a number of modules on CPAN that make 
generating accessors a breeze. One of them is Class::Accessor::Fast. 
Reading the docs for that module would be a good idea.

#!/usr/bin/perl

package My::Color::RGB;

use strict;
use warnings;

use Carp;
use Readonly;

Readonly::Array my @default_rgb => (0, 0, 0);

sub new {
    my $class = shift;
    my $self = bless { } => $class;

    @_ ? $self->set_rgb( @_ ) : $self->set_rgb( @default_rgb );
    return $self;
}

sub get_rgb { my $self = shift; @{ %$self }{qw( r g b )} }

sub set_rgb {
    my $self = shift;
    unless ( @_ == 3 ) {
        croak sprintf(
            '%s->set_rgb invoked with incorrect number or arguments', 
            __PACKAGE__,
        );
    }
    @{ %$self }{qw( r g b )} = @_;
    return;
}

sub get_color_by_name {
    my $self = shift;
    my $color = $self->__check_color_name( shift );
    return $self->{$color};
}

sub set_color_by_name {
    my $self = shift;
    my $color = $self->__check_color_name( shift );
    $self->{$color} = shift;
    return;
}

sub get_red   { my $self = shift; $self->{r} }
sub get_green { my $self = shift; $self->{g} }
sub get_blue  { my $self = shift; $self->{b} }

sub set_red   { my $self = shift; $self->{r} = shift }
sub set_green { my $self = shift; $self->{g} = shift }
sub set_blue  { my $self = shift; $self->{b} = shift }

sub __check_color_name {
    my $self = shift;
    my ($color) = @_;

    $color = lc $color;

    unless (   $color =~ /^(r)(?:ed)?$/ 
            or $color =~ /^(g)(?:reen)?$/
            or $color =~ /^(b)(?:lue)?$/ 
    ) {
        croak "Unknown color: '$color'";
    }
    return $1;
}

package main;

use strict;
use warnings;

my $rgb = My::Color::RGB->new(1, 2, 3);

print $rgb->get_color_by_name('blue'), "\n";

$rgb->set_rgb(123, 14, 45);

print $rgb->get_color_by_name('red'), "\n";

$rgb->set_green(255);
$rgb->set_red(122);

use Data::Dumper;
print Dumper $rgb;

print join(',', $rgb->get_rgb), "\n";

print $rgb->get_color_by_name('purple'), "\n";

__END__

-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html



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

Date: Thu, 10 Aug 2006 00:05:03 +0100
From: Big and Blue <No_4@dsl.pipex.com>
Subject: Re: Question about Arrays
Message-Id: <sOCdnfMvwry990fZnZ2dnUVZ8tSdnZ2d@pipex.net>

Guenther Sohler wrote:

> sub add_fruits
> {
> 	my $databaseptr=shift;
> 	push  ... databaseptr .. fruits , "Banana";
> # C Code would by: database-> fruits += "Banana";

    Then you have a strange C compiler.

    += is an integer operation.

    "Banana" is a char *  (pointer to a string).

    Whereas I expect some C compilers would let you do the  increment by 
converting the pointer to an integer first I doubt very much whether it 
would produce any reasonable result.


-- 
              Just because I've written it doesn't mean that
                   either you or I have to believe it.


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

Date: Wed, 9 Aug 2006 22:33:37 +0100
From: Ben Morrow <benmorrow@tiscali.co.uk>
Subject: Re: Read socket using both <> and sysread()
Message-Id: <hu0qq3-6kt.ln1@osiris.mauzo.dyndns.org>


Quoth xhoster@gmail.com:
> Yohan N. Leder <ynleder@nspark.org> wrote:
> > Hello. In the framework of a dialog with an ESMTP server, I would like
> > to alternate some incoming socket reading using <> (when server replies
> > with a single line) and sysread() (when server replies with several
> > lines), is there something to take care with this kind of alternance ?
> 
> Why not just use read() instead of sysread()?

Or if you're going down that route, why not just set $/ = \1024; when
you want a block read and always use <> ?

Ben

-- 
Musica Dei donum optimi, trahit homines, trahit deos. |
Musica truces mollit animos, tristesque mentes erigit.|benmorrow@tiscali.co.uk
Musica vel ipsas arbores et horridas movet feras.     |


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

Date: 10 Aug 2006 00:16:36 GMT
From: xhoster@gmail.com
Subject: Re: Read socket using both <> and sysread()
Message-Id: <20060809202602.525$ns@newsreader.com>

Ben Morrow <benmorrow@tiscali.co.uk> wrote:
> Quoth xhoster@gmail.com:
> > Yohan N. Leder <ynleder@nspark.org> wrote:
> > > Hello. In the framework of a dialog with an ESMTP server, I would
> > > like to alternate some incoming socket reading using <> (when server
> > > replies with a single line) and sysread() (when server replies with
> > > several lines), is there something to take care with this kind of
> > > alternance ?
> >
> > Why not just use read() instead of sysread()?
>
> Or if you're going down that route, why not just set $/ = \1024; when
> you want a block read and always use <> ?

I'd find it a bit confusing to be constantly changing $/ back and
forth between "\n" and \1024.  If I could just set it to one thing
at the top of the program and forget it, then that that is what I would do.
But I would rather use read() than have to keep changing $/ back and forth.

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


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

Date: 10 Aug 2006 00:42:22 GMT
From: xhoster@gmail.com
Subject: Re: Read socket using both <> and sysread()
Message-Id: <20060809205148.978$ZK@newsreader.com>

Uri Guttman <uri@stemsystems.com> wrote:
> >>>>> "x" == xhoster  <xhoster@gmail.com> writes:
>
>   x> Yohan N. Leder <ynleder@nspark.org> wrote:
>   >> Hello. In the framework of a dialog with an ESMTP server, I would
>   >> like to alternate some incoming socket reading using <> (when server
>   >> replies with a single line) and sysread() (when server replies with
>   >> several lines), is there something to take care with this kind of
>   >> alternance ?
>
>   x> Why not just use read() instead of sysread()?
>
> that works but since the api is the same, all you get is more buffering
> with read with no benefits. sockets already have buffering so there is
> no need for more in the user space.

I'm not sure I understand this.  Is this special to sockets?  What form of
IO *doesn't* already have some buffering at the system level?

Anyway, additional user-space buffering does seem like it makes things
faster for sockets (as well as for regular files) with small reads, but I
don't know if the difference is meaningful for real-world cases.

> time perl -le 'use IO::Socket;
      my $x=IO::Socket::INET->new("localhost:9871") or die $@;
      $y+=read $x,$buf,10 foreach 1..10_000_000;
      print $y'
100000000
3.694u 0.138s 0:03.84 99.4%     0+0k 0+0io 0pf+0w

> time perl -le 'use IO::Socket;
      my $x=IO::Socket::INET->new("localhost:9871") or die $@;
      $y+=sysread $x,$buf,10 foreach 1..10_000_000;
      print $y'
100000000
4.097u 5.682s 0:09.80 99.6%     0+0k 0+0io 0pf+0w


> having done tons of c and socket
> stuff before, sysread is my choice as it is just a wrapper around c's
> read.
>
> but maybe your point is mixing read with <> and that could work if done
> carefully (it is more a protocol and implementation problem then).

Yep, that is what I meant.

> it is
> just not my style so i would never do that.

So then maybe you are the wrong person to ask, but why does Perl's read
exist?

Was it intended to be just like sysread only doesn't intefere with <>?

Or was it intended to be just like sysread only faster because it uses
an extra layer of buffering?

Is the fact that read will block until the requested size (or eof) is read
(as opposed to sysread, which blocks until at least 1 byte is read, but
after that will return a partial buffer if need be rather than blocking) a
real feature or a malfeature or a bug?

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


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

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

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

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 9584
***************************************


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