[22383] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4604 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Feb 22 03:05:42 2003

Date: Sat, 22 Feb 2003 00:05:10 -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           Sat, 22 Feb 2003     Volume: 10 Number: 4604

Today's topics:
    Re: A regex to shorten up this command line (Tad McClellan)
    Re: Get Public IP Address of Linux Machine (Alan Barclay)
    Re: having PERL respond to a key press <REMOVEsdnCAPS@comcast.net>
        hiding/minimizing Win32 window <oeschey@web.de>
    Re: How do I install a new module on a webserver? <geoff.news6@alphaworks.co.uk>
    Re: How do I install a new module on a webserver? <noreply@gunnar.cc>
    Re: How to simulate real modem connection speed on loca <spam@thecouch.homeip.net>
    Re: How to simulate real modem connection speed on loca <mgjv@tradingpost.com.au>
        LWP Requests <smiley@uvgotemail.com>
    Re: Problem with buffering on non-blocking socket under <goldbb2@earthlink.net>
    Re: Religious question: commenting <abigail@abigail.nl>
    Re: Religious question: commenting (Malcolm Dew-Jones)
        Save and restore Tk UI state via cute, not brute (Phlip)
    Re: setting permissions on my server for cgi <bwalton@rochester.rr.com>
    Re: setting permissions on my server for cgi <cpb6043@osfmail.rit.edu>
    Re: Switch order of sprintf conversions (Anno Siegel)
    Re: Switch order of sprintf conversions <wuerz@yahoo.com>
    Re: Switch order of sprintf conversions <goldbb2@earthlink.net>
        Use or not to use modules [was: How do I ...] (Gunnar Hjalmarsson)
    Re: Use or not to use modules [was: How do I ...] <tassilo.parseval@post.rwth-aachen.de>
        Where does the 1 come from <oxmard.Rules@ab.ab>
    Re: Where does the 1 come from <pinyaj@rpi.edu>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 21 Feb 2003 17:12:57 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: A regex to shorten up this command line
Message-Id: <slrnb5dcjp.854.tadmc@magna.augustmail.com>

TruthXayer <TruthXayer@yahoo.com> wrote:


> perl -ne '$ct =(tr/A_string//); END{print $ct;}'


Today's date in February 21, not April 1.

Do you know what tr/// does?

Your code will (mis)function exactly the same with:

   tr/ginrst_A//

The order of the characters makes no difference...


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: 21 Feb 2003 23:59:09 GMT
From: gorilla@elaine.furryape.com (Alan Barclay)
Subject: Re: Get Public IP Address of Linux Machine
Message-Id: <1045871949.435632@elaine.furryape.com>

In article <HJe5a.40178$P1.2708832@news1.east.cox.net>,
Buck Turgidson <jc_va@hotmail.com> wrote:
>I am beating my brains out on this one.  I have a linux machine behind a
>cable router.  Using things like ifconfig will only give me the internal
>network address.  I want to get the actual public intenet address.  Are
>there any perl one-liners that can do this?

print "Enter your ip address: "; chomp($ipaddr=<>);


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

Date: Fri, 21 Feb 2003 21:01:56 -0600
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: having PERL respond to a key press
Message-Id: <Xns9329E00E01517sdn.comcast@216.166.71.239>

<tyrannous@o-space.com> wrote in news:b363e3$b40$1@news8.svr.pol.co.uk:

> yes i tried it
> i tried the following:
> 
> #!c:\perl\bin\perl.exe
> 
> use Term::ReadKey;
> 
> blah blah
> 
> says cannot find Term::ReadKey in @INC or something like that

Well then, you should go find it or something like that.  Maybe find it on 
the Internet or something like that.  

-- 
Eric
print scalar reverse sort qw p ekca lre reh 
ts uJ p, $/.r, map $_.$", qw e p h tona e;



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

Date: Sat, 22 Feb 2003 00:05:52 +0100
From: Lars Oeschey <oeschey@web.de>
Subject: hiding/minimizing Win32 window
Message-Id: <i2cd5vooaq5u19v9tdq7sg06hp7m5r2c1m@4ax.com>

Hi,

I have a problem that on a target machine of my app (Perl::Tk,
compiled with ActiveState's PerlApp) when my application is running,
there appears a fullscreen window "TclNotifier". From the tcl group I
now know that this window never should show up, but it does, and it
hides my application.
Until I know why that is, I need some workaround, so I need to
identify that window, and then minimize it.
Any clues?

Lars


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

Date: Fri, 21 Feb 2003 23:40:23 -0000
From: "Geoff Soper" <geoff.news6@alphaworks.co.uk>
Subject: Re: How do I install a new module on a webserver?
Message-Id: <3e56b8e9$0$371$cc9e4d1f@news.dial.pipex.com>

"Geoff Soper" <geoff.news6@alphaworks.co.uk> wrote in message
news:3e56a603$0$369$cc9e4d1f@news.dial.pipex.com...
> I have limited access to a webserver, i.e. I have access to the cgi-bin
> directory. A program I am writing needs to use Digest::MD5 which isn't
> currently provided. Can I install this myself with only limited access?
>

I've since found this helpful article which mentions webservers with no
rights
http://www.rcbowen.com/imho/perl/modules.html




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

Date: Sat, 22 Feb 2003 00:50:31 GMT
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: How do I install a new module on a webserver?
Message-Id: <rPz5a.11673$FF4.632406@newsb.telia.net>

Geoff Soper wrote:
> "Geoff Soper" <geoff.news6@alphaworks.co.uk> wrote in message
> news:3e56a603$0$369$cc9e4d1f@news.dial.pipex.com...
> 
>>I have limited access to a webserver, i.e. I have access to the cgi-bin
>>directory. A program I am writing needs to use Digest::MD5 which isn't
>>currently provided. Can I install this myself with only limited access?
> 
> 
> I've since found this helpful article which mentions webservers with no
> rights
> http://www.rcbowen.com/imho/perl/modules.html

I have recently written an own (my first) CPAN module, and the section 
"Manual Installation" in the docs is a simple step-by-step instruction 
that should be generally applicable:
http://search.cpan.org/author/GUNNAR/CGI-ContactForm-1.02/lib/CGI/ContactForm.pm

/ Gunnar

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



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

Date: Fri, 21 Feb 2003 22:40:01 -0500
From: Mina Naguib <spam@thecouch.homeip.net>
Subject: Re: How to simulate real modem connection speed on local apache server?
Message-Id: <niC5a.54956$qn4.719855@wagner.videotron.net>

-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1

Roman Khutkyy wrote:
> There is a problem in debugging JS script that preloads images
> for animated menu on local Apsche server (win32). Id like to see how
> this loading going as it was real modem connection. So, is there any module
> that can do such things?

I'm not sure where perl fits into the question.

However, here are a couple of options I could think of.  I'm sure others 
may have more ideas:

1. Use apache's mod_throttle module.

2. If you're lucky enough to be running FreeBSD then you can use ipfw to 
create a "pipe" or a "queue" with specific bandwidth limitations.  You 
can then use ipfw to divert some, or all traffic, to go through the pipe 
you just created.  This is an excellent way to simulate a network 
bottleneck. man ipfw for more info.

Best of luck.



-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE+VvEUeS99pGMif6wRAqFdAJ97Bcmqs723V4G44MXjn6lJ/PgiSQCdEjF8
Xkxkb+57LMSIbFHk98qtP8E=
=1wI6
-----END PGP SIGNATURE-----



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

Date: Sat, 22 Feb 2003 16:02:00 +1100
From: Martien Verbruggen <mgjv@tradingpost.com.au>
Subject: Re: How to simulate real modem connection speed on local apache server?
Message-Id: <slrnb5e128.2fm.mgjv@martien.heliotrope.home>

On Fri, 21 Feb 2003 22:40:01 -0500,
	Mina Naguib <spam@thecouch.homeip.net> wrote:
> -----BEGIN xxx SIGNED MESSAGE-----
> Hash: SHA1
> 
> Roman Khutkyy wrote:
>> There is a problem in debugging JS script that preloads images
>> for animated menu on local Apsche server (win32). Id like to see how
>> this loading going as it was real modem connection. So, is there any module
>> that can do such things?
> 
> I'm not sure where perl fits into the question.
> 
> However, here are a couple of options I could think of.  I'm sure others 
> may have more ideas:

Me too, and I hope they post them somewhere else :)

Martien
-- 
                        | 
Martien Verbruggen      | Since light travels faster than sound, isn't
                        | that why some people appear bright until you
                        | hear them speak?


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

Date: Sat, 22 Feb 2003 02:03:24 -0500
From: "Smiley" <smiley@uvgotemail.com>
Subject: LWP Requests
Message-Id: <v5e8635er9paa5@corp.supernews.com>

This is regarding the subject of my earlier post titled 'Retrieving
Information'

I set up my script to access information from the webpages search engine.  I
was able to use LWP to get the information through Post for the first page,
then changed the settings that should bring up the information on the
second, third, and so on pages.  The problem is that I only kept on getting
repeats of the first page, couldn't get to the rest of them.

Does anybody know why this might be?




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

Date: Sat, 22 Feb 2003 00:37:22 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Problem with buffering on non-blocking socket under win32
Message-Id: <3E570C92.ED0003D5@earthlink.net>



Ed W wrote:
> 
> "Benjamin Goldberg" <goldbb2@earthlink.net> wrote in message
> news:3E56939A.DF6A2058@earthlink.net...
> > Ed W wrote:
> > >
> > > Thanks for this thought, however, thats EXACTLY the problem.  I am
> > > using a select loop to read and write to a pair of sockets.
> >
> > Are you really selecting for both readability and writability (and using
> > syswrite to send data, and substr() to remove however much you've
> > written), or are you just selecting for readability, and using print()
> > to send the data?
> 
> Nope.  I think we are talking cross purposes.  See my last email.  Something
> is broken under win32.  If I use syswrite with 100MB (really!), it doesn't
> block, returns control immediately, but also, the substr never gets called,
> ie the whole darn lot gets written somewhere!
> 
> This is definitely not desirable I think...
> 
> > > The app is a POP3 proxy, and so I am listening to a mail client on
> > > localhost and speaking to a mail server over a 300 byte/sec link on
> > > the other socket.
> >
> > > The point is not to read too much from the mail client socket
> > > because it will take ages to write out to the slow remote socket (with
> > > possible timeouts on the mail client in the meantime)
> >
> > If you use syswrite(), not print, to send data to the slow remote
> > socket, then writing data should *never* *ever* block if select has
> > indicated that that socket is writable.
> 
> This is true, it *doesn't* block.  However, if the client sends even a small
> 20Kb email, then clearly the read socket will read all of this in almost
> immediately.  However, the write socket will take 20,000 / 300  = 66 secs to
> write the data back out.  60 secs is about normal for timeouts on Outlook
> (and seems like a good number to me).  Imagine sending a 200Kb email...

Ok.  I'm imagining a 200Kb email.  What I see happening is that all the
data is read almost immediately, and all the data is written to the
outgoing socket almost immediately (though not all of it is sent yet
over the wire).  The program closes the reading socket, and closes the
writing socket.

In the background, the OS transmits the data that you wrote over the
slow modem.

What's the problem?

> So the point is that the client will timeout before receving any response or
> indication that anything is happening.  The trick is only to read what I can
> send in the next few secs, that way the email client stays interested
> because it gets to write from time to time.  Is this clear?

Not entirely...

I assume that the "email client" is something which is
sending your program data over the fast connection, right?

Further, I'll assume that it's on the same machine as you're on.

This means that when it writes to you, the effect is just the same as
when you write to the slow modem -- that is, syswrite copies data into
an Operating System buffer, and then it returns immediately, even if
your program hasn't yet read the data.

Thus, no matter how slowly you read the data, your email client will
manage to write all of it's to you nearly instantly, and will then be
waiting for a response.

 ...................................

It seems to me that you should just read the data as fast as you can,
and write to the client an 'ok' message of some sort, and close the
client's connection.  If, after the data fully transmits over the slow
modem, you discover that there was an error, send yourself an email
saying so.

> Thanks for your help.  Really appreciated
> 
> (FWIW: I tried under perl 5.8 on Win32 and the same strange effect seems to
> be happening.  I guess I will just need to keep writing to the socket a
> little at a time and just stop when can_write returns false - there doesn't
> seem to be any other way to determine when the OS buffer is full - syswrite
> ALWAYS accepts the data...)

Syswrite always accepts *at least some* data, because if the OS buffer
is full, it will of course *block* until the OS buffer is *no longer*
full.  I have not ever said anything to the contrary.

Whether you write in small chunks or large chunks *will not*, *in any
way* change this behavior.

 ...................................

If you call $socket->sync, then your program will block until the OS
buffer is empty, if your operating system implements the fsync(fd) C
function.  If your email client calls this occasionally on the handle to
you, and if you call it occasionally on the handle to the slow modem,
then you might be able to avoid whatever timeout problems you're having.


-- 
$;=qq qJ,krleahciPhueerarsintoitq;sub __{0 &&
my$__;s ee substr$;,$,&&++$__%$,--,1,qq;;;ee;
$__>2&&&__}$,=22+$;=~y yiy y;__ while$;;print


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

Date: 21 Feb 2003 23:56:19 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: Religious question: commenting
Message-Id: <slrnb5df53.haa.abigail@alexandra.abigail.nl>

Helgi Briem (helgi@decode.is) wrote on MMMCDLXI September MCMXCIII in
<URL:news:3e560e1e.1208553598@news.cis.dfn.de>:
**  
**  That sort of 'non-commenting' is usually much better 
**  than excessive comments.
**  
**  I absolutely hate this sort of thing:
**  
**  # Open the phonebook file for appending:
**  open I, ">>$i" or die $!;
**  
**  when something like this would have been much
**  clearer and needed no comments at all:
**  
**  open PHONEBOOK, $phonebook 
**  or die "Cannot open $phonebook for appending:$!\n";


I dunno, but I prefer brief error messages over wrong ones.
The "doesn't need comments" example doesn't open for appending.
(Unless $phonebook starts with '>>', but then I surely would
prefer the former piece of code).


Abigail
-- 
sub f{sprintf'%c%s',$_[0],$_[1]}print f(74,f(117,f(115,f(116,f(32,f(97,
f(110,f(111,f(116,f(104,f(0x65,f(114,f(32,f(80,f(101,f(114,f(0x6c,f(32,
f(0x48,f(97,f(99,f(107,f(101,f(114,f(10,q ff)))))))))))))))))))))))))


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

Date: 21 Feb 2003 20:42:06 -0800
From: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
Subject: Re: Religious question: commenting
Message-Id: <3e56ff9e@news.victoria.tc.ca>

Abigail (abigail@abigail.nl) wrote:
: Helgi Briem (helgi@decode.is) wrote on MMMCDLXI September MCMXCIII in
: <URL:news:3e560e1e.1208553598@news.cis.dfn.de>:
: **  
: **  That sort of 'non-commenting' is usually much better 
: **  than excessive comments.
: **  
: **  I absolutely hate this sort of thing:
: **  
: **  # Open the phonebook file for appending:
: **  open I, ">>$i" or die $!;
: **  
: **  when something like this would have been much
: **  clearer and needed no comments at all:
: **  
: **  open PHONEBOOK, $phonebook 
: **  or die "Cannot open $phonebook for appending:$!\n";


: I dunno, but I prefer brief error messages over wrong ones.
: The "doesn't need comments" example doesn't open for appending.
: (Unless $phonebook starts with '>>', but then I surely would
: prefer the former piece of code).

Redundant comments can be bad.  If they describe how something is done but
get out of sync then they are misleading.

But a redundant comment that says what is *supposed* to happen can be self
evidently justified precisely by getting out of sync with the code. 

The real issue with example two is that it tries to combine two seperate
issues into one item, which is often a bad idea. 

One issue is to inform the programmer what the code is supposed to be
doing.  The second issue is to report a run time error. 

As Abigail points out, if the file is not opened for appending then the
runtime message is wrong.  As a comment it is not really that correct
either.  It does not really say that the file is supposed to be opened in
append mode, that's just implied as an almost incidental aside.  A
programmer could just as easily read that and say "that message looks
wrong because the file is not being opened in append mode - lets fix the
message to correctly report the file open error".


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

Date: 21 Feb 2003 16:42:48 -0800
From: phlip_cpp@yahoo.com (Phlip)
Subject: Save and restore Tk UI state via cute, not brute
Message-Id: <63604d2.0302211642.64bff42e@posting.google.com>

Perlies:

If I were naive, I would save the state of a UI to a flat text file
using many lines, like this:

  print >FILE, "Notebook tab = " . $notebook->currentTab() . "\n";
  print >FILE, "First List Box = " . $listbox->curselection() . "\n";
  print >FILE, "Second List Box = " . $listbox2->curselection() .
"\n";
  print >FILE, "Edit field = " . $entry->get() . "\n";
  print >FILE, blah blah blah...

(That might not be Perl, or Tk. That's not the question.)

Then similar code would drag and split those variables out, and put
them back into their controls.

It seems that's a lot of duplication. The $listbox knows that it is
the first list box, it knows that its state appears in the
curselection() method (or lower), etc. So the name of the list box,
and its state identity, are duplicated.

And we dupe the 'print' statement mechanics, over and over again. The
toplevel window knows it has all these controls, so we could use it to
write some kind of loop statement, right?

Duplication is bad.

I seek something like this:

	http://www.c2.com/cgi/wiki?InspectEvalFileFormat

In that pattern, we'd use a primitive like 'inspect' (mebbe
Data::Dumper()) to write Perl source. Then at load time we'd read that
source and eval() it.

That would use all the native abilities, elegantly, without
duplication. It would use cute code, not brute force.

Has anyone done something like this to save and restore Tk state?

-- 
  Phlip


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

Date: Sat, 22 Feb 2003 01:38:14 GMT
From: Bob Walton <bwalton@rochester.rr.com>
Subject: Re: setting permissions on my server for cgi
Message-Id: <3E56D469.3080804@rochester.rr.com>

chris wrote:

> i am having a few problems getting cgi scripts to work on my server.
> 
> my directory is http://www.rit.edu/~cpb6043/cgi/
> 
> as you can see, test.cgi works and test2.cgi does not.
> 
> i talked with my local network administrators, and they got it to work. they
> said (using telnet) to set the permissions for each file to 755
> (read,execute) and the folder to the same thing (i think). i tried this and
> it still doesn't work. the contents of test.cgi is:
> #!/usr/local/bin/perl
> 
> print "Content-type: text/html\n\n";
> 
> print "HELLO";
> 
> i have checked to make sure that the path to perl is right. it is.
> 
> i have checked to make sure that this is the proper way to send the header.
> it is.
> 
> i do not know why this doesn't work.
 ...


> chris.


Well, you showed us the contents of the one that *does* work.  What is 
the contents of the one that *doesn't* work?

Have you followed everything in the FAQ?  (that's perldoc -q 500).  Oh, 
guess not -- it says there to submit requests like yours to a different 
newsgroup.  Where they talk about server configuration and file 
permissions, rather than Perl.  Hint: this newsgroup talks about Perl 
and not about server configurations and file permissions.

BTW, conspicuously absent from your CGI script is the statement:

     use CGI;

which is a *huge* help in writing CGI scripts.  With it you, for 
example, don't have to worry about the correct format of the HTTP header 
-- and you would probably actually get it right, including specification 
of the charset.


-- 
Bob Walton



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

Date: Sat, 22 Feb 2003 01:40:20 -0500
From: "chris" <cpb6043@osfmail.rit.edu>
Subject: Re: setting permissions on my server for cgi
Message-Id: <3e571acc@news.isc.rit.edu>

i do my best to read the documentation, but it's very frustrating with the
amount of reading required. it requires time, and patience that i do not
often have to devote to this.
i tried doing use cgi; that didn't work. thanks anyway.what newsgroup should
i try posting this to?
"Bob Walton" <bwalton@rochester.rr.com> wrote in message
news:3E56D469.3080804@rochester.rr.com...
> chris wrote:
>
> > i am having a few problems getting cgi scripts to work on my server.
> >
> > my directory is http://www.rit.edu/~cpb6043/cgi/
> >
> > as you can see, test.cgi works and test2.cgi does not.
> >
> > i talked with my local network administrators, and they got it to work.
they
> > said (using telnet) to set the permissions for each file to 755
> > (read,execute) and the folder to the same thing (i think). i tried this
and
> > it still doesn't work. the contents of test.cgi is:
> > #!/usr/local/bin/perl
> >
> > print "Content-type: text/html\n\n";
> >
> > print "HELLO";
> >
> > i have checked to make sure that the path to perl is right. it is.
> >
> > i have checked to make sure that this is the proper way to send the
header.
> > it is.
> >
> > i do not know why this doesn't work.
> ...
>
>
> > chris.
>
>
> Well, you showed us the contents of the one that *does* work.  What is
> the contents of the one that *doesn't* work?
>
> Have you followed everything in the FAQ?  (that's perldoc -q 500).  Oh,
> guess not -- it says there to submit requests like yours to a different
> newsgroup.  Where they talk about server configuration and file
> permissions, rather than Perl.  Hint: this newsgroup talks about Perl
> and not about server configurations and file permissions.
>
> BTW, conspicuously absent from your CGI script is the statement:
>
>      use CGI;
>
> which is a *huge* help in writing CGI scripts.  With it you, for
> example, don't have to worry about the correct format of the HTTP header
> -- and you would probably actually get it right, including specification
> of the charset.
>
>
> --
> Bob Walton
>




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

Date: 21 Feb 2003 23:34:56 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Switch order of sprintf conversions
Message-Id: <b36d30$m3d$1@mamenchi.zrz.TU-Berlin.DE>

Mona Wuerz  <wuerz@yahoo.com> wrote in comp.lang.perl.misc:
> anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) writes:
> 
> > Josef Drexler  <nospam1202@joesbox.cjb.net> wrote in comp.lang.perl.misc:
> > > Anno Siegel wrote:
> > > > I'm amazed the need to rearrange arguments hasn't come up earlier.
> 
> As a matter of fact, it has - in de.clpm. See, e.g. 
> <jktao9.8sn.ln@al.brain.de> and the ensuing thread (in German).

My (ambiguous) use of "earlier" was aimed at the history of the particular
program, not Perl groups in general.  I would have thought that in a
multilingual system described as "mature" the need (or at least a wish)
to rearrange word order would have come up before.

> > > That's a good idea, but I think it would be more useful to have this
> > > permutation specified as part of the format string, because it will be
> > > necessary in only some instances, and a different permutation may be
> > > necessary for each string.
> > > 
> > > It would probably be worthwhile to write a substitute for sprintf that
> > > supports the "%2$d" notation of the C sprintf.
> 
> As a first stab at it, I came up with
> 
> #!perl -w
> use strict;
> 
> sub fmt {
>   my $fmt = shift;
>   my @fmts;
>   my $i=0;
>   while ($fmt =~ s/(\%\d?\$?\d?\.?\d?[%csduoxefgXEGbpn])/\$fmts[$i]/) {
>     push @fmts, $1; $i++;
>   }
>   @fmts = map  { $_->[0] }
>           sort { $a->[1] <=> $b->[1] }
>           map  { [ $_, s/\%(\d*)\$/\%/?$1:0 ] } @fmts;
>   $fmt =~ s/\$fmts\[(\d+)\]/$fmts[$1]/g;
>   $fmt =~ s/\\n/\n/g;	# etc...
>   $fmt
> }
> 
> printf "%.3f + %s = %g\n", 1.234567, 'foo', 42*1_000_000; 
> printf fmt ('%2$.3f + %1$s = %g\n'), 1.234567, 'foo', 42*1_000_000; 
> printf fmt ('%2$.3f + %3$s = %1g\n'), 1.234567, 'foo', 42*1_000_000; # oops!
> 
> __END__
> 
> That obviously doesn't allow for variable interpolation in the format
> string; it is also rather halfheartedly tested otherwise. But it's a start.

It also doesn't accept multi-digit numbers, but that's probably a
simplification for demo purposes.  A minor nitpick is that "%" doesn't
have to be escaped in a regex (far less on the substitution side of s///).

The regexes could imo profit from more grouping: "\d?\$?" accepts an
(invalid) single "$", "(?:\d+\$)?" would not (and deal with multiple
digits).  I'd group the field-width and precision parts along a similar
vein.  I like the way you deal with the pesky "%%" by making "%" a valid
type specifier.  It is certainly a start.

[...]

Anno
-- 
perl -e'$_=shift().".pm";s|::|/|g;require;exec"view",$INC{ $_}' File::Find


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

Date: 21 Feb 2003 21:16:57 -0500
From: Mona Wuerz <wuerz@yahoo.com>
Subject: Re: Switch order of sprintf conversions
Message-Id: <m3bs15vz3a.fsf@karrooite.njitdm.campus.njit.edu>

anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) writes:

> Mona Wuerz  <wuerz@yahoo.com> wrote in comp.lang.perl.misc:
> > anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) writes:
> >
> > > > > I'm amazed the need to rearrange arguments hasn't come up earlier.
> > 
> > As a matter of fact, it has - in de.clpm. See, e.g. 
> > <jktao9.8sn.ln@al.brain.de> and the ensuing thread (in German).
> 
> My (ambiguous) use of "earlier" was aimed at the history of the particular
> program, not Perl groups in general.  I would have thought that in a
> multilingual system described as "mature" the need (or at least a wish)
> to rearrange word order would have come up before.

I wouldn't be surprised that rearranging word order in sentences isn't
all that frequently needed, and therefore more an afterthought, maybe
only brought up by the occasional exotic native speaker after the
project has already been released. But I'm guessing wildly.

Just as a side note, the reason that I even remembered was that the
person who asked about this (rearranging sprintf's format) needed it
for exactly the same purpose (use of gettext for i18n).  And back then
I hadn't ever heard of gettext, so it stuck.

-- 
$_="\n,rekcah egnufeB rehtona tsuJ";#v1<?>g\:pv-<5<
s s\S+(?:B)sunpack'u',q q$;')E4qsee;#>60#^<(v!<)g6<
print scalar reverse,$@ unless m,[+;#]55,;m:_,:#^1<


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

Date: Sat, 22 Feb 2003 01:23:32 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Switch order of sprintf conversions
Message-Id: <3E571764.68DE835@earthlink.net>

Gunnar Hjalmarsson wrote:
> 
> I'm using a gettext like approach for making it easy to translate a
> Perl program to non-English languages, and the sprintf function is
> often used for the purpose:
> 
>      $string = gettext("There Is %s Than %s Way To Do It");
>      $more = gettext("More");
>      $one = gettext("One");
>      print sprintf($string, $more, $one);
> 
> Right now the program is translated into Chinese, and the translator
> has reported a need to have the value of $one printed *before* the
> value of $more. I understand from the gettext documentation that if it
> had been a C program, $string could have been assigned (the equivalent
> of) "There Is %2$s Than %1$s Way To Do It".
> 
> Is there a way in Perl to achieve the same thing?

Here's a summary of what's been suggested:

1/ Upgrade to perl5.8.0, and write it with '2$' and '1$' things in there
(remember to use '' or q//, not "" or qq// ... or alternatively, to
escape the dollar signs).

2/ Write a function which will rearrange the arguments to sprintf in a
way dependent on the locale and on the string requested.  I say that
there's a dependency on the string requested, since I'm fairly sure that
Chinese wouldn't *always* rearrange the arguments in single particular
way (in comparison to how they're arranged for english); each phrase may
require a different permutation.

3/ Write a parser for sprintf formats which handles '2$' and '1$'
thingies, and permutes the argument list and reformats the format
string, to produce an ordinary sprintf format string, and then use
perl's builtin sprintf.

And my own idea, which noone seems to have mentioned:

4/ Have gettext return a string which would be eval()ed into a coderef,
calling that coderef with the args produces an appropriate string. eg:
   $code = gettext(
      'sub { qq[There Is $_[0] Than $_[1] Way To Do It] }');
   $more = gettext("More");
   $one = gettext("One");
   print eval($code)->($more, $one);
And, for the argument-reversed thingie, gettext would put into $code a
string like:
   'sub { qq[There Is $_[1] Than $_[0] Way To Do It] }'



-- 
$;=qq qJ,krleahciPhueerarsintoitq;sub __{0 &&
my$__;s ee substr$;,$,&&++$__%$,--,1,qq;;;ee;
$__>2&&&__}$,=22+$;=~y yiy y;__ while$;;print


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

Date: 21 Feb 2003 15:32:41 -0800
From: noreply@gunnar.cc (Gunnar Hjalmarsson)
Subject: Use or not to use modules [was: How do I ...]
Message-Id: <3fcbff18.0302211532.59533c9a@posting.google.com>

"Steffen Beyer" <Steffen.Beyer@de.bosch.com> wrote in message news:<b34itt$hn8$1@ns2.fe.internet.bosch.com>...
> But in general, it is of course a good idea not to re-invent
> the wheel and to use modules wherever possible.
> 
> I'm the last to object because I'm a module author myself. :-)

I don't agree on "wherever possible".

I started to write my first Perl/CGI program about three years ago
with (almost) no programming experience. I applied the "learning by
doing" approach. Don't we all, btw? ;-)

Anyway, personally I like to really understand what I'm doing. I
believe that most often, when people use modules, they don't care to
learn how they really work. Another aspect is that some modules are
very large, and require quite some effort before you can use them and
still feel reasonably comfortable.

Consequently, in order to be in control and more rapidly increase my
own Perl/CGI knowledge, I prefer to "re-invent the wheel", before
using modules, for tasks that I consider to be *simple enough*.
Nevertheless, I often study modules without using them, and sometimes
I copy methods from them.

Recently I uploaded my first CPAN module, CGI::ContactForm
(http://search.cpan.org/author/GUNNAR/), and, believe it or not, even
if it's a pure CGI module, I'm not using CGI.pm.

I Guess I'm expressing a slightly provocative view here. But am I
really that wrong?

/ Gunnar

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


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

Date: 22 Feb 2003 07:39:40 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@post.rwth-aachen.de>
Subject: Re: Use or not to use modules [was: How do I ...]
Message-Id: <b379fs$7ru$1@nets3.rz.RWTH-Aachen.DE>

Also sprach Gunnar Hjalmarsson:

> "Steffen Beyer" <Steffen.Beyer@de.bosch.com> wrote in message news:<b34itt$hn8$1@ns2.fe.internet.bosch.com>...
>> But in general, it is of course a good idea not to re-invent
>> the wheel and to use modules wherever possible.
>> 
>> I'm the last to object because I'm a module author myself. :-)
> 
> I don't agree on "wherever possible".
> 
> I started to write my first Perl/CGI program about three years ago
> with (almost) no programming experience. I applied the "learning by
> doing" approach. Don't we all, btw? ;-)

Yes, we do. On the other hand modules are an integral part of Perl. One
has to learn this part as well. I am not referring to knowing the
interface of all the modules by heart, but rather finding a suitable
one, making it work and making it match the requirements you have for a
given task.

I also remember that I learnt some of Perl's language aspects through
modules. Among my first scripts was one that used DBI and that was
clearly too complicated for me by this time. But because I used DBI I
learned some things about nested data-structures. Perl modules cover a
wide range of Perl: You have purely functional modules, object-oriented
ones (where you only instantiate a few objects). Then there is something
like HTML::Parser that needs subclassing and teaches you the use of
event-driven models. And so on. I learnt have of my Perl by using the
modules, the other half perhaps by reinventing the wheel.

> Anyway, personally I like to really understand what I'm doing. I
> believe that most often, when people use modules, they don't care to
> learn how they really work. Another aspect is that some modules are
> very large, and require quite some effort before you can use them and
> still feel reasonably comfortable.

This is part of the learning-curve. A little bit like an excercise: In
order to use this and that feature of a module, you have to understand
some basics and learn those first.

> Consequently, in order to be in control and more rapidly increase my
> own Perl/CGI knowledge, I prefer to "re-invent the wheel", before
> using modules, for tasks that I consider to be *simple enough*.
> Nevertheless, I often study modules without using them, and sometimes
> I copy methods from them.

I think there is a distinction between knowing Perl the language and
the domain in which you intend to use it. If you want to learn how CGI
works then write your own routines. They'll probably be full of bugs and
security holes in the beginning but that's part of the process.

> Recently I uploaded my first CPAN module, CGI::ContactForm
> (http://search.cpan.org/author/GUNNAR/), and, believe it or not, even
> if it's a pure CGI module, I'm not using CGI.pm.

Module authors may have a different motivation for doing a thing in a
certain way. In a way this is also indulgence with Perl in an almost
artistic way. The average user of Perl however needs to solve a specific
task and often doesn't care that much about the work behind the scenes.
Throwing in a module is fine if it does the job. I observed that during
the last year or two I was hardly writing any Perl or C to solve
real-life problems. I was experimenting with modules and such. Some have
made it to the CPAN but oddly enough I never used them so far in my own
scripts. Perl seems to have become an end in itself for me. :-)

> I Guess I'm expressing a slightly provocative view here. But am I
> really that wrong?

I think this view is common for module authors. They have to make quite
a lot of political decisions. Using a particular module in one's own
module is often such a political thing. In order to please our own
vanity we reinvent quite a bit of wheels and yet there is nothing wrong
with that.

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


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

Date: Fri, 21 Feb 2003 17:31:14 -0600
From: "Peter Shankey" <oxmard.Rules@ab.ab>
Subject: Where does the 1 come from
Message-Id: <M0SdncmHW7lfK8ujXTWc3w@comcast.com>

if I do this:

 print ("\n \$_ is: ", $_, "\n");
  print "\n\n\n";
  $_ = 5;
  print ("\n\$_ is: ", $_, "\n");
  print $_ for print ("print ", $_, "\n");
  print ("\n\$_ is: ", $_, "\n");
  print my $i = $_ for print ("print ", $_, "\n");
  print ("\ni is: ", $i, "\n");

I get this:
$_ is: 5
print 5
1
$_ is: 5
print 5
1
i is:

I am not understanding where the 1 is coming from. I guess it has to be
coming from the for somehow. Also why get I not set $i to that value?

Thanks




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

Date: Fri, 21 Feb 2003 19:07:56 -0500
From: Jeff 'japhy' Pinyan <pinyaj@rpi.edu>
Subject: Re: Where does the 1 come from
Message-Id: <Pine.SGI.3.96.1030221190728.429559B-100000@vcmr-64.server.rpi.edu>

On Fri, 21 Feb 2003, Peter Shankey wrote:

>  print $_ for print ("print ", $_, "\n");

That prints "print $_\n", and returns 1, the return value of print(), so
you get

  print $_ for 1;

-- 
Jeff Pinyan            RPI Acacia Brother #734            2003 Rush Chairman
"And I vos head of Gestapo for ten     | Michael Palin (as Heinrich Bimmler)
 years.  Ah!  Five years!  Nein!  No!  | in: The North Minehead Bye-Election
 Oh.  Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)



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

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


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