[28295] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 9659 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Aug 28 18:05:59 2006

Date: Mon, 28 Aug 2006 15:05:06 -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           Mon, 28 Aug 2006     Volume: 10 Number: 9659

Today's topics:
    Re: A Sort Optimization Technique: decorate-sort-dedeco <jgibson@mail.arc.nasa.gov>
    Re: A Sort Optimization Technique: decorate-sort-dedeco <rvtol+news@isolution.nl>
    Re: A Sort Optimization Technique: decorate-sort-dedeco <uri@stemsystems.com>
    Re: Beginner: read $array with line breaks line by line <mstep@t-online.de>
    Re: DBI Performance Issues <chris@thezengarden.net>
    Re: DBI Performance Issues xhoster@gmail.com
    Re: pack problem? <hjp-usenet2@hjp.at>
    Re: printing a multidimensional array in table format <joelingram@gmail.com>
    Re: Problem handling a Unicode file <hjp-usenet2@hjp.at>
    Re: Problem handling a Unicode file <rvtol+news@isolution.nl>
        Question about socket and other modules <onestatusquo@yahoo.com>
        Tie and Lexical Filehandles or IO::File? <vtatila@mail.student.oulu.fi>
    Re: Tie and Lexical Filehandles or IO::File? <mritty@gmail.com>
    Re: Tie and Lexical Filehandles or IO::File? <vtatila@mail.student.oulu.fi>
    Re: What bless() do actually? anno4000@radom.zrz.tu-berlin.de
        Working with Source Code to Insert Copyright Statements <sgordon@egenera.com>
    Re: Working with Source Code to Insert Copyright Statem <john@castleamber.com>
    Re: Working with Source Code to Insert Copyright Statem <mritty@gmail.com>
    Re: Working with Source Code to Insert Copyright Statem <john@castleamber.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Mon, 28 Aug 2006 11:50:08 -0700
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: A Sort Optimization Technique: decorate-sort-dedecorate
Message-Id: <280820061150081729%jgibson@mail.arc.nasa.gov>

In article <pan.2006.08.28.08.53.08.647908@gmx.net>, Marc 'BlackJack'
Rintsch <bj_666@gmx.net> wrote:

> In <1156723602.192984.49610@m79g2000cwm.googlegroups.com>, Tom Cole wrote:
> 
> > In Java, classes can implement the Comparable interface. This interface
> > contains only one method, a compareTo(Object o) method, and it is
> > defined to return a value < 0 if the Object is considered less than the
> > one being passed as an argument, it returns a value > 0 if considered
> > greater than, and 0 if they are considered equal.
> > 
> > The object implementing this interface can use any of the variables
> > available to it (AKA address, zip code, longitude, latitude, first
> > name, whatever) to return this -1, 0 or 1. This is slightly different
> > than what you mention as we don't have to "decorate" the object. These
> > are all variables that already exist in the Object, and if fact make it
> > what it is. So, of course, there is no need to un-decorate at the end.
> 
> Python has such a mechanism too, the special `__cmp__()` method
> has basically the same signature.  The problem the decorate, sort,
> un-decorate pattern solves is that this object specific compare operations
> only use *one* criteria.

I can't believe I am getting drawn into a thread started by xahlee, but
here goes anyway:

The problem addressed by what is know in Perl as the 'Schwartzian
Transform' is that the compare operation can be an expensive one,
regardless of the whether the comparison uses multiple keys. Since in
comparison sorts, the compare operation will be executed N(logN) times,
it is more efficient to pre-compute a set of keys, one for each object
to be sorted. That need be done only N times. The sort can then use
these pre-computed keys to sort the objects. See, for example:

http://en.wikipedia.org/wiki/Schwartzian_transform

-- 
Jim Gibson

 Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------        
                http://www.usenet.com


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

Date: Mon, 28 Aug 2006 21:38:47 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: A Sort Optimization Technique: decorate-sort-dedecorate
Message-Id: <ecvnso.1gg.1@news.isolution.nl>

Jim Gibson schreef:

> The problem addressed by what is know in Perl as the 'Schwartzian
> Transform' is that the compare operation can be an expensive one,
> regardless of the whether the comparison uses multiple keys. Since in
> comparison sorts, the compare operation will be executed N(logN)
> times, it is more efficient to pre-compute a set of keys, one for
> each object to be sorted. That need be done only N times. The sort
> can then use these pre-computed keys to sort the objects.

Basically it first builds, than sorts an index.

The pre-computed (multi-)keys can often be optimized, see Uri's
Sort::Maker http://search.cpan.org/search?query=Sort::Maker
for facilities.

-- 
Affijn, Ruud

"Gewoon is een tijger."




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

Date: Mon, 28 Aug 2006 16:07:38 -0400
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: A Sort Optimization Technique: decorate-sort-dedecorate
Message-Id: <x7r6z0vd2t.fsf@mail.sysarch.com>

>>>>> "R" == Ruud  <rvtol+news@isolution.nl> writes:

  R> Basically it first builds, than sorts an index.

  R> The pre-computed (multi-)keys can often be optimized, see Uri's
  R> Sort::Maker http://search.cpan.org/search?query=Sort::Maker
  R> for facilities.

gack, now i ave been mentioned and responding to a thread started by the
moron xah. his flame on the ST shows how stupid he is. he calls it
decorate/sort/undecorate but he doesn't know it was a well known
technique way back in the 50's and 60's. typical of his narrow mind and
lack of real understanding and computer history. the ST didn't invent it
but just showed how to do it easily in perl. and the GRT didn't invent
pack/sort either, it just (and Sort::Maker) just does it easily for you
in perl. both the ST and GRT are implemetation idioms of a known sort
optimization trick used in the real world. in pure algorithmic analysis,
neither one would be even considered as they don't improve the actual
growth curve of the sort. and all the other langs (deleted from the
headers) are just showing their simple equivilent sort/cmp ops. BFD.

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: Mon, 28 Aug 2006 21:57:44 +0200
From: Marek Stepanek <mstep@t-online.de>
Subject: Re: Beginner: read $array with line breaks line by line
Message-Id: <C1191758.2B7BA%mstep@t-online.de>



I don't know, how to thank you for all your input. I came back this evening,
thinking to submit you once again my new script, but now I realize, I have
first to digest all your suggestions. So probably until tomorrow evening.

greetings from Munich


marek



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

Date: Mon, 28 Aug 2006 15:47:44 -0400
From: "Chris H." <chris@thezengarden.net>
Subject: Re: DBI Performance Issues
Message-Id: <pan.2006.08.28.19.47.43.722731@thezengarden.net>

On Mon, 28 Aug 2006 16:27:31 +0000, xhoster wrote:

> You are not making the important distiction between throughput and latency.
> Your Perl script sends the sql command, then waits to get a response.
> While waiting, it does nothing--it doesn't use CPU and/or bandwidth by
> "sending ahead on prospect" more update statements.  So you are limited by
> latency, not by throughput.  With an insert, you can circumvent this by
> using the MySQL-specific multiple row insert statement to add many rows per
> network round-trip.  Maybe you can use the MySQL-specific "INSERT...ON
> DUPLICATE KEY UPDATE" syntax to accomplish the same thing--just a
> suggestion, I've never looked into it in detail myself.  However, if your
> real situation will have the two machines on the same LAN, then latency
> will probably not be a problem.  You simply can't do benchmarking on such
> disparate setups and expect the results to be meaningful.
> 
> Xho

i understand the differences in the setup, but i dont see latency playing
that much of a role in this issue. ping response from solaris to windows
on the lan (live setup) is 2.73ms. ping response from my home machine to
my colo box is 17.5ms. my issue is that the two setups perform the same in
regards to how slow they are getting the information to the server. i can
understand it being slower, that's a given, but what im having an issue
with is that it takes just as long to update 10 records remotely as 1,000
locally...then something is going wrong somewhere. i'd suspect it should
be faster than that.

thanks again for all the information thus far, it is giving me different
ideas to try. i'll definately look at the 'insert...on duplicate key
update' documentation to see if it'll work.

-- 
Chris H.
http://www.nasland.nu



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

Date: 28 Aug 2006 21:24:37 GMT
From: xhoster@gmail.com
Subject: Re: DBI Performance Issues
Message-Id: <20060828172508.387$Yp@newsreader.com>

"Chris H." <chris@thezengarden.net> wrote:
> On Mon, 28 Aug 2006 16:27:31 +0000, xhoster wrote:
>
> > You are not making the important distiction between throughput and
> > latency. Your Perl script sends the sql command, then waits to get a
> > response. While waiting, it does nothing--it doesn't use CPU and/or
> > bandwidth by "sending ahead on prospect" more update statements.  So
> > you are limited by latency, not by throughput.  With an insert, you can
> > circumvent this by using the MySQL-specific multiple row insert
> > statement to add many rows per network round-trip.  Maybe you can use
> > the MySQL-specific "INSERT...ON DUPLICATE KEY UPDATE" syntax to
> > accomplish the same thing--just a suggestion, I've never looked into it
> > in detail myself.  However, if your real situation will have the two
> > machines on the same LAN, then latency will probably not be a problem.
> > You simply can't do benchmarking on such disparate setups and expect
> > the results to be meaningful.
> >
> > Xho
>
> i understand the differences in the setup, but i dont see latency playing
> that much of a role in this issue. ping response from solaris to windows
> on the lan (live setup) is 2.73ms. ping response from my home machine to
> my colo box is 17.5ms.

Is that under load or under no load?  Anyway, you reported 71 seconds for
1000 records, which is 71ms per record.  71ms is only fourfold more than
17.5 ms, so I would say that latency is definitely in the ball park to be a
problem. (I would expect MySQL to have more overhead then ping--4 times
more would not greatly surprise me.)

> my issue is that the two setups perform the same
> in regards to how slow they are getting the information to the server. i
> can understand it being slower, that's a given, but what im having an
> issue with is that it takes just as long to update 10 records remotely as
> 1,000 locally...then something is going wrong somewhere. i'd suspect it
> should be faster than that.

I'm afraid you lost me.  You have a localhost msyql connection with 1000
records per second, and an over-cable-modem mysql connection with 14
records per second.  Do you have a *mysql* connection on a LAN?  If so,
what speed did that provide?

Xho

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


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

Date: Mon, 28 Aug 2006 20:17:26 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: pack problem?
Message-Id: <slrnef6cpo.b8f.hjp-usenet2@yoyo.hjp.at>

["Followup-To:" header set to comp.lang.perl.misc.]
On 2006-08-28 03:17, Davy <zhushenli@gmail.com> wrote:
> I want to write a dec data to bin data translator.
> But I found the $dec_pack in the program don't print "A signed integer
> value" as the pack manual said. Why? Thanks!

Why do you think so?

	hp


-- 
   _  | Peter J. Holzer    | > Wieso sollte man etwas erfinden was nicht
|_|_) | Sysadmin WSR       | > ist?
| |   | hjp@hjp.at         | Was sonst wäre der Sinn des Erfindens?
__/   | http://www.hjp.at/ |	-- P. Einstein u. V. Gringmuth in desd


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

Date: 28 Aug 2006 11:28:20 -0700
From: "joel" <joelingram@gmail.com>
Subject: Re: printing a multidimensional array in table format
Message-Id: <1156789700.814951.173370@m73g2000cwd.googlegroups.com>

Thanks for the detailed tips, much more than I expected. I have used
printf but it would require that I write code to determine the maximum
widths of each column (the number of columns and size being variable).
Not big deal but I thought that there may be an even easier way. Perl
is for doing things the easy way, right ;-)


Brian McCauley wrote:
> joel wrote:
> > For any number of rows and columns of a multidimensional array, I would
> > like my printHandler subroutine to print in table format with minimal
> > effort. What is the easiest way of doing this?
> >
> > {
> > 	...
> > 	...
> > 	while (my (@array)=($sth->fetchrow_array()))
>
> All those parentheses don't really add clarity...
>
> 	while (my @array = $sth->fetchrow_array )
>
> > 	{
> > 		push(@result,[@array]);
> > 	}
>
> Since @array is declared within the loop (good!) you don't need to make
> a copy of it, you can push a reference to @array itself as you'll get a
> new @array on the next iteration.
>
>  		push(@result,\@array);
>
> > 	&printHandler(@result);
>
> Do you know what the & there does?  If not then remove it.
>
> As a general rule you should pass arrays to subroutines using array
> references rather than unrolling the array into a list and passing each
> element of the array as a separate argument.
>
> 	printHandler(\@result);
>
> BTW: have you considered using fetchall_arrayref?
>
> > sub printHandler
> > {
> > 	my $i=0;
> > 	foreach (@_)
> > 	{
> > 		print "@{$_[$i]}\n";
> > 		$i++;
> > 	}
> > }
>
> Ouch! You put each element of @_ in turn into $_ and then ignore that
> and hand carft a loop index to use as a subscript.
>
>         foreach (@_)
>  	{
>  		print "@{$_}\n"; # {} strictly redundant
> 	}
>
> Or if you'd passed an array ref...
>
>         my ($list)= @_;
>         foreach (@$list)
>  	{
>  		print "@$_\n";
> 	}
>
>
> > current output:
> > 4591 05-00371
> > 149817 06-02930
> > 15541 06-0369
> > 827 04-00307
> > 13406 06-01364
> >
> > desired output:
> > 4591   05-00371
> > 149817 06-02930
> > 15541  06-0369
> > 827    04-00307
> > 13406  06-01364
>
> Ah, you want to print the output formatted.  Perl has a "print
> formatted" function ..
>
>         my ($list)= @_;
>         for (@$list)
>  	{
>  		printf "%6s %s\n",@$_;
> 	}
> 
> Or if you feel terse..
> 
>  		printf "%6s %s\n",@$_ for @{+shift};



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

Date: Mon, 28 Aug 2006 20:29:24 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Problem handling a Unicode file
Message-Id: <slrnef6dg6.b8f.hjp-usenet2@yoyo.hjp.at>

On 2006-08-28 10:11, MoshiachNow <lev.weissman@creo.com> wrote:
> Thanks,
>
> did just that.Reads the file nicely.
> Then I want to reolace strings in the file and write it back in utf16
> to Araxi2.reg.
> I use the code below,but the file does not look good in Notepad
> anymore,meaning the format is not exactly utf16 ...

Notepad needs the BOM at the beginning of the file to recognize it
is UTF16, so you have to write that:

> 	open (FILE,'>:encoding(utf16)',"Araxi2.reg") || die "Could not open
	print FILE "\x{FEFF}";

or, if you prefer symbolic names:

use charnames ':short';
 ...
print FILE "\N{BOM}";


	hp


-- 
   _  | Peter J. Holzer    | > Wieso sollte man etwas erfinden was nicht
|_|_) | Sysadmin WSR       | > ist?
| |   | hjp@hjp.at         | Was sonst wäre der Sinn des Erfindens?
__/   | http://www.hjp.at/ |	-- P. Einstein u. V. Gringmuth in desd


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

Date: Mon, 28 Aug 2006 22:28:46 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: Problem handling a Unicode file
Message-Id: <ecvqr8.j8.1@news.isolution.nl>

Peter J. Holzer schreef:

> Notepad needs the BOM at the beginning of the file to recognize it
> is UTF16, so you have to write that:

With "encoding(UTF-16)", the IO-layer takes care of that. But then you
leave it up to Perl (Encode::PerlIO?) to choose between UTF-16LE and
UTF-16BE. See also perldoc Encode::Unicode.


At opening, the file is 0 bytes, but after printing a single space, it
becomes 4 bytes, with the first two holding the BOM:

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

  my $fni = 'Araxi.reg' ;
  my $fno = 'Araxi1.reg' ;

  open my $fhi, '<:encoding(UTF-16)', $fni
    or die "open '$fni', stopped $!" ;

  open my $fho, '>:encoding(UTF-16)', $fno
    or die "open '$fno', stopped $!" ;

  print $fho ' ' ;
__END__

-- 
Affijn, Ruud

"Gewoon is een tijger."




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

Date: 28 Aug 2006 14:33:26 -0700
From: "SQ" <onestatusquo@yahoo.com>
Subject: Question about socket and other modules
Message-Id: <1156800806.506969.212910@75g2000cwc.googlegroups.com>

Do these modules come standard with Perl 5.8, or do they need to be
installed 
separately:

Use Socket;
Use Pg;
Use Net::Ping;



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

Date: Mon, 28 Aug 2006 22:11:01 +0300
From: "Veli-Pekka Tätilä" <vtatila@mail.student.oulu.fi>
Subject: Tie and Lexical Filehandles or IO::File?
Message-Id: <ecvf49$vs4$1@news.oulu.fi>

Hi,
I'm playing around with tied filehandles at the moment. Now that I'm finally 
beginning to grasp the basics of Perl OOP, tying doesn't seem all that 
difficult. The only problem is, I cannot, for the life of me, figure out how 
to tie lexically scoped handles or IO::File objects. I'd like to hold my 
file handles in lexically scoped scalars be them globrefs or objects. I've 
read perltie and perlref, browsed the Perl FAQ and Googled both the groups 
and the Web with little success.

In the interest of simplicity, I won't be showing my real code here, but a 
short test script demonstrating the problem. Here's a very basic tied handle 
example slightly adapted from the perltie man-page (I had to add one my in 
it):

use strict; use warnings;
package Shout;
sub TIEHANDLE { print "<shout>\n"; my $i; bless \$i, shift }
sub PRINT
{
   no warnings 'uninitialized'; # $\ tends to be undef.
   my $r = shift; $$r++;
   print join($,,map(uc($_),@_)),$\
}
sub CLOSE { print "CLOSE called.\n" }
sub DESTROY { print "</shout>\n" }

And now the test program (actually in the same file):

package main;
# Only the first sub is succesful, the rest attempt calling TIESCALAR and 
die.
oldschool(); lexical(); oop();

The first of the Strategies looks like this:

sub oldschool
{ # Local type globs.
   local *FH;
   tie *FH, 'Shout';
   print FH "Tie Fighter\n";
   close FH;
} # sub

This strategy works well, calling TIEHANDLE, PRINT, CLOSE AND DESTROY as 
expected. The output is:

<shout>
TIE FIGHTER
CLOSE called.
</shout>

However, I like this filehandle syntax the least of the bunch.

My second approach goes like this:

sub lexical
{ # Typeglob refs in scalars.
   use Symbol 'gensym';
   my $fh;
   $fh = gensym;
   # open my $fh, '>test.dat' or die $!;
   tie $fh, 'Shout';
   print $fh "X-Wing\n";
} # sub

The only major difference is using gensym to generate a reference to an 
anonymous typeglob held in a scalar. The second method dies with the 
message:

Can't locate object method "TIESCALAR" via package "Shout"

Whether the file handle is generated with gensym or derived by passing open 
an undef scalar appears to make no difference. That is in both cases it 
seems tie looks at its args, determines that the first is a scalar and 
assumes I'd liek to tie a scalar. Although quite logical, in a way, I would 
have expected it to handle filehandles as diversly as print et al do.

The third and final method is the same as the second, except that we use a 
scalar holding an IO::File object. Again tie infers that I'd like to tie a 
scalar and the problem persists. For completeness, here's the code:

sub oop
{ # The object-oriented interface.
   use IO::File;
   my $fh = IO::File->new;
   tie $fh, 'Shout';
   print $fh "Death Star\n";
} # sub

I can, of course, use localized file handles for a solution that works. I 
would dearly liek to use some safer variant, however. For simple stuff, 
lexically scoped file handles gotten from open are my favorite.

Any help greatly appreciated.

PS: Perl version is 5.8.8  on Win XP Pro SP2.

-- 
With kind regards Veli-Pekka Tätilä (vtatila@mail.student.oulu.fi)
Accessibility, game music, synthesizers and programming:
http://www.student.oulu.fi/~vtatila/ 




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

Date: 28 Aug 2006 12:39:06 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Tie and Lexical Filehandles or IO::File?
Message-Id: <1156793946.296627.312200@m73g2000cwd.googlegroups.com>

Veli-Pekka T=E4til=E4 wrote:
> I'm playing around with tied filehandles at the moment. Now that I'm fina=
lly
> beginning to grasp the basics of Perl OOP, tying doesn't seem all that
> difficult. The only problem is, I cannot, for the life of me, figure out =
how
> to tie lexically scoped handles or IO::File objects. I'd like to hold my
> file handles in lexically scoped scalars be them globrefs or objects.

Excellent!

> I've
> read perltie and perlref, browsed the Perl FAQ and Googled both the groups
> and the Web with little success.
>
> In the interest of simplicity, I won't be showing my real code here, but a
> short test script demonstrating the problem.

THANK YOU!!

 Here's a very basic tied handle
> example slightly adapted from the perltie man-page (I had to add one my in
> it):
>
> use strict; use warnings;
> package Shout;
> sub TIEHANDLE { print "<shout>\n"; my $i; bless \$i, shift }
> sub PRINT
> {
>    no warnings 'uninitialized'; # $\ tends to be undef.
>    my $r =3D shift; $$r++;
>    print join($,,map(uc($_),@_)),$\
> }
> sub CLOSE { print "CLOSE called.\n" }
> sub DESTROY { print "</shout>\n" }
>
> And now the test program (actually in the same file):
>
> package main;
> # Only the first sub is succesful, the rest attempt calling TIESCALAR and
> die.
> oldschool(); lexical(); oop();
>
> The first of the Strategies looks like this:
>
> sub oldschool
> { # Local type globs.
>    local *FH;
>    tie *FH, 'Shout';
>    print FH "Tie Fighter\n";
>    close FH;
> } # sub
>
> This strategy works well, calling TIEHANDLE, PRINT, CLOSE AND DESTROY as
> expected. The output is:
>
> <shout>
> TIE FIGHTER
> CLOSE called.
> </shout>
>
> However, I like this filehandle syntax the least of the bunch.
>
> My second approach goes like this:
>
> sub lexical
> { # Typeglob refs in scalars.
>    use Symbol 'gensym';
>    my $fh;
>    $fh =3D gensym;
>    # open my $fh, '>test.dat' or die $!;
>    tie $fh, 'Shout';
>    print $fh "X-Wing\n";
> } # sub
>
> The only major difference is using gensym to generate a reference to an
> anonymous typeglob held in a scalar.

Right.  And references are scalars.

> The second method dies with the message:
>
> Can't locate object method "TIESCALAR" via package "Shout"
>
> Whether the file handle is generated with gensym or derived by passing op=
en
> an undef scalar appears to make no difference. That is in both cases it
> seems tie looks at its args, determines that the first is a scalar and
> assumes I'd liek to tie a scalar.

Sounds right to me.

> Although quite logical, in a way, I would
> have expected it to handle filehandles as diversly as print et al do.

You want to tie the filehandle that $fh *references* to the Shout
class, not $fh itself.

tie *{$fh}, 'Shout';

> The third and final method is the same as the second, except that we use a
> scalar holding an IO::File object. Again tie infers that I'd like to tie a
> scalar and the problem persists. For completeness, here's the code:
>
> sub oop
> { # The object-oriented interface.
>    use IO::File;
>    my $fh =3D IO::File->new;
>    tie $fh, 'Shout';
>    print $fh "Death Star\n";
> } # sub

I don't know enough abou the internals of IO::File to know *why* this
works, but it does.  Same syntax as above.  Dereference $fh as a glob,
and everything becomes happy.

Paul Lalli



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

Date: Mon, 28 Aug 2006 23:11:05 +0300
From: "Veli-Pekka Tätilä" <vtatila@mail.student.oulu.fi>
Subject: Re: Tie and Lexical Filehandles or IO::File?
Message-Id: <ecviku$kp5$1@news.oulu.fi>

Paul Lalli wrote:
> Veli-Pekka Tätilä wrote:
<snipped my own post>
>>  Here's a very basic tied handle example
>> use strict; use warnings;
>> package Shout;
>> sub TIEHANDLE { print "<shout>\n"; my $i; bless \$i, shift }
>> sub PRINT
>> {
>>    no warnings 'uninitialized'; # $\ tends to be undef.
>>    my $r = shift; $$r++;
>>    print join($,,map(uc($_),@_)),$\
>> }
>> sub CLOSE { print "CLOSE called.\n" }
>> sub DESTROY { print "</shout>\n" }
>> <in package main>
>> sub lexical
>> { # Typeglob refs in scalars.
>>    use Symbol 'gensym';
>>    my $fh;
>>    $fh = gensym;
>>    # open my $fh, '>test.dat' or die $!;
>>    tie $fh, 'Shout';
>>    print $fh "X-Wing\n";
>> } # sub
>>
>> dies with the message:
>>
>> Can't locate object method "TIESCALAR" via package "Shout"
>>
>> it seems tie looks at its args, determines that the first is a
>> scalar and assumes I'd liek to tie a scalar.
>> <snip>
> You want to tie the filehandle that $fh *references* to the Shout
> class, not $fh itself.
Ah, a good point, thanks for a quick reply. Often in programming, some 
things are blatantly obvious once you do realize them. This time I didn't. 
On the other hand,  sometimes merely trying to explain a problem in this 
group has lead to the solution, so I needn't post at all.

>> sub oop
>> { # The object-oriented interface.
>>    use IO::File;
>>    my $fh = IO::File->new;
>>    tie $fh, 'Shout';
>>    print $fh "Death Star\n";
>> } # sub
> I don't know enough abou the internals of IO::File to know *why* this
> works, but it does.
Well, neither do I. Before I saw your reply, I thought of typing an 
appologetic addition to the thread about not thinking clearly all the way 
through. That is I realized based on the docs, which mention no tying 
action, that using a IO::File object as a file handle is probably silently 
wrong. Again seems Perl did what I ment so I aint complaining, <grin>.

-- 
With kind regards Veli-Pekka Tätilä (vtatila@mail.student.oulu.fi)
Accessibility, game music, synthesizers and programming:
http://www.student.oulu.fi/~vtatila/ 




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

Date: 28 Aug 2006 22:02:52 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: What bless() do actually?
Message-Id: <4lh7gcF1uhjpU1@news.dfncis.de>

Marc Espie <espie@nerim.net> wrote in comp.lang.perl.misc:
> In article <4lg4k7F1oc21U1@news.dfncis.de>,
>  <anno4000@radom.zrz.tu-berlin.de> wrote:
> >Perl's problem with OO isn't that you can't do some things (you can),
> >but that you can do some things in too many ways.
> 
> Nonsense. Being able to do things in many ways has always been THE trademark
> of the language. I've yet to be stopped by it, and so do lots of people,
> since CPAN is thriving with interesting modules, a lot of them showing
> very advanced object techniques.

Quite so.  But when you want to use two of them together you must study
both quite carefully to see how that can best be done.  It gets worse
with more modules, and the more advanced ones are often hardest to
integrate with others.

Perl's motto TIMTOWTDI shouldn't make us blind to the risks and
drawbacks this approach also has.

> >It begins with choosing an object type.  In most OO languages, an object
> >is a struct-like data type.  In Perl you get to chose if you want to use
> >a hash, an array, a scalar or one of the more exotic types, and they
> >are incompatible unless the programmer(s) take extra efforts.  If they
> >don't a program that uses one class to inherit from the other will
> >crash an burn.  That has so far stopped Perl from developing a useful
> >class library.  Many OO languages are more strongly characterized through
> >their class library than anything else.
> 
> A useful class library ? ah! then what I use every day isn't useful.
> You have everything at your disposal, and it's usually about ten lines
> of code to adapt stuff to use an outside class you don't know anything
> about.
>
> Focusing on inheritance is a bad design decision in my opinion. Too tight
> coupling. Composition is often much better. And you can use the autoloader
> to great effect to build new behavior on top of existing classes.

Inheritance is an integral part of OO programming.  Without neglecting
other techniques, inheritance ought to be available when it is the right
tool.  Using inheritance is not, per se, a bad design decision.  A
programmer who does everything through it (or tries to) isn't making
a bad design decision but is following a bad habit.

In Perl inheritance isn't as freely available as it should be.

> >Inside-out classes are promising in this respect.  They can be inherited
> >without restriction by every other class (inside-out or not).  They can
> >also inherit from one (but not more) non-inside-out classes, besides,
> >of course, arbitrary other inside-out classes.
> 
> Well, I don't care too much about clients misusing my code. I focus on
> writing useful code, and then if they abuse it, that's their problem, not
> mine.

The point of inside-out classes isn't protection of object data.  That's
a useful side effect.  The point is that every class has its own
name space without conflicts with other classes.

Even in a world where every object was standardized to be a hash ref
whose keys and values are used like the fields of a struct, (a very
popular implementation) you can't safely add more fields without knowing
the implementation of the host object.  It gets worse when the host
can be of any type.

Inside-out classes are a way to get rid of that dependency.  They are
not about protecting your data from malicious users.

> There was a quote in _programming perl_ to the effect that perl doesn't
> really need private/public because perl programmers prefer you stay out
> of their internals because you politely ask them to, not because you
> string a fence with barbed wire around it.  I adopted this as my
> opinion.

Roughly: "Perl prefers you to stay outside the living room because you
were not invited, not because it has a shotgun."

That's very well as far as it goes.  I, too, am happy to indicate
private routines and methods with a leading underscore and nothing
else.

The problem is that you don't always know when you are entering
someone's living room.  Then you find out they do have a shotgun
after all.

Anno


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

Date: 28 Aug 2006 11:06:35 -0700
From: "Perlgirl" <sgordon@egenera.com>
Subject: Working with Source Code to Insert Copyright Statements as a Header
Message-Id: <1156788395.425665.315450@p79g2000cwp.googlegroups.com>

I was wondering if you could give me a hand.

Where could I go to find some samples of PERL code that do the
following:

1. inserts a particular copyright statement
2. checks the existing copyrights and updates it to include the current
year if it's not included
Or if the copyright has the wrong date, the script gives an error.
3. It has to process particular file types (I have that info); a
listing of files to process (I have that info too), and cannot process
binary files (have that info too)

Any help you can give me would be appreciated. Many thanks.
SRGordon



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

Date: 28 Aug 2006 18:57:53 GMT
From: John Bokma <john@castleamber.com>
Subject: Re: Working with Source Code to Insert Copyright Statements as a Header
Message-Id: <Xns982D8E0E76ED3castleamber@130.133.1.4>

"Perlgirl" <sgordon@egenera.com> wrote:

> I was wondering if you could give me a hand.
> 
> Where could I go to find some samples of PERL code that do the

What is PERL code? 

> following:

What have you tried so far?

-- 
John                Experienced Perl programmer: http://castleamber.com/

          Perl help, tutorials, and examples: http://johnbokma.com/perl/


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

Date: 28 Aug 2006 12:05:55 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Working with Source Code to Insert Copyright Statements as a Header
Message-Id: <1156791955.736574.37980@p79g2000cwp.googlegroups.com>

Perlgirl wrote:
> I was wondering if you could give me a hand.
>
> Where could I go to find some samples of PERL code that do the
> following:

What John's rather obnoxious remark meant was that the language is
spelled "Perl" and the interpreter is spelled "perl".  There is no
"PERL".

> 1. inserts a particular copyright statement
> 2. checks the existing copyrights and updates it to include the current
> year if it's not included
> Or if the copyright has the wrong date, the script gives an error.
> 3. It has to process particular file types (I have that info); a
> listing of files to process (I have that info too), and cannot process
> binary files (have that info too)
>
> Any help you can give me would be appreciated. Many thanks.
> SRGordon

This isn't really the proper forum for your request.  Generally, this
newsgroup helps people in writing and debugging their own code, not
finding other people's code.

Now, if you want some help actually *writing* the program you've
described, you're going to need to be a lot more clear.  What do you
mean by "inserting a copyright statement"?  Inserting it where?  Into a
string?  Into a file that's already opened?  Into a file that is lying
on the disk somewhere?

What is meant by an "existing copyright"?  Do you have a particular
pattern that represents a copyright notice?

I don't know for certain, but you might find Tie::File helpful.  It's a
standard module as of Perl 5.8.  If you have that version, you can type
`perldoc Tie::File` at the command line to read its documentation.  If
not, you can search for it at http://search.cpan.org

I basically recommend: Opening each file you want to process, reading
it line-by-line and using pattern-matching to determine if the
copyright exists, using Tie::File to insert it where you want if it
doesn't, and using a search-and-replace if an incorrect one is found.

You should read:
perldoc perlre
perldoc -f open
perldoc -f readline

You should also read the Posting Guidelines that are posted to this
newsgroup twice a week.  Following them maximizes your chances of
getting good help from this group.

Paul Lalli



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

Date: 28 Aug 2006 20:07:56 GMT
From: John Bokma <john@castleamber.com>
Subject: Re: Working with Source Code to Insert Copyright Statements as a Header
Message-Id: <Xns982D99EF3DACEcastleamber@130.133.1.4>

"Paul Lalli" <mritty@gmail.com> wrote:

> Perlgirl wrote:
>> I was wondering if you could give me a hand.
>>
>> Where could I go to find some samples of PERL code that do the
>> following:
> 
> What John's rather obnoxious remark meant was that the language is
> spelled "Perl" and the interpreter is spelled "perl".  There is no
> "PERL".

Apologies >:->

-- 
John                Experienced Perl programmer: http://castleamber.com/

          Perl help, tutorials, and examples: http://johnbokma.com/perl/


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

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


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