[22038] in Perl-Users-Digest
Perl-Users Digest, Issue: 4260 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Dec 13 18:05:42 2002
Date: Fri, 13 Dec 2002 15:05:09 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Fri, 13 Dec 2002 Volume: 10 Number: 4260
Today's topics:
Re: .NET, a necessary conflict? © (Please read this!) <dblood@2c-b.net>
Re: .NET, a necessary conflict? =?iso-8859-1?Q?=A9?= (P (Tad McClellan)
another sorting question (Aaron)
Re: another sorting question <no@email.com>
Re: another sorting question (Jay Tilton)
Re: can I ship perl interpreter & do I need to send imp (qanda)
Re: can I ship perl interpreter & do I need to send imp (Malcolm Dew-Jones)
Re: copy, rename, open - more confusion? (qanda)
escape special chars before loading into MySQL <du_bing@hotmail.com>
Re: escape special chars before loading into MySQL (Malcolm Dew-Jones)
Re: escape special chars before loading into MySQL <du_bing@hotmail.com>
GD font problem <nospam_stigerikson@yahoo.se>
Re: hash efficiency and key/value/array reading file (qanda)
Re: How to do 32-bit calculations WITH overflow? <nospam-abuse@ilyaz.org>
scripting advice? perl + sh (Alan)
Re: scripting advice? perl + sh (Tad McClellan)
Sending MIME attachment using Mail::Sender (raj)
Re: Sending MIME attachment using Mail::Sender <tassilo.parseval@post.rwth-aachen.de>
Re: Sending MIME attachment using Mail::Sender <nobull@mail.com>
Re: Use Strict warning .. why? <uri@stemsystems.com>
Re: Use Strict warning .. why? <nobull@mail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 13 Dec 2002 21:06:33 GMT
From: "Daniel Blood" <dblood@2c-b.net>
Subject: Re: .NET, a necessary conflict? © (Please read this!)
Message-Id: <tZrK9.54596$Ik.1589847@typhoon.sonic.net>
Yeah, but what are you going to use when M$ buys Borland?
"James Foster" <idmercury@yahoo.co.uk> wrote in message
news:b4d37d82.0212131049.7583517e@posting.google.com...
> To those who are interested in the future and want their say, please
> read this and reply. Thank you!
>
> Recently I have noticed a large divide appearing in the
> programming community, one that is uniting some while driving the
> others away. The source of this seeming conflict can be found within a
> certain Redmond company. Its name, .NET. The new set of development
> tools and standards for programming specifically aimed at web
> developers and e-commerce has been in the air for a while now, hanging
> there, however, it is only now that coders have sat up and begun to
> listen. .NET is a direct assault on the Internets commercial,
> financial and enterprise markets, challenging established platforms
> like Sun's Java. The standards are not just for developing web
> content, each of the major languages have a new versions of
> Microsoft's development software, Visual Studio, .NET or course. This
> is the latest ploy to dominate the commercial programming market from
> Microsoft. There are those who have adopted the new standards quickly,
> keen to be seen as supporters of Microsoft and those who feel they
> have no choice. The message from Microsoft, 'Join or burn!' Those that
> are left are waiting, unsure, are sceptical or will never adopt. I
> have recently acquired the .NET development software and have used it
> to code C++ (if your interested in the language, don't be) The editor
> is adequate but nothing special, the compiler is fast but not the
> fastest I've seen and the Debug was quite frankly crap. Give me
> Borland any day. And, how much, bloody hell! Depending on your needs
> and use of the software, professional, enterprise and so on it ranges
> from £850 to almost £2000 (new copy, upgrade prices are a little
> lower). I am young and foolish but many programmers just won't spend
> that kind of money unless they are among the 'elite' or work for a
> large company. Why shell out when you can get something better for
> half the price or even free? BBEdit is a wonderful editor, Borland
> and others make equal and better compilers and although this may seem
> ignorant but if your code is tight and well written, a serious
> debugger shouldn't be necessary. Compilers will tell you of any errors
> and there is no substitute for good, thorough testing. I let the
> debugger loose on a simple program that calculated results from inputs
> and printed text strings for the required outputs, 500 lines of code,
> simple, neat and consistent. I asked my friends to review and test the
> code and we all agreed that it could not be written more practically
> and systematically. In other words it was optimised and as perfect as
> it could get. I compiled it, ran it and it was perfect, no bugs.
> Huzzah! I then put it through the debugger. The result: same
> functions, almost unreadable code and multiple compile errors. This
> one actually took up more code and was not as good as the first.
> Although this probably wont happen with all of the crap you put
> through it, it shouldn't have done it at all. It could spell disaster
> for large projects and important apps. My advice is don't cut corners
> with code. Be thorough and consistent. There is no doubt that soon it
> will become the supposed 'only way', the .NET way. Me, I will be
> staying with BB and Borland, however I will keep a close eye on the
> rest of the world. I foresee that soon companies will only consider
> .NET coders and the rest of us will have to abide or fade away. But
> not yet. As well as this, the new language C# is beginning to receive
> more supporters. It is undoubtedly a good language, solid stable and
> quite powerful. However it is immature and has not gained global
> recognition. Its development will be interesting and may become a
> necessary requirement for future programmers. At the moment though I
> will be fine set in my ways. I would like to have your feedback on
> this article and see what you think about .NET. I will now go back to
> my desk and begin working again. I almost inhaled the end of my Palm's
> stylus while chewing on it and I have a project to finish. What can we
> do but watch and wait, I don't know. I will ponder it.
>
> James Foster.
>
> ID/mercury.
------------------------------
Date: Fri, 13 Dec 2002 16:21:46 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: .NET, a necessary conflict? =?iso-8859-1?Q?=A9?= (Please read this!)
Message-Id: <slrnavknbq.69s.tadmc@magna.augustmail.com>
James Foster <idmercury@yahoo.co.uk> wrote:
> To those who are interested in the future
You misunderstand the purpose of this newsgroup.
This is the Perl newsgroup. We are interested in *Perl*.
People who are interested in the future of computing in
general hang out in other newsgroups, such as:
alt.future
comp.society.futures Events in technology affecting future computing.
Your article might also be on-topic in:
alt.microsoft.sucks
comp.misc.microsoft.sucks
but not here.
Please do not spam our nice newsgroup anymore.
*plonk*
> read this
Why? Is there something about Perl in it?
[snip Perl-free text]
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 13 Dec 2002 14:19:54 -0800
From: Chewy2426@aol.com (Aaron)
Subject: another sorting question
Message-Id: <7036ffb9.0212131419.1f08d410@posting.google.com>
I have the hardest time sorting stuff.
I have an array with this contents:
2002-12-12,ascend_rtr,CA1905R1,88.87,76783
2002-12-12,cabletron_sw,CA1022V1,99.65,86100
2002-12-12,ascend_rtr,CA1022R1,99.67,86113
2002-12-12,ascend_rtr,CA0313R1,100.00,86400
I want to sort this information by the uptime. If it's lower then 100%
I want it at the top of the list... kinda like it is now (88.87).
I tried $value = (@data,split /,/)[4]; but that didn't work.
Any ideas would be greatly appreciated.
thanks!
------------------------------
Date: Fri, 13 Dec 2002 22:57:16 -0000
From: "Brian Wakem" <no@email.com>
Subject: Re: another sorting question
Message-Id: <atdolr$136p5d$1@ID-112158.news.dfncis.de>
"Aaron" <Chewy2426@aol.com> wrote in message
news:7036ffb9.0212131419.1f08d410@posting.google.com...
> I have the hardest time sorting stuff.
>
> I have an array with this contents:
> 2002-12-12,ascend_rtr,CA1905R1,88.87,76783
> 2002-12-12,cabletron_sw,CA1022V1,99.65,86100
> 2002-12-12,ascend_rtr,CA1022R1,99.67,86113
> 2002-12-12,ascend_rtr,CA0313R1,100.00,86400
>
> I want to sort this information by the uptime. If it's lower then 100%
> I want it at the top of the list... kinda like it is now (88.87).
>
> I tried $value = (@data,split /,/)[4]; but that didn't work.
>
> Any ideas would be greatly appreciated.
>
> thanks!
perldoc -q sort
--
Brian Wakem
------------------------------
Date: Fri, 13 Dec 2002 23:00:05 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: another sorting question
Message-Id: <3dfa64b6.1508659@news.erols.com>
Chewy2426@aol.com (Aaron) wrote:
: I have an array with this contents:
: 2002-12-12,ascend_rtr,CA1905R1,88.87,76783
: 2002-12-12,cabletron_sw,CA1022V1,99.65,86100
: 2002-12-12,ascend_rtr,CA1022R1,99.67,86113
: 2002-12-12,ascend_rtr,CA0313R1,100.00,86400
:
: I want to sort this information by the uptime. If it's lower then 100%
: I want it at the top of the list... kinda like it is now (88.87).
A Schwartzian Transform might speed things along, but let's get the
primary goal sorted out (heh) first.
#!perl
use warnings;
use strict;
print for sort {
(split /,/, $a)[4] <=> (split /,/, $b)[4]
} <DATA>;
__DATA__
2002-12-12,ascend_rtr,CA0313R1,100.00,86400
2002-12-12,ascend_rtr,CA1022R1,99.67,86113
2002-12-12,cabletron_sw,CA1022V1,99.65,86100
2002-12-12,ascend_rtr,CA1905R1,88.87,76783
: I tried $value = (@data,split /,/)[4]; but that didn't work.
In future, elaborate on "didn't work." That line by itself is rather
baffling. Showing some of the surrounding code might have clarified
what it was meant to accomplish and why it failed.
------------------------------
Date: 13 Dec 2002 13:50:46 -0800
From: fumail@freeuk.com (qanda)
Subject: Re: can I ship perl interpreter & do I need to send imported modules
Message-Id: <62b4710f.0212131350.1d3bcbf6@posting.google.com>
tadmc@augustmail.com (Tad McClellan) wrote in message news:<slrnavjpp3.2f2.tadmc@magna.augustmail.com>...
> qanda <fumail@freeuk.com> wrote:
>
> > Assuming I wanted to deliver perl script(s) to a customer:-
> > 1) what should I do legally - from the perspective of perl not my
> > company
>
>
> You do what Perl's license says to do. :-)
good point! - I admit it, I've been very busy, stressed out and quite
stupid; it always gets like that around xmas time :)
Thanks again, can you tell me how to answer the second part (ie what
needs packaging for customer without perl), ie somewhere quite
concrete; I have spent a few hours on this and got nowhere yet - maybe
I'm just not looking in the right place.
------------------------------
Date: 13 Dec 2002 14:44:16 -0800
From: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
Subject: Re: can I ship perl interpreter & do I need to send imported modules
Message-Id: <3dfa62c0@news.victoria.tc.ca>
qanda (fumail@freeuk.com) wrote:
: tadmc@augustmail.com (Tad McClellan) wrote in message news:<slrnavjpp3.2f2.tadmc@magna.augustmail.com>...
: > qanda <fumail@freeuk.com> wrote:
: >
: > > Assuming I wanted to deliver perl script(s) to a customer:-
: > > 1) what should I do legally - from the perspective of perl not my
: > > company
: >
: >
: > You do what Perl's license says to do. :-)
: good point! - I admit it, I've been very busy, stressed out and quite
: stupid; it always gets like that around xmas time :)
: Thanks again, can you tell me how to answer the second part (ie what
: needs packaging for customer without perl), ie somewhere quite
: concrete; I have spent a few hours on this and got nowhere yet - maybe
: I'm just not looking in the right place.
On unix, simply install perl (it's usually installed anyway), e.g. on
redhat simply ask the customer to use the right rpm and bingo, everything
works.
On windows, the easiest is to use an existing install, (active state is
quite commonly used)
but if you wish to install fewer files then install (probably) the
following
C:\Perl\bin\ perl.exe
C:\Perl\bin\ perlcore.dll
C:\WINNT\system32\ PerlCRT.dll
(the perl\bin stuff could be anywhere in the path, I'm not sure offhand
about the PerlCRT.dll)
plus of course whatever modules you use, plus any other files mentioned
in the license such as the license itself (probably).
If I'm working temporarily on someone elses PC then I usually copy a
minimal number of my things I "must" have into a single directory - and
that includes a few key perl files, (forget which exactly, but it's very
minimal to gain access to use perl).
------------------------------
Date: 13 Dec 2002 11:58:25 -0800
From: fumail@freeuk.com (qanda)
Subject: Re: copy, rename, open - more confusion?
Message-Id: <62b4710f.0212131158.27a6bae1@posting.google.com>
tadmc@augustmail.com (Tad McClellan) wrote in message news:<slrnavjstq.2f2.tadmc@magna.augustmail.com>...
> qanda <fumail@freeuk.com> wrote:
>
> > I'm all confused again - the more of my Camel book, perldoc man pages,
> > this newsgroup and faqs I read the more confused I seem to get!
>
>
> That never happens to anyone else. (not! :-)
>
>
> > Background ...
> > I need to process x number of files, each with 1 record per line.
> > Each record has a record identifier within a field (say the 15th
> ^^^^^^^^^^^^^^^^^
> ^^^^^^^^^^^^^^^^^ implies unique amongst the records
>
Sorry, I'm a C programmer and should know better (I'm blaming it on
crazy work I'm doing at the moment), it is a record TYPE however the
record format guide I'm working to (from an extremely large well known
company-ssshhh) calls it a record identifier!
> > field). As I read each file I write out different record types to new
> > files, eg if the current file has 5 records with ID's a, b, c, d and
> > e, I want to write out all the records with ID a to a_type.txt, b to
> ^^^
> ^^^ implies more than one record with same ID
>
yep - absolutely correct, for the purposes of this question that is
enough but in reality there are roughly 5 to 15 fields required to
define uniqueness.
> > b_type.txt. However I'm not interested in types c or e, so I write
> > out types d to d_type.txt; types c and e need to go back into the
> > original file.
>
>
> So it isn't really a "record identifier" at all, it is a
> "type identifier" or some such.
>
correct - see above
>
> > I'm happy with record processing, my question concerns copy/rename
> > etc. I imagine the best way is to use the current file for record
> > splitting putting ignored types into ignore.txt for eg, then when when
> > the current file is finished, close it then rename ignore to original.
> ^^^^^^ ^^^^^^^^^^^
> > So we end up with types c and e only in the original file.
>
>
> Perl can handle in-place editing for you, as described in the
> answer to this FAQ:
>
> How do I change one line in a file/delete a line in a
> file/insert a line in the middle of a file/append to the
> beginning of a file?
>
thanks - I've seen answers like this but have trouble finding them in
the faqs, need to be more familiar with them.
>
> > The perl must be efficeint, portable and robust (ie commercially safe
> > to use).
>
>
> Oh. I think there's a problem with in-place editing on Windows
> when you don't specify a backup for -i (or $^I), but I'm not
> clear on the details as I don't use Windows.
>
Oooh the 'W' word - I'm a UNIX bod myself, however I do need to think
about the Windows world, thanks again.
> > I've seen examples using open to create a tempfile, the rename
> > function, sysopen and the File::Copy but am confused taking the above
> > into account what to use.
> >
> > Any ideas, suggestions?
>
> untested:
>
> # open A, B and D filehandles before here
> { local $^I = '.old';
> local @ARGV = 'file.original';
>
> while ( <> ) {
> my $type = determine_type($_);
>
> if ( $type eq 'a' )
> { print A }
> elsif ( $type eq 'b' )
> { print B }
> elsif ( $type eq 'd' )
> { print D }
> else
> { print }
> }
> unlink 'file.original.old'; # don't really want a backup file
> }
Thanks a lot for your help, I'll look at your example to learn what I
can from it, you've answered more than one of my questions - thank you
very much for the help.
------------------------------
Date: Fri, 13 Dec 2002 14:42:38 -0600
From: user <du_bing@hotmail.com>
Subject: escape special chars before loading into MySQL
Message-Id: <3DFA463E.E41961DD@hotmail.com>
Does Perl have any similar funtion to addslashes() that PHP does?
What I want to do is to insert uploaded file into MySQL.
=========
open(F,$upload_name) || die "can not open $uploaded for reading;
read F,$content,$upload_size;
close(F);
$mysql_dbh->do("INSERT INTO $table (content,filename,filesize) VALUES
('$content', '$upload_name','$upload_size')");
==========
My $content may have some special charactors, e.g single quote ('), that
must be escaped before it can be accepted by MySQL. Just wondering if
there is any funtion that can escape _all_ the characters considered
special by MySQL?
Thanks for any help,
Bing
------------------------------
Date: 13 Dec 2002 13:34:40 -0800
From: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
Subject: Re: escape special chars before loading into MySQL
Message-Id: <3dfa5270@news.victoria.tc.ca>
user (du_bing@hotmail.com) wrote:
: Does Perl have any similar funtion to addslashes() that PHP does?
: What I want to do is to insert uploaded file into MySQL.
: =========
: open(F,$upload_name) || die "can not open $uploaded for reading;
: read F,$content,$upload_size;
: close(F);
: $mysql_dbh->do("INSERT INTO $table (content,filename,filesize) VALUES
: ('$content', '$upload_name','$upload_size')");
: ==========
: My $content may have some special charactors, e.g single quote ('), that
: must be escaped before it can be accepted by MySQL. Just wondering if
: there is any funtion that can escape _all_ the characters considered
: special by MySQL?
: Thanks for any help,
how about a function called "quote" which is documented in DBI
perldoc DBI
------------------------------
Date: Fri, 13 Dec 2002 16:48:21 -0600
From: user <du_bing@hotmail.com>
Subject: Re: escape special chars before loading into MySQL
Message-Id: <3DFA63B5.A8BF5D71@hotmail.com>
Ah, thanks for the pointer. I saw this in the manual:
=========
Quote will probably not be able to deal with all
possible input (such as binary data or data containing
newlines), and is not related in any way with escaping
or quoting shell meta-characters. There is no need to
quote values being used with the section on
/"Placeholders and Bind Values.
=========
So it does not fit out needs because uploaded files can be of any type. I'm
thinking about using MIME::Base64. Anybody has any better suggestions?
Thanks,
Bing
Malcolm Dew-Jones wrote:
> user (du_bing@hotmail.com) wrote:
> : Does Perl have any similar funtion to addslashes() that PHP does?
> : What I want to do is to insert uploaded file into MySQL.
>
> : =========
> : open(F,$upload_name) || die "can not open $uploaded for reading;
> : read F,$content,$upload_size;
> : close(F);
>
> : $mysql_dbh->do("INSERT INTO $table (content,filename,filesize) VALUES
> : ('$content', '$upload_name','$upload_size')");
> : ==========
>
> : My $content may have some special charactors, e.g single quote ('), that
> : must be escaped before it can be accepted by MySQL. Just wondering if
> : there is any funtion that can escape _all_ the characters considered
> : special by MySQL?
>
> : Thanks for any help,
>
> how about a function called "quote" which is documented in DBI
>
> perldoc DBI
------------------------------
Date: Fri, 13 Dec 2002 22:47:28 +0100
From: stig <nospam_stigerikson@yahoo.se>
Subject: GD font problem
Message-Id: <atdkj1$brk$1@oden.abc.se>
hi
running RH8 and GD-2.05
i experience problems when trying to create text in images.
grafics works fine however.
if it try the demo ttf.pl:
> perl ttf.pl
2.05 at ttf.pl line 10.
Could not find/open font at ttf.pl line 23.
the ttf.pl demo looks as below, the only thing i changed is the path to perl
(first line).
the files and directories used at lines 3,4,5 exists, i even tried with
absolute paths.
so question is, why does it not work and how could i make work?
is this a RH8 problem (change of fonthandling system), or some thing else?
########start###########
#!/usr/bin/perl
use lib '../blib/lib','../blib/arch';
use GD 1.20;
use constant font => '../t/Generic.ttf';
$im = new GD::Image(400,250);
warn $GD::VERSION;
($white,$black,$red,$blue,$yellow) =
(
$im->colorAllocate(255, 255, 255),
$im->colorAllocate(0, 0, 0),
$im->colorAllocate(255, 0, 0),
$im->colorAllocate(0,0,255),
$im->colorAllocate(255,250,205)
);
$im->interlaced(1); # cool venetian blinds effect
# Some TTFs
$im->stringTTF($black,font,12.0,0.0,20,20,"Hello world!") || die $@;
$im->stringTTF($red,font,14.0,0.0,20,80,"Hello world!")|| die $@;;
$im->stringTTF($blue,font,30.0,-0.5,60,100,"Goodbye cruel world!")|| die
$@;;
binmode STDOUT;
# print the image to stdout
print $im->png;
##########end###########
thanks in advance
stig
------------------------------
Date: 13 Dec 2002 14:17:56 -0800
From: fumail@freeuk.com (qanda)
Subject: Re: hash efficiency and key/value/array reading file
Message-Id: <62b4710f.0212131417.6625c5bf@posting.google.com>
"Tassilo v. Parseval" <tassilo.parseval@post.rwth-aachen.de> wrote in message news:<atcclg$9d8$1@nets3.rz.RWTH-Aachen.DE>...
> Also sprach qanda:
>
> > I'm having trouble learning some basics
> >
> > I have a file like this:
> > # comment1
> > # comment 2
> > key1 = val1
> > key2 = val2 # comment
> > key3
> > key4 = # configure later
> > ...
> > key9999 = val9999
> >
> > I have a list of known keys (say key1, key50, key192) that I want to
> > check for values. I got stuck on the basics ...
> >
> > (only relevant sections shown)
> > # read each line in file
> > while( my $rec =<CFG_FILE )
> ^
> ^
> Please do not type code, use copy and paste.
>
> > # remove newline
> > chomp( $rec );
> > # ignore comment and blank lines
> > if( $rec !~ /^\s*#|^\s*$/ )
> >
> > I'm sure there's an easier way in perl of doing this matching
>
> Depending in your needs there might be. Your data are probably best
> preprocessed by removing the unwanted parts out: that includes
> whitespaces and comments. Thus, anything that is left after this
> preprocessing can be considered relevant data.
>
> > I then had trouble trying to figure out how to read the relevant
> > fields in each matching line, ie should I read the whole file into a
> > hash (noting that I need fields 0 and 2, including empty fields) or
> > just fields 0 and 2 in the hash if field 2 has a value? Alternatively
> > should I build a list of keys wanted (say key3,7,25) then read the
> > file only filling a hash with wanted keys that have a value; I'm lost
> > with this kind of thinking at the moment.
> >
> > I then got stuck with arrays
> >
> > I can read a file of records ok, with for example
> > ...
> > while( my $rec =<AFILE> )
> > {
> > chomp($rec);
> > my @flds = split( /<pat>/, $rec, -1 );
> > foreacy my $fld (@flds)
> ^
> !!!!!
> > {
> > # do something
> >
> > And all works OK, however when I try the same approach for the above
> > everything goes bad
>
> Unfortunately I don't understand at all how the above while-loop is to
> operate on your sample data so I can't comment on this.
>
> Anyway, here is a little loop that preprocesses the data as described
> further above. It makes the distinction between 'val=' and 'val'. In the
> first case it results in 'val => ""' whereas in the second it is 'val =>
> undef' which might be useful:
>
> my %hash;
> while (<DATA>) {
>
> chomp;
> s/\s*#.*//;
> $_ or next; # line empty after preprocessing => next line
>
> s/\s+//g; # squeeze whitespaces of what is left
> $hash{$1} = $2, next if /(.+)=(.*)/;
> $hash{$_} = undef; # only if no '=' shows up
> }
> __DATA__
> # comment1
> # comment 2
> key1 = val1
> key3
> key4 = # configure later
> key2 = val2 # comment
>
> This results in:
>
> $VAR1 = {
> 'key1' => 'val1',
> 'key2' => 'val2',
> 'key3' => undef,
> 'key4' => ''
> };
>
> If you want to distinguish between key3 and key4 you have to use
>
> if(exists $hash{key3} && !defined $hash{key3})
>
> whereas for key4 it has to be
>
> if (exists $hash{key4} && defined $hash{key4})
>
> For all other is is simply:
>
> if ($hash{key})
>
> Tassilo
Thanks, a small working sample like that really helps, with that in
mind I've changed it slightly so only keys with values are saved: -
#!/usr/bin/perl -w
use diagnostics -verbose;
use warnings;
use strict;
my %hash;
while (<DATA>)
{
chomp;
s/\s+|#.*$//g;
# The hash works, but I don't fully understand.
$hash{$1} = $2, next if /(\w+)=(\w+)/;
}
# This does what I want but is it perlistic!
foreach my $key (keys %hash )
{
print "$key:$hash{$key}\n";
}
__DATA__
# normal comment
# comment with spaces2
# comment with tabs
key1 = value1
key4
# random comment
key5 = value5
key2 = # comment on key line, no value
key3 = value3 # comment on key/value line
key7 = value7
key6 = value6
Seeing DATA surprised me but know I now what it does I like it - LOTS!
Thanks for the reply, you can see there are two places I'm still
trying to get round my head (keep in mind I come from a C/UNIX
background which may help to explain). The script works OK but there
are two more issues for me, both related to efficiency and perlisms.
Lets suppose I now have a file with 10 million key value pairs and I'm
only interested in say for example 100 keys (lets say key1...key20,
key21...key40, etc). Should we use the above to load 10 million
key/value pairs and then sort/search for the 100 we want or should we
build a list of the 100 we are interested in and then only load those
100 (assuming they have a value) into the hash?
Thanks again.
------------------------------
Date: Fri, 13 Dec 2002 21:13:06 +0000 (UTC)
From: Ilya Zakharevich <nospam-abuse@ilyaz.org>
Subject: Re: How to do 32-bit calculations WITH overflow?
Message-Id: <atdih2$1fq2$1@agate.berkeley.edu>
[A complimentary Cc of this posting was sent to
Mikko Noromaa
<mikkon@excelsql.com>], who wrote in article <at5evd$hf2$1@phys-news1.kolumbus.fi>:
> sub mod32
> {
> my $n=shift(@_);
> my $i=shift(@_);
> my $r;
>
> no integer;
> if ($n<0) { $n+=4294967296; }
> if ($i<0) { $i+=4294967296; }
> $r=$n%$i;
> use integer;
> return ($r);
> }
Why not just
sub mod32 { (0xffffffff & shift) % (0xffffffff & shift) }
? Hmm, I see: clumping to the MIN_I32 gets in the way...
>pprintf %#x -0xefffffff & 0xffffffff
0x80000000
Anyway, I see no reason for your use/no integer...
Ilya
------------------------------
Date: 13 Dec 2002 11:13:23 -0800
From: alan_santacruz@yahoo.com (Alan)
Subject: scripting advice? perl + sh
Message-Id: <b7784f4d.0212131113.4d4d2d1a@posting.google.com>
hi
we're writing a perl script to install an app which uses a 'tailored'
shell script -- the shell script is run at boot. That is, sections of
the shell script must be adjusted to suit the platform. The shell
script is roughly 500 lines, and the tailored bit of ~15 lines follows
the header, about 50 lines in. I'd appreciate advice on the most
maintainable way to create the shell script.
In the perl we'll use something like
@script_lines =
"appdir=\"/lib/modules/$osrel/kernel/drivers/net/appsys\"",
"if [ -f $dnedir/dneinit.conf ]; then",
" . $dnedir/dneinit.conf",
"else",
;
<snip>
foreach @script_lines {
print( FH,"$_\n";
}
... etc
The perl script could of course create the whole shell script, but
that means that any bugfixing/changes of the shell script are made
"once-removed" -- the snag to embedding the entire shell script in the
perl is the extra complexity of quoting.
So I'm thinking of keeping the header and body of the shell script as
separate files, and using perl to combine those with the new tailored
bit.
any thoughts on the cleanest approach please? Is there a Better Way?
alan
(also posted to comp.os.linux.misc)
------------------------------
Date: Fri, 13 Dec 2002 13:31:14 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: scripting advice? perl + sh
Message-Id: <slrnavkdc2.5rc.tadmc@magna.augustmail.com>
Alan <alan_santacruz@yahoo.com> wrote:
> we're writing a perl script to install an app which uses a 'tailored'
> shell script -- the shell script is run at boot. That is, sections of
> the shell script must be adjusted to suit the platform.
> I'd appreciate advice on the most
> maintainable way to create the shell script.
Use a "here document", documented in perldata.pod.
> So I'm thinking of keeping the header and body of the shell script as
> separate files, and using perl to combine those with the new tailored
> bit.
>
> any thoughts on the cleanest approach please?
# untested
print FH<<ENDSH;
appdir="/lib/modules/$osrel/kernel/drivers/net/appsys"
if [ -f $dnedir/dneinit.conf ]; then
. $dnedir/dneinit.conf
else
ENDSH
> (also posted to comp.os.linux.misc)
You should crosspost a single article rather than multipost.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 13 Dec 2002 12:32:16 -0800
From: sharda@hotmail.com (raj)
Subject: Sending MIME attachment using Mail::Sender
Message-Id: <ac4b6b9a.0212131232.7da1486d@posting.google.com>
Hi Guys,
Can we send a file as an attachment(in MIME format) using the
MAIL::SENDER module(SCO OpenServer 5, perl 5.x).
Iam using the following code:
use Mail::Sender
defined the variables for smtp, to, subject, body.
ref ( $sender = new Mail::Sender { from => $from, smtp => $smtp})
or die "Error($sender) : $Mail::Sender::Error\n";
$sender->OpenMultipart({to => $to, subject => $subject});
$sender->Body;
$sender->SendLine($body);
$sender->SendLine(
{description => 'testing',
ctype => 'application/octet-stream',
encoding => 'Base64',
disposition => 'attachment; filename="abc.TXT"; type="ZIP
archive"',
file => "abc.TXT"});
$sender->Close || die "Error sending email: $Mail::Sender::Error\n";
Whenever i run this chunk of code, i do get the email but there is no
attachment(file do exists and i have tried giving the absolute path of
the file), and in the body, apart from the content of $body, i also
get "HASH(0x80fd158)".
Any helpful suggestions/advice?
Thanks in Advance.
------------------------------
Date: 13 Dec 2002 20:39:48 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@post.rwth-aachen.de>
Subject: Re: Sending MIME attachment using Mail::Sender
Message-Id: <atdgik$h71$1@nets3.rz.RWTH-Aachen.DE>
Also sprach raj:
> Can we send a file as an attachment(in MIME format) using the
> MAIL::SENDER module(SCO OpenServer 5, perl 5.x).
[...]
Possible of course. But this basically boils down to creating the
raw message yourself prior sending. Perhaps you should first give
MIME::Lite a try. This module was written to do exactly this: sending
out MIME-enriched mails.
Tassilo
--
$_=q!",}])(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus;})(rekcah{lrePbus;})(lreP{rehtonabus;})(rehtona{tsuJbus!;
$_=reverse;s/sub/(reverse"bus").chr(32)/xge;tr~\n~~d;eval;
------------------------------
Date: 13 Dec 2002 20:47:58 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: Sending MIME attachment using Mail::Sender
Message-Id: <u9wumd3atd.fsf@wcl-l.bham.ac.uk>
sharda@hotmail.com (raj) writes:
> use Mail::Sender
> ref ( $sender = new Mail::Sender { from => $from, smtp => $smtp})
> or die "Error($sender) : $Mail::Sender::Error\n";
>
> $sender->OpenMultipart({to => $to, subject => $subject});
> $sender->Body;
> $sender->SendLine($body);
> $sender->SendLine(
> {description => 'testing',
> ctype => 'application/octet-stream',
> encoding => 'Base64',
> disposition => 'attachment; filename="abc.TXT"; type="ZIP
> archive"',
> file => "abc.TXT"});
> $sender->Close || die "Error sending email: $Mail::Sender::Error\n";
>
>
> Whenever i run this chunk of code, i do get the email but there is no
> attachment(file do exists and i have tried giving the absolute path of
> the file), and in the body, apart from the content of $body, i also
> get "HASH(0x80fd158)".
I've never used Mail::Sender but I've just gone and glanced and the
manual.
> Any helpful suggestions/advice?
You seem to use SendLine() method where you should be using another
method, probably Attach().
> Thanks in Advance.
If you are going to do something in advance, perhaps it should be to
consult the manual.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Fri, 13 Dec 2002 19:08:09 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Use Strict warning .. why?
Message-Id: <x77ked7n53.fsf@mail.sysarch.com>
>>>>> "gp" == gibbering poster <noone@nowhere.com> writes:
gp> @{$self}{width, height} = ($MAX_WIDTH, $MAX_HEIGHT); # <-- THIS LINE
^^^^^ ^^^^^^
gp> GENERATES WARNING
those are barewords which are not allowed under strict. a single
bareword hash key is allowed but when you have more than one you have to
quote them. i prefer to always quote my hash keys in all places so i
don't have to deal with that issue.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: 13 Dec 2002 19:12:20 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: Use Strict warning .. why?
Message-Id: <u97ked68dn.fsf@wcl-l.bham.ac.uk>
"gibbering poster" <noone@nowhere.com> writes:
> use strict;
> @{$self}{width, height} = ($MAX_WIDTH, $MAX_HEIGHT); # <-- THIS LINE
> GENERATES WARNING
> Bareword "width" not allowed while "strict subs" in use Map.pm line 18
> Bareword "height" not allowed while "strict subs" in use Map.pm line 18
That's a compile-time error not a warning.
Compile-time errors are like half-way between being warning and
errors. They don't cause compilation to terminate immediately but do
set a flag that will cause perl to terminate when it reaches the end
of the current compliation unit or the next BEGIN{} (or use())
compiler directive.
> if I assign these with $self->{width} = $MAX_WIDTH, I dont get the warning...
> For some reason, the interpreter doesn't like the hash-slice method of assigning
> values to keys within the object hash... why?
It's not the slice it objects to, it's the barewords.
Barewords are where perl interprets width as 'width' - that is to say
it is implicitly quoted. Without "strict subs" any unrecognised word
(i.e. any word that's not an in-built Perl reserved word and hasn't
been defined as a user defined function/subroutine) is treated as a
string.
There are also in Perl syntax some special contexts where words (even
if they are perl reserved words or functions) are treated as strings.
One of these specal contexts is in the subscript of as hash.
If I write $foo{shift} this is interpreted as $foo{'shift'} not
$foo{shift()}.
This special case only exists where the word is the only
(non-whitespace) thing in the {}.
In your slice @{$self}{width, height} you cannot omit the quotes
arround the strings if you have "strict subs" in effect.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
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.
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 4260
***************************************