[29514] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 758 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Aug 16 14:14:31 2007

Date: Thu, 16 Aug 2007 11: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           Thu, 16 Aug 2007     Volume: 11 Number: 758

Today's topics:
        Help: How to delete the first character from a line? <openlinuxsource@gmail.com>
    Re: Help: How to delete the first character from a line <mritty@gmail.com>
    Re: Help: How to delete the first character from a line anno4000@radom.zrz.tu-berlin.de
    Re: Help: How to delete the first character from a line <mritty@gmail.com>
    Re: Help: How to delete the first character from a line <openlinuxsource@gmail.com>
    Re: Help: How to delete the first character from a line <mritty@gmail.com>
    Re: Help: How to delete the first character from a line <noreply@gunnar.cc>
        Help: How to use filehandle to save files? <openlinuxsource@gmail.com>
    Re: Help: How to use filehandle to save files? (Jens Thoms Toerring)
    Re: Help: How to use filehandle to save files? <mritty@gmail.com>
    Re: Help: How to use filehandle to save files? <bik.mido@tiscalinet.it>
        How to ask questions on comp.lang.perl.*? <dan.otterburn@gmail.com>
    Re: How to ask questions on comp.lang.perl.*? anno4000@radom.zrz.tu-berlin.de
    Re: How to ask questions on comp.lang.perl.*? <dan.otterburn@gmail.com>
    Re: How to ask questions on comp.lang.perl.*? anno4000@radom.zrz.tu-berlin.de
    Re: How to ask questions on comp.lang.perl.*? (Randal L. Schwartz)
    Re: How to ask questions on comp.lang.perl.*? <dan.otterburn@gmail.com>
    Re: How to ask questions on comp.lang.perl.*? anno4000@radom.zrz.tu-berlin.de
    Re: How to ask questions on comp.lang.perl.*? (Randal L. Schwartz)
        How to get "<$myclass_instance>" to work? <socyl@987jk.com.invalid>
    Re: How to get "<$myclass_instance>" to work? xhoster@gmail.com
    Re: How to get "<$myclass_instance>" to work? <mritty@gmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Thu, 16 Aug 2007 20:48:03 +0800
From: Amy Lee <openlinuxsource@gmail.com>
Subject: Help: How to delete the first character from a line?
Message-Id: <pan.2007.08.16.12.48.03.551812@gmail.com>

Hello,

There's a file like this,

;348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323;asdassssssss
dadssdasef;ttgfhdfg

I hope it can be this,

348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323;asdassssssss
dadssdasef;ttgfhdfg

So my problem is how use Perl to delete the first character of the first
line. Use \s?

Thank you very much~

Regards,

Amy Lee



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

Date: Thu, 16 Aug 2007 06:04:27 -0700
From:  Paul Lalli <mritty@gmail.com>
Subject: Re: Help: How to delete the first character from a line?
Message-Id: <1187269467.354988.45170@r29g2000hsg.googlegroups.com>

On Aug 16, 8:48 am, Amy Lee <openlinuxsou...@gmail.com> wrote:
> There's a file like this,
>
> ;348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323;asdassssssss
> dadssdasef;ttgfhdfg
>
> I hope it can be this,
>
> 348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323;asdassssssss
> dadssdasef;ttgfhdfg
>
> So my problem is how use Perl to delete the first character of the first
> line. Use \s?

I have no idea how the space metacharacter would help you here.

I would just use four-arg substr.

perl -lpi.bkp -e'substr($_, 0, 1, "");' file.txt

perldoc -f substr

There Is, of course, More Than One Way To Do It.  Other ways include:
search-and-replace: s/^.//;
reverse/chomp: $_ = reverse $_; chomp; $_ = reverse $_;
three-arg substr: substr($_, 0, 1) = "";

If one of those makes more sense to you than the others, go for it.  I
prefer the substr.

Paul Lalli



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

Date: 16 Aug 2007 13:14:25 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: Help: How to delete the first character from a line?
Message-Id: <5ij0thF3op8mvU1@mid.dfncis.de>

Amy Lee  <openlinuxsource@gmail.com> wrote in comp.lang.perl.misc:
> Hello,
> 
> There's a file like this,
> 
> ;348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323;asdassssssss
> dadssdasef;ttgfhdfg
> 
> I hope it can be this,
> 
> 348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323;asdassssssss
> dadssdasef;ttgfhdfg
> 
> So my problem is how use Perl to delete the first character of the first
> line. Use \s?

Your question is not very clear.  Do you want to change the disk file
so that the leading semicolon is no longer there?  Or do you want to
keep it in the file, but remove it from the line before processing the
line further?

What do you mean with "use \s"?  "\s" denotes white space in a regular
expression, but that won't help deleting a semicolon.

Assuming you want to keep the file as it is, and assuming that $fh is
a readable file handle to your file (untested):

    while ( <$fh> ) {
        s/^;// if $. == 1; # only on the first line
        # further processing
    }

Anno


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

Date: Thu, 16 Aug 2007 06:16:52 -0700
From:  Paul Lalli <mritty@gmail.com>
Subject: Re: Help: How to delete the first character from a line?
Message-Id: <1187270212.984929.237450@g4g2000hsf.googlegroups.com>

On Aug 16, 9:04 am, Paul Lalli <mri...@gmail.com> wrote:
> On Aug 16, 8:48 am, Amy Lee <openlinuxsou...@gmail.com> wrote:
>
> > There's a file like this,
>
> > ;348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323;asdassssssss
> > dadssdasef;ttgfhdfg
>
> > I hope it can be this,
>
> > 348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323;asdassssssss
> > dadssdasef;ttgfhdfg
>
> > So my problem is how use Perl to delete the first character of the first
> > line.

Hrm.  I didn't register the "of the first line" part of this when I
answered.  My answer was presuming you wanted to delete the first
character of each line.

> I would just use four-arg substr.
>
> perl -lpi.bkp -e'substr($_, 0, 1, "");' file.txt

Simply add a check in there to make sure you only do this to the first
line:

perl -lpi.bkp -e'substr($_, 0, 1, "") if $. == 0;' file.txt

or, slurp the entire file at once rather than reading/writing line-by-
line (not recommended for large files):

perl -0 0777 -pi.bkp -e'substr($_, 0, 1, "");' file.txt

Paul Lalli



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

Date: Thu, 16 Aug 2007 21:48:38 +0800
From: Amy Lee <openlinuxsource@gmail.com>
Subject: Re: Help: How to delete the first character from a line?
Message-Id: <pan.2007.08.16.13.48.26.164730@gmail.com>

On Thu, 16 Aug 2007 13:14:25 +0000, anno4000 wrote:

> Amy Lee  <openlinuxsource@gmail.com> wrote in comp.lang.perl.misc:
>> Hello,
>> 
>> There's a file like this,
>> 
>> ;348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323;asdassssssss
>> dadssdasef;ttgfhdfg
>> 
>> I hope it can be this,
>> 
>> 348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323;asdassssssss
>> dadssdasef;ttgfhdfg
>> 
>> So my problem is how use Perl to delete the first character of the first
>> line. Use \s?
> 
> Your question is not very clear.  Do you want to change the disk file
> so that the leading semicolon is no longer there?  Or do you want to
> keep it in the file, but remove it from the line before processing the
> line further?
> 
> What do you mean with "use \s"?  "\s" denotes white space in a regular
> expression, but that won't help deleting a semicolon.
> 
> Assuming you want to keep the file as it is, and assuming that $fh is
> a readable file handle to your file (untested):
> 
>     while ( <$fh> ) {
>         s/^;// if $. == 1; # only on the first line
>         # further processing
>     }
> 
> Anno

Thank you sir, and $. means amount of lines?

Regards,

Amy Lee


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

Date: Thu, 16 Aug 2007 07:48:54 -0700
From:  Paul Lalli <mritty@gmail.com>
Subject: Re: Help: How to delete the first character from a line?
Message-Id: <1187275734.639058.223070@r34g2000hsd.googlegroups.com>

On Aug 16, 9:48 am, Amy Lee <openlinuxsou...@gmail.com> wrote:
> Thank you sir, and $. means amount of lines?

perldoc perlvar
<snip>
     $.      Current line number for the last filehandle
             accessed.


Paul Lalli



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

Date: Thu, 16 Aug 2007 17:32:52 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Help: How to delete the first character from a line?
Message-Id: <5ij91aF3plmesU1@mid.individual.net>

Amy Lee wrote:
> There's a file like this,
> 
> ;348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323;asdassssssss
> dadssdasef;ttgfhdfg
> 
> I hope it can be this,
> 
> 348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323;asdassssssss
> dadssdasef;ttgfhdfg
> 
> So my problem is how use Perl to delete the first character of the first
> line.

     open my $file, '+<', 'myfile.txt' or die $!;
     my @lines = <$file>;
     $lines[0] = substr $lines[0], 1;
     seek $file, 0, 0;
     truncate $file, 0;
     print $file @lines;

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: Thu, 16 Aug 2007 21:58:45 +0800
From: Amy Lee <openlinuxsource@gmail.com>
Subject: Help: How to use filehandle to save files?
Message-Id: <pan.2007.08.16.13.58.45.145955@gmail.com>

Hello, 

I use Perl to make a script to format EST sequences, and I meet a problem
that I can't solve.

When the script has formated the sequences, I hope the results can be
saved in files, not output to screen. For example, the sequence called
soybean_60.fasta, and I hope when it has been formated, the formation
result soybean_60.fasta_format can be created.

It's my codes:

foreach $EST (@ARGV)
  {
    if (! -e $EST)
    {
      print "Error: Couldn't find the $EST sequence.\n";
      print "Error: Pass ......\n";
      next;
    } 
    open EST, "$EST";
    while (<EST>)
    {
      s/ .*//g;
      s/\./ /g;
      s/ .*/>/g;
      s/gi\|//g;
      s/\|gb\|/>/g;
      s/^\n$//g;
      if ($. == 1)
      {
        s/^>//;
      }
      chomp;
    }
    print "$EST sequence formated okay.\n";
    close EST;
  }

Which part should I modify? How to accomplish this function by filehandle?

Thank you very much~

Regards,

Amy Lee


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

Date: 16 Aug 2007 14:24:17 GMT
From: jt@toerring.de (Jens Thoms Toerring)
Subject: Re: Help: How to use filehandle to save files?
Message-Id: <5ij50hF3p81rqU1@mid.uni-berlin.de>

Amy Lee <openlinuxsource@gmail.com> wrote:
> I use Perl to make a script to format EST sequences, and I meet a problem
> that I can't solve.

> When the script has formated the sequences, I hope the results can be
> saved in files, not output to screen. For example, the sequence called
> soybean_60.fasta, and I hope when it has been formated, the formation
> result soybean_60.fasta_format can be created.

> It's my codes:

> foreach $EST (@ARGV)
>   {
>     if (! -e $EST)
>     {
>       print "Error: Couldn't find the $EST sequence.\n";
>       print "Error: Pass ......\n";
>       next;
>     } 
>     open EST, "$EST";

I would use

      unless ( open $est_in, '<', $EST ) {
          print "Error, can't read $EST for reading.\n";
          print "Error: Pass ......\n";
          next;
      }

since the mere existence of the file (which you test above) doesn't
tell you that you have the necessary permissions to read the file.
I also don't like the use of 'EST' since nowadays you can use simple
variables as file handles. And note the additional '<' in the call
of open, it explicitely says that you want to open the file for
reading.

Once you have the file open I now would open the output file:

      unless ( open $est_out, '>', $EST . '_format' ) {
          print "Error, can't open file for writing.\n";
          print "Error: Pass ......\n";
          close $est_in;
          next;
      }

>     while (<EST>)

If you use $est_in instead you will need here

      while ( <$est_in> ) {

>     {
>       s/ .*//g;

You don't need the 'g' here since nothing will be left for a second
go once a space has been found.

>       s/\./ /g;
>       s/ .*/>/g;

Also here the 'g' isn't needed, everything including the first space
in the string will be replaced by a single '>'. And because of that
also the 'g' in the line above doesn't change anything. You could
replace these two lines for the same result by

        s/\..*/>/;

i.e. find the first dot and replace it and everything possibly
following it by '>'.


>       s/gi\|//g;
>       s/\|gb\|/>/g;
>       s/^\n$//g;

What's that supposed to do?

>       if ($. == 1)
>       {
>         s/^>//;
>       }
>       chomp;

Leave out the chomp, it doesn't do anything useful here. Instead write
the result of your conversions into the output file:

      print $est_out $_;

>     }
>     print "$EST sequence formated okay.\n";
>     close EST;

And here use

      close $est_in;
      close $est_out;

>   }
                                 Regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de


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

Date: Thu, 16 Aug 2007 07:51:56 -0700
From:  Paul Lalli <mritty@gmail.com>
Subject: Re: Help: How to use filehandle to save files?
Message-Id: <1187275916.925141.182600@19g2000hsx.googlegroups.com>

On Aug 16, 9:58 am, Amy Lee <openlinuxsou...@gmail.com> wrote:
> When the script has formated the sequences, I hope the results
> can be saved in files, not output to screen.

> Which part should I modify? How to accomplish this function by
> filehandle?

There are two basic kinds of filehandles.  Those opened for reading,
as you have here, and those opened for writing, which is what you
want.

You open a file for writing very similarly to how you open a file for
reading:

Read:
open my $rfh, '<', $filename or die $!;
Write:
open my $wfh, '>', $filename or die $!;

Then once you have your filehandle opened for writing, you simply
print to that filehandle whatever you want to print:

print $wfh "This text goes to the file\n";

and when you're done printing, close the filehandle:
close $wfh;

For a full description on how to work with filehandles, see:
perldoc -f open
perldoc perlopentut

Paul Lalli



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

Date: Thu, 16 Aug 2007 17:36:58 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: Help: How to use filehandle to save files?
Message-Id: <qmr8c3p81aeav1c69pbpp76qq66dcq2squ@4ax.com>

On Thu, 16 Aug 2007 21:58:45 +0800, Amy Lee
<openlinuxsource@gmail.com> wrote:

>When the script has formated the sequences, I hope the results can be
>saved in files, not output to screen. For example, the sequence called

Then redirect from your shell. Or else

  open my $out, '>', $somefile or die "Can't open $somefile: $!\n";
  select $out;


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: Thu, 16 Aug 2007 08:26:38 -0000
From:  Dan Otterburn <dan.otterburn@gmail.com>
Subject: How to ask questions on comp.lang.perl.*?
Message-Id: <1187252798.454392.177520@b79g2000hse.googlegroups.com>

I recently posted a question on comp.lang.perl.modules and have not
yet received any replies, leading me to the assumption that I have
made one of the following mistakes:

a) I have posted the question in the wrong group.
b) I have not titled the post in a clear or interesting enough way.
c) I have not asked a direct enough question.
d) I have formatted the post badly so it is difficult to read.*
e) The question is simply too dull to warrant a response!

Having read ESR's "How To Ask Smart Questions" (and particularly
http://www.catb.org/~esr/faqs/smart-questions.html#bespecific), I
suspect that I have failed on point b) but I would be particularly
grateful for any advice on this matter.

(I have used the comp.lang.perl groups a source of invaluable
information for years but this was the first question I have ever
posted).

I include the post below and would welcome any brutal dissection from
a group veteran!

Subject:  PAUSE Definition of Pre-Alpha, Alpha, Beta and Released
Software

On 14 Aug, 09:35, Dan Otterburn <dan.otterb...@gmail.com> wrote:
> I ask this question in specific relation to the "Development Stage"
> choice on the PAUSE Register Namespace form, though I guess it is
> really far more generic than that:
>
> Is there a formal or accepted definition of the various stages of
> software development within the Perl community, and if so I would be
> grateful if someone could point me in the right direction to pick up a
> bit more knowledge?
>
> I have Googled on the subject and definitions seem to be vague and
> vary quite significantly, particularly between the pre-release/pre-
> stable stages: pre-alpha/alpha/beta. I have put together a brief
> description below based on my research so far and look forward to
> being corrected!
>
> Idea - just an idea, no code yet.
> Pre-Alpha - first stab at code, proof-of-concept, likely to be very
> buggy, interface could change dramatically, entirely untested.
> Alpha - starting to take shape but still likely to be buggy and
> largely untested, interface settling down but still open to
> significant change.
> Beta - nearly there, largely tested and working, one or two known
> bugs, interface unlikely to change dramatically but no guarantees.
> Released - comprehensively tested, no _known_ bugs, interface is
> formalised and future development/bug fixes should take this into
> account.

*(I am currently using the Google Groups web interface to post
questions)



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

Date: 16 Aug 2007 10:40:05 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: How to ask questions on comp.lang.perl.*?
Message-Id: <5iins5F3pcpcfU1@mid.dfncis.de>

Dan Otterburn  <dan.otterburn@gmail.com> wrote in comp.lang.perl.misc:
> I recently posted a question on comp.lang.perl.modules and have not
> yet received any replies, leading me to the assumption that I have
> made one of the following mistakes:
> 
> a) I have posted the question in the wrong group.
> b) I have not titled the post in a clear or interesting enough way.
> c) I have not asked a direct enough question.
> d) I have formatted the post badly so it is difficult to read.*
> e) The question is simply too dull to warrant a response!

f) The question is fine, but there is no canonical answer.

[...]

> > Is there a formal or accepted definition of the various stages of
> > software development within the Perl community, and if so I would be
> > grateful if someone could point me in the right direction to pick up a
> > bit more knowledge?

There is no such definition.  It is left to the individual module
author to assign one of the development stages to published code.
Most authors seem to pass over the stages below "released".

> > Idea - just an idea, no code yet.
> > Pre-Alpha - first stab at code, proof-of-concept, likely to be very
> > buggy, interface could change dramatically, entirely untested.
> > Alpha - starting to take shape but still likely to be buggy and
> > largely untested, interface settling down but still open to
> > significant change.
> > Beta - nearly there, largely tested and working, one or two known
> > bugs, interface unlikely to change dramatically but no guarantees.
> > Released - comprehensively tested, no _known_ bugs, interface is
> > formalised and future development/bug fixes should take this into
> > account.

g) The question already contains an answer that no reader felt compelled
   to amend.

Anno


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

Date: Thu, 16 Aug 2007 11:02:59 -0000
From:  Dan Otterburn <dan.otterburn@gmail.com>
Subject: Re: How to ask questions on comp.lang.perl.*?
Message-Id: <1187262179.765039.292450@r29g2000hsg.googlegroups.com>

Many thanks for the advice Anno, it is much appreciated, and also for
answering the original question! With your permission I will post your
answer against my original message in comp.lang.perl.modules so it is
there for posterity...




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

Date: 16 Aug 2007 11:10:14 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: How to ask questions on comp.lang.perl.*?
Message-Id: <5iipkmF3q32h0U1@mid.dfncis.de>

Dan Otterburn  <dan.otterburn@gmail.com> wrote in comp.lang.perl.misc:
> Many thanks for the advice Anno, it is much appreciated, and also for
> answering the original question! With your permission I will post your
> answer against my original message in comp.lang.perl.modules so it is
> there for posterity...

That's fine with me if you think it's warranted.

On Usenet, you got to learn to read the silence.  The one you got was
the silence of approval, not the silence of dismissal.

Anno


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

Date: Thu, 16 Aug 2007 07:32:16 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: How to ask questions on comp.lang.perl.*?
Message-Id: <86tzqzeej3.fsf@blue.stonehenge.com>

>>>>> "anno4000" == anno4000  <anno4000@radom.zrz.tu-berlin.de> writes:

anno4000> On Usenet, you got to learn to read the silence.  The one you got was
anno4000> the silence of approval, not the silence of dismissal.

Or the silence of "WTF!".  :)

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

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



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

Date: Thu, 16 Aug 2007 16:16:50 -0000
From:  Dan Otterburn <dan.otterburn@gmail.com>
Subject: Re: How to ask questions on comp.lang.perl.*?
Message-Id: <1187281010.430757.9470@22g2000hsm.googlegroups.com>

On 16 Aug, 15:32, mer...@stonehenge.com (Randal L. Schwartz) wrote:

> Or the silence of "WTF!".  :)

I hesitate to request clarification on the (perceived?) ambiguity of a
statement from a name that I associate with the stuff of legend for
fear that I might get what is coming to me, but here goes:

Does that mean 'The silence of "WTF!" is another important type of
silence you must be able to recognise on Usenet' or 'Foolish newbie!
The original post was greeted with a silence of "WTF!" because it was
beneath contempt' ... or a bit of both?




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

Date: 16 Aug 2007 16:44:07 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: How to ask questions on comp.lang.perl.*?
Message-Id: <5ijd6nF3prra2U1@mid.dfncis.de>

Randal L. Schwartz <merlyn@stonehenge.com> wrote in comp.lang.perl.misc:
> >>>>> "anno4000" == anno4000  <anno4000@radom.zrz.tu-berlin.de> writes:
> 
> anno4000> On Usenet, you got to learn to read the silence.  The one you got was
> anno4000> the silence of approval, not the silence of dismissal.
> 
> Or the silence of "WTF!".  :)

 ...not to mention that of the lambs.

Anno


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

Date: Thu, 16 Aug 2007 10:27:46 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
To: Dan Otterburn <dan.otterburn@gmail.com>
Subject: Re: How to ask questions on comp.lang.perl.*?
Message-Id: <86bqd7e6el.fsf@blue.stonehenge.com>

>>>>> "Dan" == Dan Otterburn <dan.otterburn@gmail.com> writes:

Dan> On 16 Aug, 15:32, mer...@stonehenge.com (Randal L. Schwartz) wrote:
>> Or the silence of "WTF!".  :)

Dan> I hesitate to request clarification on the (perceived?) ambiguity of a
Dan> statement from a name that I associate with the stuff of legend for
Dan> fear that I might get what is coming to me, but here goes:

Dan> Does that mean 'The silence of "WTF!" is another important type of
Dan> silence you must be able to recognise on Usenet' or 'Foolish newbie!
Dan> The original post was greeted with a silence of "WTF!" because it was
Dan> beneath contempt' ... or a bit of both?

I sometimes skip answering questions simply because I don't know where
to begin or if it would even help.

For example, if the question is something like (thinking out loud here)...

  "How would I best integrate Perl into a shop that has decided that Perl
   is too unmaintainable, and has chosen C++ instead?"

I'd stand back and let others start poking at that tar-baby (reference Br'er
Rabbit) before I would come anywhere near.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

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



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

Date: Thu, 16 Aug 2007 15:34:22 +0000 (UTC)
From: kj <socyl@987jk.com.invalid>
Subject: How to get "<$myclass_instance>" to work?
Message-Id: <fa1qpu$imt$1@reader1.panix.com>




I want to write a class whose instances are iterators that will
behave similarly to (readonly) file handles.  I.e. I want to be
able to write:

  my $thingie = Thingie->new( @args );
  while ( <$thingie> ) {
    # do stuff with $_ 
  }

Can someone point me to a good example of a module in CPAN that
does this, for me to study?  I'm looking specifically for a module
that achieves this functionality via standard Perl subclassing (as
opposed to tied variables, which I find too magic and mysterious).

TIA!

kj
-- 
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.


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

Date: 16 Aug 2007 16:32:19 GMT
From: xhoster@gmail.com
Subject: Re: How to get "<$myclass_instance>" to work?
Message-Id: <20070816123221.243$NG@newsreader.com>

kj <socyl@987jk.com.invalid> wrote:
> I want to write a class whose instances are iterators that will
> behave similarly to (readonly) file handles.  I.e. I want to be
> able to write:
>
>   my $thingie = Thingie->new( @args );
>   while ( <$thingie> ) {
>     # do stuff with $_
>   }
>
> Can someone point me to a good example of a module in CPAN that
> does this, for me to study?  I'm looking specifically for a module
> that achieves this functionality via standard Perl subclassing (as
> opposed to tied variables, which I find too magic and mysterious).

Have you already looked at the Iterator section of perldoc overload?
It isn't exactly an example, but it should be pretty informative if
you haven't already read it.

Surprisingly, I can't find any installed modules that use this feature of
overload.  Maybe I just can't formulate the grep correctly to find them.

fgrep --include=\*.pm -r -C 4 'overload' /tools/perl/ | fgrep '<>'

Xho

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


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

Date: Thu, 16 Aug 2007 09:44:39 -0700
From:  Paul Lalli <mritty@gmail.com>
Subject: Re: How to get "<$myclass_instance>" to work?
Message-Id: <1187282679.716646.33380@50g2000hsm.googlegroups.com>

On Aug 16, 11:34 am, kj <so...@987jk.com.invalid> wrote:
> I want to write a class whose instances are iterators that will
> behave similarly to (readonly) file handles.  I.e. I want to be
> able to write:
>
>   my $thingie = Thingie->new( @args );
>   while ( <$thingie> ) {
>     # do stuff with $_
>   }
>
> Can someone point me to a good example of a module in CPAN that
> does this, for me to study?  

I don't know about a CPAN module explicitly, but it's pretty easy for
me to create an example.  <> is one of the overloadable operators with
the 'overload' pragma:

package MyClass;
use strict;
use warnings;
use overload
      '<>' => \&iter,
;

sub new {
    my $class = shift;
    my $ref = { count => -1, things => [ @_ ] };
    bless $ref, $class;
}

sub iter {
    my $obj = shift;
    my $count = ++$obj->{count};
    if ($count == @{$obj->{things}}){
        $obj->{count} = -1;
        return;
    }
    return $obj->{things}[$count];
}

1;


> I'm looking specifically for a module
> that achieves this functionality via standard Perl subclassing (as
> opposed to tied variables, which I find too magic and mysterious).

You certainly could do it with tied variables.  I think you'd want to
tie your object as a filehandle.  But I agree, not much point in that
level of indirection in this case.  Overloading the operator is
easier.

Here's a main file for you to test the above class on:

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

my $mc = MyClass->new(qw/foo bar baz/);

print "First\n";
while (my $thing = <$mc>) {
    print "$thing\n";
}
print "Second\n";
while (my $thing = <$mc>) {
    print "$thing\n";
}
__END__

(I iterated twice simply to prove that the overloaded <> operator
correctly returns undef when the list is exhausted, and resets the
counter correctly.)

Hope this helps,
Paul Lalli



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

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


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