[11827] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 5427 Volume: 8

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Apr 20 10:07:32 1999

Date: Tue, 20 Apr 99 07:00:19 -0700
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, 20 Apr 1999     Volume: 8 Number: 5427

Today's topics:
    Re: Another beginner question (Daniel Beckham)
    Re: Can i run cgi in Win95 of my PC ?? (Daniel Beckham)
    Re: CGI/Telnet?? (Zach Trexler)
        circular references, destructor redux martin@cutup.de
        How to determine if a specified disk drive exists lingane@my-dejanews.com
    Re: How to join two hashes? (Larry Rosler)
        Net::SSL on Windows NT <nospam@here.com>
    Re: Perl cookbook ... (Larry Rosler)
        Please Help !!!!! ranjeev_s._pamnani@hud.gov
    Re: Scripting language with LFN support? <nospam.shardy@seanet.com>
    Re: scriptlet to add line breaks at even multiples? (Larry Rosler)
    Re: Tool Reuse Considered Beneficial (was: New FAQ: How (Daniel Beckham)
    Re: Tool Reuse Considered Beneficial (was: New FAQ: How (Greg Andrews)
        Unix files in MacPerl <graphics@alphacrc.com>
    Re: Unix files in MacPerl <tchrist@mox.perl.com>
    Re: Unix files in MacPerl (I R A Aggie)
    Re: Unix files in MacPerl <graphics@alphacrc.com>
    Re: Wanted: some help for a perl script (Daniel Beckham)
    Re: Wanted: some help for a perl script (Daniel Beckham)
        Special: Digest Administrivia (Last modified: 12 Dec 98 (Perl-Users-Digest Admin)

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

Date: Tue, 20 Apr 1999 08:29:05 -0500
From: danbeck@scott.net (Daniel Beckham)
Subject: Re: Another beginner question
Message-Id: <MPG.11863d20f6688569896a0@news.idt.net>

Bad advice, he can't use compile time switches, his scripts will be 
_less_ portable and personally, I think it just doesn't look like a perl 
script without the ever present:

#!/usr/bin/perl

(of course your command line will always vary)

In article <371B28FE.104E12A6@tidalwave.net>, kgenus@tidalwave.net 
says...
> ibrew wrote:
> 
> > I get an error on the following chunck of code taken directly from the
> > "Learning Perl" book, pages 8-9
> >
> > !#c:/perl/bin/perl
> > @words = qw(camel llama alpaca);
> >
> > I get a compile error on the second statement;
> > "Can't modify not in scalar assignment on line two..."
> >
> > i would be thankful for any help.
> 
> #! no !# ...
> 
> Also, if you are using windows, you do not need that line ... you should
> set up associations and file types using FTYPE and ASSOC commands from
> the command prompt.
> 
> -g
> 
> 


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

Date: Tue, 20 Apr 1999 09:27:10 -0500
From: danbeck@scott.net (Daniel Beckham)
Subject: Re: Can i run cgi in Win95 of my PC ??
Message-Id: <MPG.11864abd634aeadb9896a6@news.idt.net>

[This followup was posted to comp.lang.perl.misc and a copy was sent to 
the cited author.]

As you might notice, you got a few somewhat "ugly" responses.  You 
received these because:

1) You used funny characters in your name.

2) You asked your question twice.

3) CGI is not a program, it's a spec or implemenation of a specific 
process.

4) You question has nothing what-so-ever to do with perl.

I would check out www.apache.org for a web server and www.activestate.com 
for a distribution of perl that will work on your machine. (if in fact, 
you wish to write your CGI script in perl)  I would also take a look at 
the cgi newsgroups available on your news server for better help as far 
as CGI scripting goes.

Regards,

Daniel

In article <1107_924346326@austin>, austin95002887@yahoo.com says...
> 
> 
> Can i run cgi in Win95 of my PC ??
> 
> ( I have no Network Card, and i really do not want to buy one 
> because of this reason. )
> 
> Because, i want to test the cgi program wriiten by myself before 
> upload the cgi to server.
> 
> 
> 
> HE and SHE - %L )M &o  :
> ======================
> http://start.cgirealm.com/heshe1/
> 
> 



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

Date: 20 Apr 1999 13:59:52 GMT
From: 00205613@bigred.unl.edu (Zach Trexler)
Subject: Re: CGI/Telnet??
Message-Id: <7fi18o$jpr@crcnis3.unl.edu>

Abigail (abigail@fnx.com) wrote:
: Z Trexler (ztrexler@heehaw.com) wrote on MMLVIII September MCMXCIII in
: <URL:news:7fgiqj$d1j$1@nnrp1.dejanews.com>:
: {} I own a WebTV, but I am also an intermediate web developer at a university. 
: 
: Goodie. An intermediate web developer. I guess we get a well phrased, to
: the point question, and not a "I wanna do this, and I've no clue where to
: start". After all, you are intermediate. And you own a WebTV.

Hey, you're quick.
 
: {} I want to find a way to write a CGI script that allows me to telnet from my
: {} WebTV. Keep in mind that WebTV has no java capabilities, and obviously no
: {} built-in telnet program.
: 
: I suggest you start with studying the telnet protocol, and while you are
: doing that, figure out which of the languages supported by WebTV you want
: to write your telnet client in.
: 
: What do you say? WebTV doesn't have support for any language? Well.....
: then maybe it's impossible.

No, it's not impossible.  Where did I say WebTV has no support for 
_any_ language?   


: {} Apparently someone actually has created one before, at hypermart.com, but it
: {} was removed because of heavy traffic.  I never saw it, and have only heard
: {} about it.
: 
: Uhm, yeah. Sure. Did you write hypermart.com about it?

UHM, no, I did not write Hypermart about it.  How would they know 
about one little site that no longer exists?  Anyways, I was able to 
find someone who had a backup copy of it on an angelfire site.  Try 
this:

http://demo69.hypermart.net/Telnet.pl

And you'll see that you can attempt to log-in to a telnet application, 
or something of the sort.  Try logging on to a remote server, though, 
and all you get is connected and then promptly disconnected.  Try 
this:

http://demo69.hypermart.net/Unix.pl

And you'll get a Unix prompt, albeit limiting.  

: 
: Now, what was your Perl question again?

There has to be a way to alter this script to get it to continually 
send input and output back and forth, without it disconnecting.  You 
can either help me, mock me, or ignore me.  It seems you've chosen to 
mock me, which I expected because I included the magical word "WebTV" 
in my post. 


: 
: Abigail
: -- 
: srand 123456;$-=rand$_--=>@[[$-,$_]=@[[$_,$-]for(reverse+1..(@[=split
: //=>"IGrACVGQ\x02GJCWVhP\x02PL\x02jNMP"));print+(map{$_^q^"^}@[),"\n"

Cute.

Trex

--
 




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

Date: Tue, 20 Apr 1999 13:29:12 GMT
From: martin@cutup.de
Subject: circular references, destructor redux
Message-Id: <7fhvf3$ip5$1@nnrp1.dejanews.com>

Hi,

I basically have four questions:

1. In a data structure where an object holds a back reference to its parent,
how is the allocated memory to be reclaimed?

2. If the solution to the "how" question in 1. lies in a container object, why
is that container object necessary to solve the problem?

3. In the case of "is-a" inheritance, how is nested destruction to be dealt
with?

4. How can I monitor the memory allocated by Perl?


By way of absolution, I would like to remark that I have checked out the
following sources which all address the first three questions:

The Camel Book
3 posts by Tom Christiansen:
"Coping with Circular Data Structures"
"Re:Inheriting the same object twice"
"Re:Deleting objects in Perl"

Even though I found all four sources to be very informative I still don't have
the feeling that the above questions are (completely) answered. One reason for
this feeling could, of course, simply be my own stupidity (not trying to fake
humility, it's a very real threat). However, since, at least theoretically,
it's possible that there are some open ends left over I would like to pose the
questions in greater detail:


Questions 1. and 2. (regarding circular references)

#___________________________________
package Dad;
use Kid;
sub new{
      my($type) = @_;
      my $self = {};
      bless $self, $type;
      $$self{kid} = Kid->new($self);
      return $self;
}

#____________________________________
package Kid;
sub new{
      my($type,$dad) = @_;
      my $self = {};
      bless $self, $type;
      $$self{dad} = $dad;
      return $self;
}

#____________________________________
package main;
use Dad;

my $dad = Dad->new();
undef $dad;

If you run the above program in the debugger and print out $dad you get
something similar to the following:

Dad=HASH(0x89a3cc)
   'kid' => Kid=HASH(0x88f3bc)
      'dad' => Dad=HASH(0x89a3cc)
         -> REUSED_ADDRESS


Since I know no better way to check out what's going on in RAM when Perl is
running (s. question 4) my assumptions are based on this and are as follows:

After the line "$dad = Dad->new();" memory has been reserved for:
1. the string "$dad"
2. the address 0x88f3bc, to which "$dad" refers
3. the actual Dad HASH
4. the address 0x89a3cc, to which $dad's {kid} will refer
5. the actual Kid HASH
6. kid's hash key {dad} which refers to the address 0x88f3bc
   (reusing the Dad HASH adress)
7. $dad's hash key {kid} which refers to the address 0x89a3cc

So, at least to my current understanding, this is the dilemma: If I undef
$dad, the memory allocated in steps 2. - 7. will remain reserved since kid's
hash key {dad} still refers to the address of the Dad HASH (step 6. =
circular reference).

So far, so good.

The suggested solution to this problem is to instantiate a class that wraps
this circular structure and can undef it "from the outside". In a lot of my
classes this would lead to an added level of structural complexity I'm not too
keen on. Before someone says: "This isn't about your personal _preferences_,
yogurthead!" I would like to ask:

Why isn't it enough for the $dad object to destroy the {kid} object's back
reference like so:

# in Dad.pm
sub DESTROY{
      my($self) = @_;
      undef $$self{kid}{dad};
}

This should remove the reference created in step 6, leaving the reference
created in step 1 as the only one pointing to the Dad HASH. Destroying that
reference leads to the destruction of the Dad HASH which, in turn, contains
the only reference to the Kid HASH and everything is cleaned up nice and
tidy, or isn't it?



Question 3. (regarding destructors)

#___________________________________
package Granddad;
sub new{
      my($type) = @_;
      my $self = {};
      bless $self, $type;
      $$self{granddad_att} = "about granddad";
      return $self;
}

#___________________________________
package Dad;
use Granddad;
@ISA = qw(Granddad);
sub new{
      my($type) = @_;
      my $self = Granddad->new();
      bless $self, $type;
      $$self{dad_att} = "about dad";
      $$self{circ_structure} = {};
      $$self{circ_structure}{next} = $$self{circ_structure};
      return $self;
}

sub DESTROY{
      my($self) = @_;
      # code to clean up $$self{circ_structure} is
      #    unquestionably necessary
      # is code to clean up {dad_att} necessary?
      # what's with {granddad_att}?
}

#____________________________________
package main;
use Dad;

my $dad = Dad->new();
undef $dad;



A couple of (sub-)questions arise:

Since I need to define a DESTROY method to take care of the {circ_structure}
do I also need to explicitly undef all elements of the Dad HASH or will these
be taken care of by Perl since - after "undef $dad" - there will be no more
references to the Dad HASH?

How do I make sure the memory allocated for the string "about granddad"
(stored under {granddad_att}) will be reclaimed?


The Camel Book states that Perl does not do nested destruction when you write
custom destructors. There are more explicit statements to the effect
that "SUPER::DESTROY" would have to be called, unfortunately without code
examples so that dolts like me can assimilate exactly what the ancestor method
is supposed to do. Why isn't it obvious? you may be asking.

To me it is not obvious because I assume that blessing an object simply means
setting the appropriate (internal) field of the relevant HASH (or whatever)
to signify that the object belongs to the class passed to the bless operator.
Tom Christiansen has stated that in the case of "is-a" inheritance there is
no data aggregation. I take that to mean that reblessing the object simply
overwrites the object's "class field" with the string for the new class.

So, in the example above, I assume that the HASH that was born as a Granddad
is completely and totally turned into a Dad HASH in the Dad::new() method. In
other words, a field like "granddad_att" is in no way related to the Granddad
class. When the reference to the Dad HASH is destroyed by calling "undef
$dad" and if Dad manages to clean up the {circ_structure} the way should be
clear for complete memory reclamation.


This is the way I think it _could_ work:

Calling DESTROY on the Dad level completely blocks all Perl GC regarding the
Dad object. So a fitting destructor could look like this:

sub DESTROY{
      my($self) = @_;
      # code to take care of {circ_structure} plus:
      undef $self;
}

But then SUPER::DESTROY would be completely superfluous, or would it?
Or does it play a role only when there is a chance of circular structures
having been created on the parent level?



Question 4 (regarding memory diagnosis)
I obviously could have considerably shortened this post if I had had a chance
to see how Perl internally handles all this stuff. Any references to
appropriate tools would be greatly appreciated. On the other hand, one or two
clear-cut code snippets would be even better.



Thanks a lot to anyone who actually _reads_ this gargantuan post,
Thanks even more to people who can give me a couple of hints on how to avoid
endless core dumps when I switch to mod_perl and the days of thread-closing gc
are over :-),



Martin

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    


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

Date: Tue, 20 Apr 1999 13:22:53 GMT
From: lingane@my-dejanews.com
Subject: How to determine if a specified disk drive exists
Message-Id: <7fhv39$ihp$1@nnrp1.dejanews.com>

I need to determine if a disk drive exists.  The ways I have been trying all
return errors if it doesn't exist or it doesn't have a disk in it.

I need to know if it is a legitimate drive for the the machine.  I need to be
able to do this on 95/98 and NT platforms.

Also, if possible I need to do it without having to add in predefined Perl
functions.  This script must take up as little space as possible.

Thanks

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    


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

Date: Tue, 20 Apr 1999 06:53:33 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: How to join two hashes?
Message-Id: <MPG.118626b66133f4d09898ec@nntp.hpl.hp.com>

[Posted and a courtesy copy sent.]

In article <371C5AB4.84699EC9@gredos.cnb.uam.es> on Tue, 20 Apr 1999 
12:45:09 +0200, Federico Abascal <fabascal@gredos.cnb.uam.es> says...
> does anybody knows how to join two hashes, if possible? (The two hashes
> have some keys similar)

The hash-slice approach is efficient.  The values of the shared keys 
from the second hash overwrite those of the original hash.

  @hash1{keys %hash2} = values %hash2;

-- 
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Tue, 20 Apr 1999 09:21:46 -0400
From: "Link Confirm" <nospam@here.com>
Subject: Net::SSL on Windows NT
Message-Id: <7fhv46$gct$1@antiochus.ultra.net>

Hi all...

If anyone has successful installing Net::SSL on an NT machine, and they'd
like to walk me through it, I'd be very greatful!!! I'm just not having any
luck with this at all....

Thanks very much,

Richie

email: richie(AT)roxy.com

replace (AT) with @




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

Date: Tue, 20 Apr 1999 06:28:34 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Perl cookbook ...
Message-Id: <MPG.118620e07729457b9898ea@nntp.hpl.hp.com>

[Rearranged for readability.  Damned M$ so-called newsreader puts quoted 
material at the end.  Sheesh!]

In article <7fhjn6$mlv$1@the-fly.zip.com.au> on Tue, 20 Apr 1999 
20:08:33 +1000, Bruce Woodward <bruceb3@zipworld.com.au> says...
> Ron Reidy wrote in message <371BD678.1DC4D87D@uswest.net>...
 ...
> >The cookbook example is locate on page 651:
> >
> >open(SENDMAIL, "|/usr/lib/sendmail -oi -t -odq")
> >    or die "Can't fork for sendmaile: $!\n";
> 
> Should'nt the "or" be "||" so it would read ....
> open(SENDMAIL, "|/usr/lib/sendmail -oi -t -odq") || die "Can't fork for
> sendmail: $!\n"

As long as the parentheses are there, they are identical.  A perhaps 
useful style is to use && and || for Boolean calculation, and 'and' and 
'or' to signify flow-control change based on a Boolean value.

> >We could not force the die statement to fail.  Is it me/him, or is there
> >something missing here.

The die statement is triggered on failure to fork, not failure to 
execute the pipe successfully.  That is determined by testing the 
corresponding 'close'.

-- 
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Tue, 20 Apr 1999 13:43:08 GMT
From: ranjeev_s._pamnani@hud.gov
Subject: Please Help !!!!!
Message-Id: <7fi09c$jmk$1@nnrp1.dejanews.com>

Hi,

I am new to perl and want to execute a unix command (mailx) from within perl.
Can anyone let me know how to invoke a unix command from within a perl script?

Thanks in advance,
Ranjeev

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    


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

Date: Tue, 20 Apr 1999 06:32:02 -0700
From: "steve hardy" <nospam.shardy@seanet.com>
Subject: Re: Scripting language with LFN support?
Message-Id: <7fhvk6$852$1@q.seanet.com>


>> My question is quite simple:
>> Is there a scripting language in NT
>> (akin to shell, batch, perl etc.) that
>> supports long file names?
>>
>> NT's DOS CLI only supports long file
>> names for built in commands (cd, md, rd,
>> copy, ren, dir, type...) which makes it worthless.


hmm. Where does this assertion comes from?  CMD.exe does support LFN's. If
the file name happens to include embedded delimiters (e.g., whitespace), the
name needs to be surrounded by double quotes.
[as in: NOTEPAD "my long filename.txt"] Of course, if you're working with 16
bit Applications (or apps written with a 16 bit mindset), those tend not to
support LFN's regardless of shell.

>> I'm hoping to write platform independent
>> install scripts that can manage files
>> on both NT and UNIXwith minimal change.
>
>I think perl is the best way to do it.


i agree that perl is a good choice, where platform independence is the
criterion.

steve




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

Date: Tue, 20 Apr 1999 06:41:14 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: scriptlet to add line breaks at even multiples?
Message-Id: <MPG.118623cc7b18f09e9898eb@nntp.hpl.hp.com>

In article <7fhldr$a2n$1@nnrp1.dejanews.com> on Tue, 20 Apr 1999 
10:37:47 GMT, Ronny <ronald_f@my-dejanews.com> says...
+ In article <371C263B.A747D8FA@counsel.net>,
+   "Counsel.Net" <webmaster@counsel.net> wrote:
+ > Is there a simple solution for adding linebreaks on long fields?  I
+ > have a field fed by a web form which I want to wrap at about 80
+ > characters. Wrapping the field via the HTML input textarea is not an
+ > option here.  I figure I want the data to wrap at the first word
+ > break less than 80 characters for each line.  Is there a simple
+ > substitution operation I can do on this data that will do this for
+ > me?
+ 
+ Not simple maybe.....
+ 
+ $field_with_breaks="";
+ while($your_long_field =~ s/(.{1,80})\s//)
+ {
+   $field_with_breaks .= "$1\n";
+ }

A simple substitution operation using that regex would be simply:

  $your_long_field =~ s/(.{1,80})\s/$1\n/g; 

-- 
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Tue, 20 Apr 1999 09:00:16 -0500
From: danbeck@scott.net (Daniel Beckham)
Subject: Re: Tool Reuse Considered Beneficial (was: New FAQ: How can I read in an entire file all at once?)
Message-Id: <MPG.11864468114051249896a4@news.idt.net>

In article <m3u2ucbb33.fsf@moiraine.dimensional.com>, 
dgris@moiraine.dimensional.com says...
> 
> I wouldn't use perl to do those things any more than I'd use C++ or
> assembler to do those things.  When I need to write a shell script, I
> use sh.  When I need to write a program, I usually use perl. :-)

Ah, but the entire reason you are using perl is for it's power, 
flexibility and ease of use.  Why bother fumbling with a shell script 
when you need perl?  Just because of a puritan view of using system 
tools?  It seems that you are making it hard on yourself.

> If you want reliable code that can be moved from environment to
> environment then you _shouldn't_ use the toolkit.  
> 
> Period.  Ever.  

Who wants that?  I have a few systems types here and I generally make 
sure I have the tools I need.  Are you writing software for 
redistribution?

> You have no way of knowing exactly how the executable file
> `cat' will act on any arbitrary system.  For all you know, the
> user has a veterinary diagnostic program for felines under
> that name.  Hope it does what you expect.

Why would anyone shell 'cat'?  It's quicker to just do it in perl, but as 
mentioned by Tom, viewing a post script file is not.

Daniel



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

Date: 20 Apr 1999 06:26:26 -0700
From: gerg@shell1.ncal.verio.com (Greg Andrews)
Subject: Re: Tool Reuse Considered Beneficial (was: New FAQ: How can I read in an entire file all at once?)
Message-Id: <7fhva2$ain$1@shell1.ncal.verio.com>

danbeck@scott.net (Daniel Beckham) writes:
>
>Why would anyone shell 'cat'?  It's quicker to just do it in perl, but as 
>mentioned by Tom, viewing a post script file is not.
>

Ever looked at your Unix box's startup scripts?  The author of
those wanted to replicate 'cat' in a shell script.  The reason
is /usr/bin/cat lives on a mounted partition, and one or two of
the startup scripts need to read the contents of files before
/usr is mounted.  Particularly if /usr is mounted from an NFS
server - the network interfaces must be brought up first.

Now you may intended to ask "Why would *an ordinary user* shell
'cat'?"  Aside from a desire to not fork a child process in order
to simply read a file, I don't know.

There might only be a few script authors who would pay attention
to such things, but the answer to your stated question is "reasons
do exist."

  -Greg


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

Date: Tue, 20 Apr 1999 14:06:34 +0000
From: graphics <graphics@alphacrc.com>
Subject: Unix files in MacPerl
Message-Id: <371C89EA.639E5DEC@alphacrc.com>

Hello,

Just a quick and simple question...

I regularly swap Perl scripts between Mac and Linux/Unix and would like
to know if there's a simple way of getting MacPerl 5.2.0r4 to read text
files with Unix-style line endings, \n as opposed to the Mac's \r ?

It's getting tedious using BBEdit to change them each time. I could
write a utility to do it for me but it would be nice not to have to
worry about it...

Thanks in advance.

Jason

jason.holland@dial.pipex.com


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

Date: 20 Apr 1999 07:20:42 -0700
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: Unix files in MacPerl
Message-Id: <371c7f2a@cs.colorado.edu>

 [courtesy cc of this posting sent to cited author via email]

In comp.lang.perl.misc, 
    jason.holland@dial.pipex.com writes:
:I regularly swap Perl scripts between Mac and Linux/Unix and would like
:to know if there's a simple way of getting MacPerl 5.2.0r4 to read text
:files with Unix-style line endings, \n as opposed to the Mac's \r ?

I'm afraid that you're slightly confused.  Unix uses "\n" to end a line.
Mac uses "\n" -- not "\r" -- to end a line.  *Everything* does -- by
definition.  However, while the virtual line-terminator is invariant,
its physical representation varies.

You'll find that
    http://language.perl.com/ppt/
probably has what you want.   

Here's a copy of mac2cpm, unix2mac, etc.

--tom

#!/usr/bin/perl -w
# nlcvt - convert newline notations
# Tom Christiansen, 9 March 1999

#   "The most brilliant decision in all of Unix was 
#    the choice of a *single* character for the 
#    newline sequence.      --Mike O'Dell, only half jokingly

use strict;

END {
    close STDOUT            || die "$0: can't close stdout: $!\n";
    $? = 1 if $? == 255;    # from die
} 

my(
    $src,		# input format style
    $dst,		# output format style
    %format,		# table of conversion
    $errors, 		# file input errors
);

$errors = 0;

%format = (

    # the good...

    "unix"		=> "\cJ",	# CANON
    "plan9"		=> "\cJ",
    "inferno"		=> "\cJ",
    "linux"		=> "\cJ",	# some people don't get it
    "bsd"		=> "\cJ",	# some people don't get it
    "be"		=> "\cJ",
    "beos"		=> "\cJ",

    # the not so good, but still ok...

    "mac"		=> "\cM", 	# CANON
    "apple"		=> "\cM",
    "macintosh"		=> "\cM", 

    # and the really unbelievably idiotic...

    "cpm"		=> "\cM\cJ",	# CANON
    "cp/m"		=> "\cM\cJ",	# could be in first arg
    "dos"		=> "\cM\cJ",
    "windows"		=> "\cM\cJ",
    "microsoft"		=> "\cM\cJ",
    "nt"		=> "\cM\cJ",
    "win"		=> "\cM\cJ",

);

sub usage {    
    warn "$0: @_\n" if @_;
    my @names = sort { 
			$format{$a} cmp $format{$b} 
				     ||
				$a cmp $b 
    } keys %format;
    my $fmts = "@names";
    print STDERR "usage: $0 src2dst [file ...]\n";
    format STDERR = 
    where src and dst are both one of:
~~      ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
	$fmts
 .
    write STDERR;
    exit(1);
}

($src, $dst) = ($0 =~ /(\w+)2(\w+)/);

usage("insufficient args") unless @ARGV || ($src && $dst);

if (@ARGV && $ARGV[0] =~ /(\w+)2(\w+)/) {
    ($src, $dst) = ($1, $2);
    shift @ARGV;
} 

usage("no conversion specified") unless $src && $dst;

usage("unknown input format: $src")  unless $/ = $format{lc $src};
usage("unknown output format: $dst") unless $\ = $format{lc $dst};

binmode(STDOUT);

unshift @ARGV, '-' unless @ARGV;

for my $infile (@ARGV) {
    unless (open(INPUT, $infile)) {
	warn "$0: cannot open $infile: $!\n";
	$errors++;
	next;
    } 


    binmode(INPUT);

    unless (-T INPUT) {
	warn "$0: WARNING: $infile appears to be a binary file.\n";
	$errors++;
    } 

    while (<INPUT>) {
	unless (chomp) {
	    $errors++;
	    warn "$0: WARNING: last line of $infile truncated, correcting\n";
	} 
	print;
    } 

    unless (close INPUT) {
	warn "$0: cannot close $infile: $!\n";
	$errors++;
	next;
    } 
} 

exit ($errors != 0);

__END__

=head1 NAME

nlcvt - convert foreign line terminators

=head1 SYNOPSIS

B<nlcvt> I<src>2I<dst> [I<file> ...]

B<unix2mac> [I<file> ...]

B<unix2cpm> [I<file> ...]

B<cpm2unix> [I<file> ...]

B<cpm2mac> [I<file> ...]

B<mac2unix> [I<file> ...]

B<mac2cpm> [I<file> ...]

=head1 DESCRIPTION

Mike O'Dell said, only half-jokingly, that "the most brilliant decision
in all of Unix was the choice of a I<single> character for the newline
sequence."  But legacy systems live on past their days, and these programs
can help that.  Note, however, that if you've downloaded a binary file in
"text" mode rather than "binary", your mileage may vary.

The B<nlcvt> program, or any of its many aliases, is a filter to convert
from one system's notion of proper line terminators to that of another.
This usually happens because you've downloaded or otherwise directly
transferred a text file in so-called "binary" rather than "text" mode.

Unix format considers a lone Control-J to be the end of line.  Mac format
considers a lone Control-M to be the end of line.  The archaic CP/M
format considers a Control-M and a Control-J to be the end of line.

This program expects its first argument to be of the form I<src>2I<dst>,
where I<src> and I<dst> are both one of B<unix>, B<mac>, or B<cpm>.
(That's speaking canonically--many aliases for those systems exist: call
B<nlcvt> without arguments to see what names are accepted.)  The converted
data is written to the standard output.  B<nlcvt> does I<not> do 
destructive, in-place modification of its source files.  Do this
instead:

    cpm2unix < file.bad > file.good 
    mv file.good file.bad

This program can also be called by the name of the conversion itself.
Just create links to the B<nlcvt> program for each systems, and the
program use its own name to determine the conversion.  For example:

    #!/usr/bin/perl
    # make nlcvt links
    chomp($path = `which nlcvt`);
    @systems = qw(unix mac cpm);
    for $src (@systems) {
	for $dst (@systems) {
	    next if $src eq $dst;
	    ln($path, "${src}2$dst") || die $!;
	} 
    } 

=head1 DIAGNOSTICS

Any of the following diagnostics cause B<nlcvt>
to exit non-zero.

=over

=item C<insufficient args>

You called the program by its canonical name, 
and supplied no other arguments.
You must supply a conversion argument.

=item C<no conversion specified>

Neither the name of the program nor its
first argument were of the form I<src>2I<dst>.

=item C<unknown input format: %s>

The specified input format, C<%s>, was unknown.
Call B<nlcvt> without arguments for a list of
valid conversion formats.

=item C<unknown output format: %s>

The specified output format, C<%s>, was unknown.
Call B<nlcvt> without arguments for a list of
valid conversion formats.

=item C<cannot open %s: %m>

The input file C<%s> could not be opened for the reason
listed in C<%m>.

=item C<cannot close %s: %m>

The input file C<%s> could not be close for the reason
listed in C<%m>.  This error is rare.

=item C<can't close stdout: %m>

The filter could not finish writing to its standard output for the
reason listed in C<%m>.  This could be caused by a full or temporarily
unreachable file system.

=item C<WARNING: last line of %s truncated, correcting>

Text files contain zero or more variable-length, newline-terminated
records.  Occasionally, the final record terminator is missing,
perhaps due to an incomplete transfer, perhaps due to an aberrant
I<emacs> user.  A newline sequence appropriate to the destination
system is appended.  This would be a valid use of a I<unix2unix>
conversion.  And no, you can't call it as B<emacs2vi>.

=item C<WARNING: %s appears to be a binary file>

Perl's C<-T> operator did not think the input file was a text file.
The conversion is still performed, but is of dubious value.  If
the file really was binary, the resulting output may be mangled.
Garbage in, garbage out.

=back

=head1 AUTHOR

Tom Christiansen, I<tchrist@perl.com>.

=head1 COPYRIGHT

This program is copyright (c) 1999 by Tom Christiansen.

This program is free and open software. You may use, copy, modify,
distribute, and sell this program (and any modified variants) in any
way you wish, provided you do not restrict others from doing the same.
-- 
    echo "ICK, NOTHING WORKED!!!  You may have to diddle the includes.";;
            --Larry Wall in Configure from the perl distribution


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

Date: 20 Apr 1999 13:47:21 GMT
From: fl_aggie@thepentagon.com (I R A Aggie)
Subject: Re: Unix files in MacPerl
Message-Id: <slrn7hp1g5.pqs.fl_aggie@stat.fsu.edu>

On 20 Apr 1999 07:20:42 -0700, Tom Christiansen
<tchrist@mox.perl.com>, in <371c7f2a@cs.colorado.edu> wrote:

+  [courtesy cc of this posting sent to cited author via email]

+ In comp.lang.perl.misc, 
+     jason.holland@dial.pipex.com writes:
+ :I regularly swap Perl scripts between Mac and Linux/Unix and would like
+ :to know if there's a simple way of getting MacPerl 5.2.0r4 to read text
+ :files with Unix-style line endings, \n as opposed to the Mac's \r ?

+ definition.  However, while the virtual line-terminator is invariant,
+ its physical representation varies.

+ You'll find that
+     http://language.perl.com/ppt/
+ probably has what you want.   

Major overkill...if Jason will simply set his ftp client to transfer his
perl code in ASCII mode and not binary, this "problem" will go away.

James


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

Date: Tue, 20 Apr 1999 15:00:36 +0000
From: graphics <graphics@alphacrc.com>
Subject: Re: Unix files in MacPerl
Message-Id: <371C9693.EFAFF436@alphacrc.com>

Thanks Tom,

Don't worry, I know what I mean, it's just that I don't always write it
down so that others know what I mean. Confusing?

Bye!

jason.holland@dial.pipex.com


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

Date: Tue, 20 Apr 1999 09:30:31 -0500
From: danbeck@scott.net (Daniel Beckham)
Subject: Re: Wanted: some help for a perl script
Message-Id: <MPG.11864b8678026e8b9896a7@news.idt.net>

I won't do it for free, but you are welcome to hire me to do the 
consulting work for you.

I can be reached privately at:

danbeck@scott.net

BTW, I will need half the fee in advance and the other half after I've 
completed the task.. to make it safe for everyone.

Regards,

Daniel

In article <37189bfb.24315004@news.tiscalinet.it>, marc@mercator.net 
says...
> I have just bought a perl script for site-news and would like to make
> some changes. Because I don`t want to bother the company where I
> bougth the script, I`d like to have some help from one of you.
> 
> If someone has some spare time and would like to help me with just
> some small things for free, please let me know. 
> 
> It4s really a small thing. 
> 
> Please respond to: marc@mercator.net
> 
> Thanks,
> 
> Marc
> 
> 


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

Date: Tue, 20 Apr 1999 09:31:12 -0500
From: danbeck@scott.net (Daniel Beckham)
Subject: Re: Wanted: some help for a perl script
Message-Id: <MPG.11864ba8b3798b949896a8@news.idt.net>

[This followup was posted to comp.lang.perl.misc and a copy was sent to 
the cited author.]

I won't do it for free, but you are welcome to hire me to do the 
consulting work for you.

I can be reached privately at:

danbeck@scott.net

BTW, I will need half the fee in advance and the other half after I've 
completed the task.. to make it safe for everyone.

Regards,

Daniel


In article <37189bfb.24315004@news.tiscalinet.it>, marc@mercator.net 
says...
> I have just bought a perl script for site-news and would like to make
> some changes. Because I don`t want to bother the company where I
> bougth the script, I`d like to have some help from one of you.
> 
> If someone has some spare time and would like to help me with just
> some small things for free, please let me know. 
> 
> It4s really a small thing. 
> 
> Please respond to: marc@mercator.net
> 
> Thanks,
> 
> Marc
> 
> 


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

Date: 12 Dec 98 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Special: Digest Administrivia (Last modified: 12 Dec 98)
Message-Id: <null>


Administrivia:

Well, after 6 months, here's the answer to the quiz: what do we do about
comp.lang.perl.moderated. Answer: nothing. 

]From: Russ Allbery <rra@stanford.edu>
]Date: 21 Sep 1998 19:53:43 -0700
]Subject: comp.lang.perl.moderated available via e-mail
]
]It is possible to subscribe to comp.lang.perl.moderated as a mailing list.
]To do so, send mail to majordomo@eyrie.org with "subscribe clpm" in the
]body.  Majordomo will then send you instructions on how to confirm your
]subscription.  This is provided as a general service for those people who
]cannot receive the newsgroup for whatever reason or who just prefer to
]receive messages via e-mail.

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.  

To submit articles to comp.lang.perl.misc (and this Digest), send your
article to perl-users@ruby.oce.orst.edu.

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.

The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.

The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.

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 V8 Issue 5427
**************************************

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