[31183] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2428 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue May 19 21:09:39 2009

Date: Tue, 19 May 2009 18:09:07 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Tue, 19 May 2009     Volume: 11 Number: 2428

Today's topics:
    Re: ampersand subroutine <news123@free.fr>
    Re: ampersand subroutine <RedGrittyBrick@SpamWeary.foo>
    Re: ampersand subroutine <uri@PerlOnCall.com>
    Re: ampersand subroutine <hjp-usenet2@hjp.at>
    Re: ampersand subroutine <uri@PerlOnCall.com>
    Re: ampersand subroutine <jurgenex@hotmail.com>
    Re: ampersand subroutine <hjp-usenet2@hjp.at>
    Re: ampersand subroutine <tadmc@seesig.invalid>
        ANNOUNCE: Math::Polynomial 1.001 <mhasch@cpan.org>
    Re: Archive::Zip and correct extension (mixture of Word <gcox@freeuk.com>
    Re: Archive::Zip and correct extension (mixture of Word <jimsgibson@gmail.com>
    Re: beginner array of array... thank you all <uri@PerlOnCall.com>
    Re: compiled perl question. <1usa@llenroc.ude.invalid>
    Re: compiled perl question. <spamtrap@dot-app.org>
    Re: Outputting to PDF simply? <RedGrittyBrick@SpamWeary.foo>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Tue, 19 May 2009 21:00:51 +0200
From: News123 <news123@free.fr>
Subject: Re: ampersand subroutine
Message-Id: <4a1301e3$0$5240$426a74cc@news.free.fr>

i Juergen,

I'm a little surprised by one of Guy's and one of your statements.


Jürgen Exner wrote:
> "Guy" <someone@somewhere.nb.ca> wrote:

>> So I read that you can omit the ampersand if "the compiler sees the 
>> subroutine definition before invocation or..."

Please see my example at the end of the post.
the function is declared after the call and things work fine.


> 3: the ampersand is not "a thing of the past". It modifies the calling
> semantic, such that 
> 	a) prototypes are overridden
> 	b) @_ is visible to the called sub

@_ is also visible in my example, but you say the ampersand is needed.
I guess you mean soething different, but I don't understand it.

My example:
----------------
use strict;
use warnings;
require 5.000;
func1(1,2,3);
func2(4,5,6);
exit(0);

# ##############################################################
# now only function declarations (after the "function calls"
# the question is whether I should do this and what damage
# is done if I do it.
#
sub func1 {
    print "I am func 1 and my args are <",join("> <",@_),">\n";
    func2(@_);
}


sub func2 {
    print "I am func 2 and my args are <",join("> <",@_),">\n";
}


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

Date: Tue, 19 May 2009 20:01:59 +0100
From: RedGrittyBrick <RedGrittyBrick@SpamWeary.foo>
Subject: Re: ampersand subroutine
Message-Id: <k-ednar04OO6n47XnZ2dnUVZ8gydnZ2d@bt.com>

Guy wrote:
> I've seen some code that call user subroutines without the ampersand.  I 
> didn't know you could do that.
> 
> "BTW, I went out and bought Learning Perl to help with my Intermediate 
> Perl."
> 
> So I read that you can omit the ampersand if "the compiler sees the 
> subroutine definition before invocation or..."
> 
> But instead of declaring all my subroutines at the begining of my script, or 
> ensuring they're located ahead of invocation, wouldn't it just be safer and 
> even easier to always have the ampersand? or are ampersand really a thing of 
> the past when not absolutely needed?
> 

For me, ampersands as a function-name prefix are a thing of the past.

C:\temp> type test.pl
#!perl
use strict;
use warnings;

foo(1);
foo(2);
foo();

sub foo {
   my $p = shift || 42;
   bar($p);
}

sub bar {
   my $q = shift;
   print "bar($q)\n";
}

C:\temp> perl test.pl
bar(1)
bar(2)
bar(42)

-- 
RGB


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

Date: Tue, 19 May 2009 15:10:48 -0400
From: "Uri Guttman" <uri@PerlOnCall.com>
Subject: Re: ampersand subroutine
Message-Id: <87r5ykaoxz.fsf@quad.sysarch.com>

>>>>> "N" == News123  <news123@free.fr> writes:

  N> @_ is also visible in my example, but you say the ampersand is needed.
  N> I guess you mean soething different, but I don't understand it.

you don't get the docs. it is not that @_ is 'visible'. when you call a
sub with () you always set @_ to the arg list or empty if no args. when
you call with &foo, @_ is LEFT AS IS and that is seen by the called
sub. that is a major difference and nasty if you don't expect it. so
using () is the proper way to call subs in ALL cases unless you must use
the & semantic (which is very very rarely needed). there is no other
defensible reason to use & style calls in perl.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Tue, 19 May 2009 21:49:24 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: ampersand subroutine
Message-Id: <slrnh163a4.t5a.hjp-usenet2@hrunkner.hjp.at>

On 2009-05-19 04:21, Jürgen Exner <jurgenex@hotmail.com> wrote:
> 3: the ampersand is not "a thing of the past". It modifies the calling
> semantic, such that 
> 	a) prototypes are overridden
> 	b) @_ is visible to the called sub

I'd say it is exactly these properties that make & "a thing of the
past". In modern perl-code you don't want to do these things (well,
almost never). 

There is one place where you still need the ampersand - if you need to
take a reference to a sub. You cannot write

    sub foo {
	...
    }

    my $x = \foo;

you need to write

    my $x = \&foo;

(otherwise foo will be called and $x will get a reference to its return
value).

	hp


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

Date: Tue, 19 May 2009 16:02:35 -0400
From: "Uri Guttman" <uri@PerlOnCall.com>
Subject: Re: ampersand subroutine
Message-Id: <87hbzg97z8.fsf@quad.sysarch.com>

>>>>> "PJH" == Peter J Holzer <hjp-usenet2@hjp.at> writes:

  PJH> There is one place where you still need the ampersand - if you
  PJH> need to take a reference to a sub.

  PJH>     my $x = \&foo;

also with magic goto you need the & prefix. and it leaves @_ as is which
is an important part of its semantics of replacing the current sub with
another. we never said & was not needed in perl, it is just not needed
nor generally wanted in directly calling subs. when i see & for sub
calls it raises a red flag and it usually means the rest of the code is
on the poor side.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Tue, 19 May 2009 14:10:10 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: ampersand subroutine
Message-Id: <er761596bicdfd6fd8laj48tcj72lca75m@4ax.com>

News123 <news123@free.fr> wrote:
>I'm a little surprised by one of Guy's and one of your statements.
>
>
>Jürgen Exner wrote:
>> "Guy" <someone@somewhere.nb.ca> wrote:
>
>>> So I read that you can omit the ampersand if "the compiler sees the 
>>> subroutine definition before invocation or..."
>
>Please see my example at the end of the post.
>the function is declared after the call and things work fine.

That's because (as Tad explained earlier) you are calling it with an
argument list, i.e. with paranthesis. If you omit the paranthesis then
you will get an error about bareword not allowed.

>> 3: the ampersand is not "a thing of the past". It modifies the calling
>> semantic, such that 
>> 	a) prototypes are overridden
>> 	b) @_ is visible to the called sub
>
>@_ is also visible in my example, but you say the ampersand is needed.
>I guess you mean soething different, but I don't understand it.

Uri already explained that part.

jue


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

Date: Tue, 19 May 2009 23:24:52 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: ampersand subroutine
Message-Id: <slrnh168t4.u8q.hjp-usenet2@hrunkner.hjp.at>

On 2009-05-19 20:02, Uri Guttman <uri@PerlOnCall.com> wrote:
>>>>>> "PJH" == Peter J Holzer <hjp-usenet2@hjp.at> writes:
>  PJH> There is one place where you still need the ampersand - if you
>  PJH> need to take a reference to a sub.
>
>  PJH>     my $x = \&foo;
>
> also with magic goto you need the & prefix. and it leaves @_ as is which
> is an important part of its semantics of replacing the current sub with
> another.

Yes, magic goto was one of the reasons why I qualified "you don't want
to do that" with "almost never". 

> we never said & was not needed in perl,

who is "we"? And anyway, I didn't say that you said that, quite the
contrary. To me, Jürgens claim that "& is not a thing of the past"
implied that & *is* regularly needed in Perl for supressing prototypes
and preserving @_. I stronglly disagree with that (and I don't actually
think Jürgen meant that), so I wrote that you usually *don't* want to do
these things.

> it is just not needed nor generally wanted in directly calling subs.
> when i see & for sub calls it raises a red flag and it usually means
> the rest of the code is on the poor side.

I fully agree with that.

	hp


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

Date: Tue, 19 May 2009 16:31:20 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: ampersand subroutine
Message-Id: <slrnh16998.c4q.tadmc@tadmc30.sbcglobal.net>

News123 <news123@free.fr> wrote:
> i Juergen,
>
> I'm a little surprised by one of Guy's and one of your statements.
>
>
> Jürgen Exner wrote:
>> "Guy" <someone@somewhere.nb.ca> wrote:
>
>>> So I read that you can omit the ampersand if "the compiler sees the 
>>> subroutine definition before invocation or..."
>
> Please see my example at the end of the post.
> the function is declared after the call and things work fine.


There *are no* function declarations in your code.

There are only function definitions.

"define" and "declare" mean very different things!


>> 3: the ampersand is not "a thing of the past". It modifies the calling
>> semantic, such that 
>> 	a) prototypes are overridden
>> 	b) @_ is visible to the called sub
>
> @_ is also visible in my example, 


That is because you *pass* its contents to func2.

func1's @_ is NOT visible to func2. func2 has a *different* @_.


> but you say the ampersand is needed.
> I guess you mean soething different, but I don't understand it.


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

func1(1,2,3);

sub func1 {
   &func2;
}

sub func2 {
    print "$_\n" for @_;
}
----------------

We have not passed anything to func2, yet it outputs 3 things.

Where did the 3 things come from?


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


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

Date: Wed, 20 May 2009 00:07:47 GMT
From: Martin Becker <mhasch@cpan.org>
Subject: ANNOUNCE: Math::Polynomial 1.001
Message-Id: <KJwz3K.Axs@zorch.sf-bay.org>

I am pleased to announce the release of Math::Polynomial 1.001,
which is a major development step from the previous version (0.04).

It is now (or should shortly be) available from:

    http://search.cpan.org/~mhasch/Math-Polynomial-1.001/

Math::Polynomial is a Perl class representing polynomials in one
variable.  It provides a set of operations defined for polynomials,
like addition, multiplication, division with remainder, evaluation,
nesting, etc., as well as attribute inspection and formatting
capabilities.

Version 1.001 is identical to version 1.000 except for the version
number and the fact that its tarball was not truncated by a broken
server before it made it to CPAN.

New in version 1.000 are many operations, like power, greatest
common divisor, nesting, derivative and antiderivative, comparison
and modular inverse.

There is now more control over string representations of polynomials,
in general and per object.

New, too, is support for arbitrary coefficient spaces, like
complex numbers, rationals, finite fields or matrices.

Gone are methods like tidy() and size() dealing with implementation
details of no mathematical significance.  In fact, as backwards
compatibility was given up anyway, some more cosmetic API changes
could be established as well.

For a more detailed history of changes, see the Changes file in the
distribution.  Migration issues for applications upgrading from
older versions to 1.000 are addressed in detail in the examples
collection.

CPAN Module Entry

6) Data Types and Data Type Utilities (see also Database Interfaces)

Name           DLSIP  Description                                  Info
-----------    -----  -------------------------------------------- -----
Math::
::Polynomial   RdpOp  Perl class for polynomials in one variable

DLSIP STATUS

   Development State:  R - Released
   Language Used:      p - Perl-only
   Support Level:      d - Developer
   Interface Style:    O - Object oriented
   Public License:     p - Standard-Perl

Share and enjoy!

-Martin




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

Date: Tue, 19 May 2009 19:35:33 +0100
From: Geoff Cox <gcox@freeuk.com>
Subject: Re: Archive::Zip and correct extension (mixture of Word and PowerPoint files)?
Message-Id: <6gu5159pssd62mpdciatfarh4dp0g5hb6l@4ax.com>

On Tue, 19 May 2009 06:58:37 -0500, Tad J McClellan
<tadmc@seesig.invalid> wrote:

>If you change the requirements, the code wil likely need to be changed.
>
>
>>     ( my $name = $_ ) =~ s/\.zip$/.doc/i or return;
>
>
>That is vestigial.
>
>It used to have a purpose, but now has no purpose.
>
>If you no longer want to replace with "doc", then you need to remove
>code that replaces with "doc".

Tad,

I don't understand this. The code below gives an xls extension to the
new files where the original zip file conatined an xls file, a ppt
extension to those which contained a ppt file and a doc extension to
those which contained a doc file and the same base name as the
original zip files - but! It only works if the

   ( my $name = $_ ) =~ s/\.zip$/.doc/i or return;

line is present. If it isn't I get 

can't call method "memberNames" on an undefined value at ***

What is happening?!

Geoff


use warnings;
use strict;

use File::Find;
use Archive::Zip;

my $dir = 'c:/a-temp2/docs';

find( sub {
   ( my $name = $_ ) =~ s/\.zip$/.doc/i or return;
    my $zip = Archive::Zip->new( $_ );
    my($arch_ext) = ($zip->memberNames)[ 0 ]; ***
    $arch_ext =~ s/.*\.//;   # strip all but the extension
    my $basename = $_;
    $basename =~ s/\..*//;  # strip off the extension
    $zip->extractMember( ($zip->memberNames)[ 0 ],
"$basename.$arch_ext" );
    unlink $_ or warn "Cannot delete $_: $!";
}, $dir );


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

Date: Tue, 19 May 2009 12:23:21 -0700
From: Jim Gibson <jimsgibson@gmail.com>
Subject: Re: Archive::Zip and correct extension (mixture of Word and PowerPoint files)?
Message-Id: <190520091223214948%jimsgibson@gmail.com>

In article <6gu5159pssd62mpdciatfarh4dp0g5hb6l@4ax.com>, Geoff Cox
<gcox@freeuk.com> wrote:

> On Tue, 19 May 2009 06:58:37 -0500, Tad J McClellan
> <tadmc@seesig.invalid> wrote:
> 
> >If you change the requirements, the code wil likely need to be changed.
> >
> >
> >>     ( my $name = $_ ) =~ s/\.zip$/.doc/i or return;
> >
> >
> >That is vestigial.
> >
> >It used to have a purpose, but now has no purpose.
> >
> >If you no longer want to replace with "doc", then you need to remove
> >code that replaces with "doc".
> 
> Tad,
> 
> I don't understand this. The code below gives an xls extension to the
> new files where the original zip file conatined an xls file, a ppt
> extension to those which contained a ppt file and a doc extension to
> those which contained a doc file and the same base name as the
> original zip files - but! It only works if the
> 
>    ( my $name = $_ ) =~ s/\.zip$/.doc/i or return;
> 
> line is present. If it isn't I get 
> 
> can't call method "memberNames" on an undefined value at ***
> 
> What is happening?!

That line, besides copying the value in $_ to $name and changing the
last four characters from '.zip' to '.doc', also executes the return
statement if the s/.../.../ substitute operator returns a false value.
In other words, if $_ does not end in '.zip', none of the remaining
statements are executed for that file.

So you are attempting to process non-zip files (or directories) as a
zip file, hence the later errors. You need to put something like:

  return unless /\.zip$/;

as the first line of your subroutine. With this line, only zip files
will be processed.


> use warnings;
> use strict;
> 
> use File::Find;
> use Archive::Zip;
> 
> my $dir = 'c:/a-temp2/docs';
> 
> find( sub {
>    ( my $name = $_ ) =~ s/\.zip$/.doc/i or return;
>     my $zip = Archive::Zip->new( $_ );
>     my($arch_ext) = ($zip->memberNames)[ 0 ]; ***
>     $arch_ext =~ s/.*\.//;   # strip all but the extension
>     my $basename = $_;
>     $basename =~ s/\..*//;  # strip off the extension
>     $zip->extractMember( ($zip->memberNames)[ 0 ],
> "$basename.$arch_ext" );
>     unlink $_ or warn "Cannot delete $_: $!";
> }, $dir );

-- 
Jim Gibson


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

Date: Tue, 19 May 2009 14:49:25 -0400
From: "Uri Guttman" <uri@PerlOnCall.com>
Subject: Re: beginner array of array... thank you all
Message-Id: <87ws8d9bd6.fsf@quad.sysarch.com>

>>>>> "TZ" == Ted Zlatanov <tzz@lifelogs.com> writes:

  TZ> On Mon, 18 May 2009 18:46:11 -0400 "Uri Guttman" <uri@PerlOnCall.com> wrote: 
  UG> you should learn it now. indexing fields by number and not name is prone
  UG> to bugs and misunderstanding later. this will bite you one day for sure
  UG> so learn hashes now. 

  TZ> Arrays use less memory, which can become important for some
  TZ> applications.  If I face that situation, I usually use constants to
  TZ> reduce the chance of error:

  TZ> use constant NAME => 0;
  TZ> use constant SURNAME => 1;

and that will bite you even harder when you need to add/delete/change
keys. trust me, i have seen it. if you want proof, look at the modules
which give ordering to hashes. all have major fails when things change.

if you are ram sensitive, usually you can find other ways to save space
other than using arrays over hashes.

  TZ> Obviously Uri's advice is valid too, it's just that in some cases you
  TZ> are forced to use arrays.

for some definition of forced! :)

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Tue, 19 May 2009 18:50:31 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: compiled perl question.
Message-Id: <Xns9C1096F95B40Fasu1cornelledu@127.0.0.1>

Jürgen Exner <jurgenex@hotmail.com> wrote in
news:mrm515t6050f4kbub8p5dsgo9jk57hev1l@4ax.com: 

> DaLoverhino <DaLoveRhino@hotmail.com> wrote:
>>On May 19, 11:57 am, Lawrence Statton <yankeeinex...@gmail.com> wrote:
>>> DaLoverhino <DaLoveRh...@hotmail.com> writes:
>>> > On May 19, 11:45 am, Jürgen Exner <jurge...@hotmail.com> wrote:
>>> >> DaLoverhino <DaLoveRh...@hotmail.com> wrote:
>>> >> >I read somewhere that perl gets compiled to bytecodes and is run
>>> >> >in a perl virtual machine similar to Java.  Is that true?  What
>>> >> >I'd like to do is give a bytecode compiled version of my perl
>>> >> >script, but not the script itself, to prevent copying or
>>> >> >modifying it.  I'd rather not run the script through an
>>> >> >obfuscator. 
>>>
>>> >> perldoc -q hide
>>>
>>> >>   How can I hide the source for my Perl program?
>>>
>>> >> jue
>>>
>>> > Thanks, unfortunately, I don't have perldoc on my system, and I am
>>> > not the sys admin of our unix boxes.  Can you copy/past the output
>>> > of that command for me onto this thread, please?  Thanks.
>>>
>>I guess because perldoc is 32mb file, and I don't have the space on my
>>account.  
> 
> I think you meant 32MB, not 32 millibar.
> 
> Owwww, wait a second. Are we living in the same year? 1TB drives go
> for just over about 100$ US nowadays. That translates into those 32MB
> costing about 0.32 Cent US. Are you saying your employer or university
> or whoever owns those machines cannot afford 1/3 of a cent to have a
> working Perl installation? But they can afford their employees writing
> programs without having the documentation they need.
> I had not idea the economic crisis was that bad.

I guess it would be too much to hope that he would have access to some 
other computer where he could install the corresponding version of Perl 
with documentation and everything.

I mean, computers are giant water-cooled cabinets housed in a big 
concrete windowless building. I did some work in one of those about 25 
years ago.

There is no excuse for not having a system with a full installation of 
Perl even if your sysadmins are willing to live with a completely borked 
installation of Perl.

Sinan

-- 
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://www.rehabitation.com/clpmisc/


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

Date: Tue, 19 May 2009 16:01:54 -0400
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: compiled perl question.
Message-Id: <m1y6sskgjx.fsf@dot-app.org>

Jürgen Exner <jurgenex@hotmail.com> writes:

> I don't understand. perldoc is part of Perl. If it isn't installed then
> your Perl installation is broken. Maybe fix it?

That kind of brokenness is sadly common among Linux distributions. Many
of them separate Perl's 'perldoc' script and '*.pod' files into a -dev
package that must be installed separately.

> Why don't you create your own installation of Perl instead of using a
> broken one?

Completely agreed. Whether or not perldoc is available on the deployment
server is irrelevant, because one should only be using one's own desktop
development machine for that anyway.

sherm--

-- 
My blog: http://shermspace.blogspot.com
Cocoa programming in Perl: http://camelbones.sourceforge.net


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

Date: Tue, 19 May 2009 21:35:17 +0100
From: RedGrittyBrick <RedGrittyBrick@SpamWeary.foo>
Subject: Re: Outputting to PDF simply?
Message-Id: <geqdnW7O7JibhY7XnZ2dnUVZ8vGdnZ2d@bt.com>

bobm3@worthless.info wrote:
> Fellow mongers...
> 
> I've searched CPAN as well as googled for numerous pdf creator
> utilities.  I've read about some of the perl modules available (i.e.:
> PDF::API, PDF::Report, PDF::ReportWriter, etc).
> 
> I think all these are overkill for what I am trying to do.  Perhaps
> some of you can recommend a workable solution that I can research and
> hopefully incorporate.  
> 
> I have some perl programs that create business reports using the
> write(format) feature (which works very well, BTW) and they are now
> creating text output.  I would like to have those converted to or
> output directly as PDF's so they can be more easily opened for
> viewing, searching  and printing with Acrobat.
> 
> Any recommendations as to what my fill this bill with the least amount
> of changes?
> 
> Thanks all for your kind and knowledgeable advice!

I mostly use the approach suggested by A. Sinan Unur.

Sometimes I write Postscript directly and, if needed, convert the result 
to PDF on the fly.

Sometimes I use Perl to insert generated Postscript into a Postscript 
template.

My basic approach is to use Perl to convert
   Lorem ipsum dolor sit amet
   consectetur adipisicing elit
   sed do eiusmod tempor incididunt
to
   (Lorem ipsum dolor sit amet) newline
   (consectetur adipisicing elit) newline
   (sed do eiusmod tempor incididunt) newline

E.g.

    #!perl
    use strict;
    use warnings;

    my $template = $ARGV[0] || 'template.ps';
    my $psname = $ARGV[1] || 'report.ps';
    open my $in, '<', $template or die "can't open '$template' - $!";
    open my $out, '>', $psname or die "can't write '$psname' - $!";
    while (my $tline=<$in>) {
      print $out $tline;
      if ($tline =~ /insert data here/) {
        for my $dataline (<DATA>) {
            print $out "($dataline) newline\n";
        }
      }
    }
    close $out;
    close $in;
    __DATA__
    Lorem ipsum dolor sit amet
    consectetur adipisicing elit
    sed do eiusmod tempor incididunt

In the above, 'newline' is an operator I've defined in the Postscript 
template: This saves most of the tedious manual calculating of x and y 
positions.

   %!PS
   %
   % Very basic example Postscript template, with example text data
   %
   /inch { 72 mul } def           % Postscript's units are 1/72 inch
   /leftmargin 1 inch def
   /lineheight 1 inch 6 div def
   /newline {                     % Stack contains text
     show                         % <empty>
     leftmargin                   % 72
     currentpoint                 % 72 x y
     exch                         % 72 y x
     pop                          % 72 y
     lineheight                   % 72 y 12
     sub                          % 72 y'
     moveto                       % <empty>
   } def

   /Courier 10 selectfont
   1 inch 8 inch moveto           % origin is bottom left

   (This is a page heading) newline
   (and a subheading) newline
   () newline

   % insert data here

   showpage

Then I convert the resulting Postscript file to PDF as needed.

If you know Postscript it isn't hard to extend my newline operator above 
to check for bottom of page and start a new one. Google for Ben 
Cranston's asciiprint.ps - it copes with multiple pages, tabs etc.

The above approach has some advantages over a2ps or enscript but, If I 
don't need a complicated mixture of fonts, boxes, lines, custom page 
headers etc, the latter two are what I would use if they are available.

-- 
RGB


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

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


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