[30930] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2175 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Jan 31 16:09:49 2009

Date: Sat, 31 Jan 2009 13:09:12 -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, 31 Jan 2009     Volume: 11 Number: 2175

Today's topics:
    Re: cgi style % decode and utf-8: missing something <hjp-usenet2@hjp.at>
    Re: cgi style % decode and utf-8: missing something <whynot@pozharski.name>
    Re: CGI.pm and Use of uninitialized value in pattern ma <bart.lateur@pandora.be>
    Re: Constructing a scalar reference <rvtol+usenet@xs4all.nl>
    Re: Constructing a scalar reference sln@netherlands.com
    Re: Constructing a scalar reference <kst-u@mib.org>
    Re: Perl IP to Domain Name converter. <1usa@llenroc.ude.invalid>
    Re: Perl IP to Domain Name converter. <tadmc@seesig.invalid>
    Re: Perl IP to Domain Name converter. <cwilbur@chromatico.net>
    Re: Perl Peeves <hjp-usenet2@hjp.at>
    Re: Perl Peeves <hjp-usenet2@hjp.at>
    Re: Perl Peeves <rvtol+usenet@xs4all.nl>
    Re: Perl Peeves <hjp-usenet2@hjp.at>
    Re: unix - rsync - filter question <hjp-usenet2@hjp.at>
    Re: Want to write a script to do the batch conversion f <hjp-usenet2@hjp.at>
    Re: Want to write a script to do the batch conversion f <someone@example.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sat, 31 Jan 2009 10:27:22 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: cgi style % decode and utf-8: missing something
Message-Id: <slrngo86bq.lsv.hjp-usenet2@hrunkner.hjp.at>

On 2009-01-29 18:15, Eli the Bearded <*@eli.users.panix.com> wrote:
> I have some strings of UTF-8 that were octet by octet encoded into
> CGI style %XX strings. I would like to get back the Unicode code points.
> When I decode the strings, I get things that print as UTF-8, but are
> not recognized as UTF-8 for the purposes of ord().

ord doesn't expect an UTF-8 string, it expects a character string.

I repeat my mantra that whoever had the brilliant idea of calling perl
character strings "utf8 strings" ought to be handed over to sln for
spanking (and it that doesn't help, he ought to be forced to read sln's
code). That causes no end to confusion.

perl has (since 5.8.0) "byte strings" and "character strings". 

If you have a byte string "\x{CF}\x{B1}", this is a string of two
elements (bytes). ord("\x{CF}\x{B1}") with return the code of the first
element of this string, i.e., 0xCF (207 decimal).

If you know that this string is a text in UTF-8 encoding (and not a text
in latin-1 encoding, or a bit vector, or a 16 bit integer in big endian
encoding, or whatever), you need to decode it:

    use Encode;
    my $text = decode('UTF-8', "\x{CF}\x{B1}");

Now $text will contain a character string with a single element
(character) with the code 0x3F1 (1009 decimal).


Similarly, when you want to encode a character string to a byte string,
you need to call encode with the encoding you want.

decode_utf8 (which you mentioned in a later posting) is of course just a
(possibly optimized) shorthand for decode('UTF-8', ...).

> has not posted here in years

Welcome back!

	hp


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

Date: Sat, 31 Jan 2009 15:40:44 +0200
From: Eric Pozharski <whynot@pozharski.name>
Subject: Re: cgi style % decode and utf-8: missing something
Message-Id: <slrngo8l6s.jp9.whynot@orphan.zombinet>

On 2009-01-31, Peter J. Holzer <hjp-usenet2@hjp.at> wrote:
*SKIP*
> decode_utf8 (which you mentioned in a later posting) is of course just a
> (possibly optimized) shorthand for decode('UTF-8', ...).

Yes, no, it depends.  It does some black magic, IOW (F<Encode.pm>, line
194).

sub decode_utf8($;$) {
    my ( $str, $check ) = @_;
    return $str if is_utf8($str);
    if ($check) {
        return decode( "utf8", $str, $check );
    }
    else {
        return decode( "utf8", $str );
        return $str;
    }
}

What I can get is: why it does C<return decode>, then C<return $str>.

*CUT*

p.s.  BTW, B<encode_utf8> is wrapper about B<utf8::encode>.

-- 
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom


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

Date: Sat, 31 Jan 2009 17:49:00 +0100
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: CGI.pm and Use of uninitialized value in pattern match
Message-Id: <t009o49nkc08bukpmv5pdp7ih5tu0595ba@4ax.com>

A. Farber wrote:

>afarber@ablsw01:~> ~/yapassgen.pl
>File is not a perl storable at ../../lib/Storable.pm (autosplit
>into ../../lib/auto/Storable/_retrieve.al) line 331, at /home/afarber/
>perl/lib/perl5/site_perl/5.8.8/Crypt/YAPassGen.pm line 91

>my $passgen = Crypt::YAPassGen->new(
>        freq        =>  '/usr/share/dict/linux.words',
>        length      =>  10,
>        post_subs   =>  [sub { $_ = uc }, "digits"],
>);

The docs say:

    freq       =>  '/path_to_american-english_default_freq_file.dat',

So, judging by that file extension, you're using thewrong kind of file.
Also, I don't see how a word list could be interpreted as " frequency
database".

Well, there's this entry in the docs a bit further downn:

  my $freq = Crypt::YAPassGen->make_freq($dict_file, $freq_file, $ascii)

    This class method will generate a new frequency file reading from
    $dict_file and writing the result in $freq_file. 

Well, there you have it... you have to generate that freq file from your
wordlist file, first.

-- 
	Bart.


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

Date: Sat, 31 Jan 2009 13:00:13 +0100
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: Constructing a scalar reference
Message-Id: <49843d4e$0$183$e4fe514c@news.xs4all.nl>

Chet Butcher wrote:

> In the following sequence
> 
> $r = {}; # a hashref
> $r = []; # an arrayref
> $r = ?; # a scalar ref
> 
> What is ? ? I want to pass a ref to a scalar (pass by reference)

$ perl -wle'
   my $v = 1;
   my $r = \$v;
   $$r = 2;
   print $v;
'
2

-- 
Ruud


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

Date: Sat, 31 Jan 2009 17:19:55 GMT
From: sln@netherlands.com
Subject: Re: Constructing a scalar reference
Message-Id: <tq19o4tt8cllnkqbseh39ojs47uu98jc47@4ax.com>

On Fri, 30 Jan 2009 22:47:53 +0000, Chet Butcher <invalid@invalid.invalid> wrote:

>Hi
>
>In the following sequence
>
>$r = {}; # a hashref
>$r = []; # an arrayref
>$r = ?; # a scalar ref
>
>What is ? ? I want to pass a ref to a scalar (pass by reference)
>without resorting to
>
>  my $r;
>  mySub( \$r );
>
>I just want to use
>
>  my $r = (something);
>  mySub( $r );
>
>to be consistent with
>
>  my $r = {}; # or  my $r = [];
>  mySub( $r );
>
>I know it's not a big drama on the surface, but I'm trying to overload
>the method to return various results depending on the reference type,
>and I dont want the \ in some calls and not others. 
>
>Thanks

Not sure that overloads can be done in Perl, maybe.
Wether the method expects a reference, and what type of reference, or not, is up to you.
Use alias parameter processing, calls can be general, figure out specifics in the method.
Then you could return success while processing data directly (one schema - are many more).

sln

-------------------------------------
sub ProcessData
{
	return 0 if (@_ < 1);
	my $Dataref;
	if (!length( ref($_[0]) ) {
		$Dataref = \$_[0];
	} else {$Dataref = $_[0]}
	shift;

	if (ref($Dataref) eq 'SCALAR') {
		return ProcessScalar($Dataref, @_);
	}
	if (ref($Dataref) eq 'ARRAY') {
		return ProcessArray($Dataref, @_);
	}
	if (ref($Dataref) eq 'HASH') {
		return ProcessHash($Dataref, @_);
	}
	return 0;
}

sub ProcessScalar
{
	my ($scalar_ref, ...) = @_;
}
sub ProcessArray
{
	my ($array_ref, ...) = @_;
}
sub ProcessHash
{
	my ($hash_ref, ...) = @_;
}



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

Date: Sat, 31 Jan 2009 10:38:46 -0800
From: Keith Thompson <kst-u@mib.org>
Subject: Re: Constructing a scalar reference
Message-Id: <lnpri3cocp.fsf@nuthaus.mib.org>

Chet Butcher <invalid@invalid.invalid> writes:
> In the following sequence
>
> $r = {}; # a hashref
> $r = []; # an arrayref
> $r = ?; # a scalar ref
>
> What is ? ? I want to pass a ref to a scalar (pass by reference)
> without resorting to
[snip]

Chet, you posted this to comp.lang.perl.misc.  Why on Earth did you
set followups to comp.lang.c?

(I've cross-posted this to comp.lang.perl.misc and comp.lang.c, and
set followups back to comp.lang.perl.misc).

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"


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

Date: Sat, 31 Jan 2009 13:55:07 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Perl IP to Domain Name converter.
Message-Id: <Xns9BA45AB9665DAasu1cornelledu@127.0.0.1>

Hongyi Zhao <hongyi.zhao@gmail.com> wrote in 
news:95b8o49ba1su49rhnpo4f8271u53rh6hlh@4ax.com:

> Hi all,
> 
> I find a Perl IP to Domain Name converter from the following url:
> 
> http://www.osix.net/modules/article/?id=194
> 
> I copy the code to a perl script named PerlIP2DomainName.pl, when I
> run it I meet the errors like this:
> 
>>-----------------------------<
> 
> $ ./PerlIP2DomainName.pl
> Global symbol "$infilePlacing" requires explicit package name at
> ./PerlIP2Domain
> Name.pl line 28.
> syntax error at ./PerlIP2DomainName.pl line 31, near "=)"
> syntax error at ./PerlIP2DomainName.pl line 41, near "}"
> Execution of ./PerlIP2DomainName.pl aborted due to compilation errors.
> 

 ...

> 
> Any hints on this issue? 
> 

You don't know Perl at all, do you?

You have no intention of learning Perl, do you?

You just want someone else to write you a script, don't you?

Syntax errors like that are easy to diagnose and fix if you know a 
little bit of Perl.

However, I don't see the point of spending time on the particular script 
you copied from some web site.

If all you want is a log file processor, there are oodles of them on the 
interweb.

Sinan

-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/


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

Date: Sat, 31 Jan 2009 08:40:47 -0600
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Perl IP to Domain Name converter.
Message-Id: <slrngo8onf.11k.tadmc@tadmc30.sbcglobal.net>

Hongyi Zhao <hongyi.zhao@gmail.com> wrote:


> I find a Perl IP to Domain Name converter from the following url:
>
> http://www.osix.net/modules/article/?id=194
>
> I copy the code to a perl script named PerlIP2DomainName.pl, when I
> run it I meet the errors like this:
>
>>-----------------------------<
>
> $ ./PerlIP2DomainName.pl
> Global symbol "$infilePlacing" requires explicit package name at
> ./PerlIP2Domain
> Name.pl line 28.
> syntax error at ./PerlIP2DomainName.pl line 31, near "=)"
> syntax error at ./PerlIP2DomainName.pl line 41, near "}"
> Execution of ./PerlIP2DomainName.pl aborted due to compilation errors.
>
>>-----------------------------<
>
> Here is the perl code:


If there are syntax errors, then what you have is not a Perl program...


> #!/usr/bin/perl -w 
> use Socket; 
> use Getopt::Std; 
> use strict; 
> # simple script to resolve ip addresses in a web log file 
> # into host names 
> # typical logline: 
> # 213.123.213.254 - - [11/Feb/2002:13:29:14 +0000] "GET
> /phppolls/phppolls.php?poll_action=viewPoll&poll_id=22 HTTP/1.1" 200
> 5394 "http:#www.sonictown.co.uk/main/main.php" "Mozilla/4.0
> (compatible; MSIE 6.0; Windows NT 5.0)" 
>   
> my %options; 
> my ($infile, $outfile, %cached_ips, $line, @lines, $tmp, @bits,
> $newline, @newlist); 
> my $progname=$0; 
> # use basename only: 
> $progname=~s,.*/,,; 
> # parse our arguments: 
> getopts("dho:vf:", %options); 
> # if no infile, print usage: 
> if(!$options{f} || $options{h}){usage();} 
>  $infile=$options{f}; 
> # if no outfile given, default to $infile."new": 
> if(!$outfile){$outfile=$infile.".new";} 
>   
> # attempt to open infile: 
> open(IN, $infile) || die("Unable to open file: $infile"); 
> # attempt to open outfile: 
> open(OUT, ">$outfile") || die("Unable to open file: $outfile"); 
>  if($options{v}){ 
>         print "Taking input from: $infilePlacing output to: $outfile";


This is the first mention of $infilePlacing in the program.

It has not been declared, which violates the "use strict" pragma and
produces the first error message above.

Furthermore, it has not been given any value, so outputting it is not likely
to be productive anyway.


> sleep 2; 
> } 
>  while($line=){ 


Perhaps this was meant to be:

    while ($line = <IN>) {


Clearly this program has never ever been executed.

There are lots of bad "programs" on the wild interweb, you have found one
of them.

Even without the syntax errors, I can say that this was not written
by a good Perl programmer...


> Any hints on this issue? 


What you have found is garbage.

Keep looking.


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


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

Date: Sat, 31 Jan 2009 10:37:07 -0500
From: Charlton Wilbur <cwilbur@chromatico.net>
Subject: Re: Perl IP to Domain Name converter.
Message-Id: <863aezwkpo.fsf@mithril.chromatico.net>

>>>>> "HZ" == Hongyi Zhao <hongyi.zhao@gmail.com> writes:

    HZ> On Sat, 31 Jan 2009 13:55:07 GMT, "A. Sinan Unur"
    HZ> <1usa@llenroc.ude.invalid> wrote:

    >> You don't know Perl at all, do you?

    HZ> You're absolutely correct!  At least for now so:-)


    >> You have no intention of learning Perl, do you?
    >> 
    >> You just want someone else to write you a script, don't you?

    HZ> Shame on me, I'm just a very newbie and can not take much time
    HZ> to learn it presently.

Indeed.  Shame on you.

You have two options.  You can learn Perl, or you can hire a programmer.

For the former option, http://www.perl.org/books/beginning-perl/

For the latter option, http://jobs.perl.org/

In either case, good luck.

Charlton


-- 
Charlton Wilbur
cwilbur@chromatico.net


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

Date: Sat, 31 Jan 2009 09:19:49 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Perl Peeves
Message-Id: <slrngo82d5.lsv.hjp-usenet2@hrunkner.hjp.at>

On 2009-01-30 23:23, A Dude <jjcassidy@gmail.com> wrote:
> On Jan 27, 11:47 pm, Uri Guttman <u...@stemsystems.com> wrote:
>> >>>>> "AD" == A Dude
>>   AD>     $my_boolean=
>>
>>   AD> is illustrative to me. (Unless it might be a ' ')
>>
>> ' ' is a true value. it would be foolish to ever use it as just a
>> boolean if you want to print it.
>
> I forgot how much people on usenet like to argue--even if they have to
> forget the context in order to do it.
>
> The concept is the *illustrative value* of printing
>
> $my_boolean=
>
> Not the advisability of sticking just anything in $my_boolean.
> On a standard terminal/console you can't tell that it's not a ' '.

Well, on a standard terminal, you can't distinguish '0' from '0 ',
either, so that's rather pointless. If you are printing this only for
debugging reasons, you probably know about the context to tell that
"nothing visible printed" means false. If you don't, then you either
need to print delimiters (I usually use "<$variable>") or even write
some code to print invisible characters in hex (unfortunately,
Data::Dumper doesn't do this).

> That's why I left off with advice to use more mature printing tools.

Right.

	hp


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

Date: Sat, 31 Jan 2009 11:53:26 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Perl Peeves
Message-Id: <slrngo8bd9.vrv.hjp-usenet2@hrunkner.hjp.at>

On 2009-01-29 22:54, Bruce Cook <bruce-usenet@noreplybicycle.synonet.comnoreply> wrote:
> Peter J. Holzer wrote:
>> On 2009-01-27 09:08, Eric Pozharski <whynot@pozharski.name> wrote:
>>> On 2009-01-27, Tim McDaniel <tmcd@panix.com> wrote:
>>>> (5) That "special false".  I was going nuts trying to figure out what
>>>> was different between
>> 
>> [ '' in string context, 0 in numeric context ]
>> 
>>> That special false is evaluated to blah-blah-blah *immediately* since
>>> the context is known at compile time (am I right?).
>> 
>> No. Consider:
>> 
>> #!/usr/bin/perl
>> use warnings;
>> use strict;
>> 
>> my $bool = (1 < 0);
>> 
>> if (rand() < 0.5) {
>>     print "<$bool>\n";
>> } else {
>>     print 0 + $bool, "\n";
>> }
>
> Anyone who applies a numeric operator to a logical value and expects 
> consistent results is asking for trouble.

The program above does produce consistent results and is perfectly
well-defined in Perl. I am not asking for trouble, but demonstrating a
perfectly well-defined (though somewhat surprising) property of the
scalar value returned by (1 < 0). (Interestingly, if I change (1 < 0) to
(0 < 1), then I am asking for trouble).

> In strongly-typed languages you would get a compiler or run-time
> error/warning, however perl is a scripting language and is built to be
> flexible, assumes you know what you're doing and will silently oblige
> even the most horrendous abuses. 
>
> You actually have the same issue in C: false is defined as 0 and true is 
> !false.

However, in C, !0 is defined as 1. 

There is no "boolean" type in C (well, C99 has _bool, but ...) and all
the "logical" operators in C return a well-defined result (1 or 0) of
type int.


> I have seen a discussion recently where someone was using an 
> library "elegantly" and came unstuck on a different platform because he 
> assumed that the true returned from that library was 1 (as it was on his 
> development platform).  His usage became really interesting because the new 
> platform was returning -1 (binary all 1s), which is another common value 
> used for true.

This is however a problem with his use of this particular library, not a
problem with his use of C. 

    x = (1 > 0) + 5;

is perfectly well-defined in C and will always assign 6 to x.

If a function is defined as "returning a true value on success" of
course you cannot test this with if (function() == 1), you need to use 
if (function()).


> As a programmer you need to understand when you're crossing type boundaries 

And you also need to know when you are *not* crossing type boundaries. 
In C (1 > 0) + 5 doesn't cross a type boundary. Both (1 > 0) and 5 are
expressions of type int.


> and make sure at that point you force defined behavior  the || 0  construct 
> is a good clean way of making sure your falses are actually integer/cleanly 
> printable.

"actually integer" and "cleanly printable" have nothing to do with each
other. '', 'green', 'false' are all cleanly printable, but none of them
is an integer. The "special false" value is an integer (the integer
zero) and it is also printable (it prints as ''), but depending on your
expectations it may not be "cleanly printable" (a "normal" integer of
value 0 prints as '0', not '').


> Perl implicitly acknowledges data type differences in the fact that it uses 
> different binary operators for strings vs integers.

Actually, it does this because there is *no* difference in the data
type. In a strongly typed language you can use the same operators and
the compiler (or run-time environment) can figure out which operation
was meant from the (static or dynamic) type of the arguments. In perl,
there are only scalars, so you have to tell it whether you want a string
or a numeric comparison[1]. (Similarily, in B, there were different
operators for integer and floating point operations, because B was
typeless).

> Bool can occur in either which is why the imprecise definition of what
> you will actually have in a scalar when it's a bool result.

I don't understand the last sentence.

Perl has a very precise definition what will be recogized as true and
false. It also has a definition what value (the "special false") will be
returned by the "logical" operators when they return false. For some
reason it doesn't define what these operators return for the true value.
I am very sure that this omission has nothing to do with the
number/string ambivalence of scalars. I tend to think that the omission
is a simple oversight, but it might be loosely defined as "any true
value" on purpose.

	hp

[1] Strictly speaking the interpreter could figure it out at run-time.
But since the type (NV, IV, UV, PV, or a combination) can be changed
just by using a variable, this would be extremely confusing.


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

Date: Sat, 31 Jan 2009 13:24:30 +0100
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: Perl Peeves
Message-Id: <498442fe$0$185$e4fe514c@news.xs4all.nl>

Peter J. Holzer wrote:

> As we have seen, the true value of the
> relational operators is not defines, so (($x < $y) || '0') is not
> correct, you have to write ( $x < $y ? '1' : '0')[1]).

I also see !! being used, like: !!($x < $y).


$ perl -we'
   my ($x, $y) = (1, 2);
   print q{<}, defined() ? qq{$_:} : q{undef:}, !!$_, qq{>\n} for
     $x < $y,
     $x > $y,
     $x & $y,
     $x && $y,
     $x + $y,
     undef,
     q{},
     q{ },
   ;
'
<1:1>
<:>
<0:>
<2:1>
<3:1>
<undef:>
<:>
< :1>

-- 
Ruud


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

Date: Sat, 31 Jan 2009 21:57:00 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Perl Peeves
Message-Id: <slrngo9eos.14u.hjp-usenet2@hrunkner.hjp.at>

On 2009-01-31 12:24, Dr.Ruud <rvtol+usenet@xs4all.nl> wrote:
> Peter J. Holzer wrote:
>> As we have seen, the true value of the
>> relational operators is not defines, so (($x < $y) || '0') is not
>> correct, you have to write ( $x < $y ? '1' : '0')[1]).
>
> I also see !! being used, like: !!($x < $y).

That makes no sense. ($x < $y) already returns a "boolean" result (i.e.,
either the special false value or an undocumented true value (which
happens to be 1)). ! will negate this and the second ! will negate this
again, so the end result will still be either the special false value or
an undocumented true value (which happens to be 1).

If you trust ! to return 1 or special-false, you can also trust < to
return 1 or special false. If you don't trust < to return 1 or
special-false, you have no reason to trust ! to do this.

Where this idiom is useful is if you have an argument which is less
restricted. For example you have a function which returns a true value
on success (maybe even a "zero but true" value like '0E0') and false on
failure. Then you can count successful calls like this

my $count;
for (...)  {
    $count += !!my_function();
}

Of course this assumes that !0 is 1, which isn't documented.

In general, this idiom doesn't seem to be as useful in Perl as in C,
where !!  always turns its argument into an integer with value 1 or 0. 

	hp


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

Date: Sat, 31 Jan 2009 10:07:32 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: unix - rsync - filter question
Message-Id: <slrngo856l.lsv.hjp-usenet2@hrunkner.hjp.at>

On 2009-01-30 17:17, Jim Gibson <jimsgibson@gmail.com> wrote:
> In article <slrngo4p1d.ahn.tadmc@tadmc30.sbcglobal.net>, Tad J
> McClellan <tadmc@seesig.invalid> wrote:
>> Jim Gibson <jimsgibson@gmail.com> wrote:
>> > In Unix, errors are usually printed to STDERR, which is unbuffered (or,
>> > for the pedantic among us, "autoflushed").
>> 
>> 
>> Errr, no, STDERR is unbuffered if we're to believe the man page.

STDERR is a Perl stream. rsync isn't written in Perl, so this is
irrelevant for the operation of rsync (and doubly so for the effects on
the script in question - clearly rsync has to flush any buffers before
it exits, or they wouldn't be output at all).

>> > man 3 stderr

stderr is a C stream, so it would be relevant for rsync. But C doesn't
have "autoflush", only block-buffered, line-buffered, and unbuffered
streams.

>>    CONSIDERATIONS
>>      The stream stderr is unbuffered.
>
> I suspect that the term "unbuffered" means different things to
> different people.
>

To me "buffered output" means that the I/O-functions don't write
directly to the file handle. Instead they write into a fixed size
buffer, which is flushed if it gets full or if some other condition
which requires flushing occurs. "Unbuffered output" means that the
I/O-functions either write directly to the file handle or the buffer is
only one byte. 

In C, stderr is really unbuffered. If you write a string which is larger
than the buffer like this:

    char x[8002];
    for (int i = 0; i < 8000; i++) {
        x[i] = 'a';
    }
    x[8000] = '\n';
    x[8001] = '\0';
    fprintf(stderr, x);
    sleep(1);
    return 0;

depending on the implementation you either see a single write of 8001
bytes, or 8001 writes of a single byte. If you use stdout instead of
stderr, with a buffer size of 4096 bytes, you see two writes: 4096 bytes
are written immediately, and the rest (3905 bytes) are written after the
sleep (actually after the return from main, but you don't see that with
strace).

In Perl (or at least in perl), STDERR is autoflushed. 

    my $x = ("a" x 8000) . "\n";
    print STDERR $x;
    sleep(1);

You see two writes: First 4096 bytes (the buffer is full and must be
flushed), then 3905 bytes (the buffer is autoflushed), and only then you
see the sleep.

	hp




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

Date: Sat, 31 Jan 2009 09:28:42 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Want to write a script to do the batch conversion from domain name to IP.
Message-Id: <slrngo82tq.lsv.hjp-usenet2@hrunkner.hjp.at>

On 2009-01-30 17:27, Tad J McClellan <tadmc@seesig.invalid> wrote:
> Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
>> On Fri, 30 Jan 2009 08:52:33 -0600, Tad J McClellan
>><tadmc@seesig.invalid> wrote:
>>>    my $outfile = '/some/other/myIP.txt';
>>>    open my $OTHER, '>', $outfile or die "could not open '$outfile' $!";
>>
>> I use the following two lines:
>>
>> my $outfile = '~/myIP.txt';
>> open my $OTHER, '>', $outfile or die "could not open '$outfile' $!";
>
>
> Using tilde (~) to mean "home directory" is a shell feature.
>
> Perl is not the shell, so that feature is not available.
>
>     my $outfile = '/home/zhao/myIP.txt';

Only if the $HOME is '/home/zhao'. 

    my $outfile = "$ENV{HOME}/myIP.txt";

is the equivalent to ~/myIP.txt.

(or use glob, as Ted suggested)

	hp


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

Date: Sat, 31 Jan 2009 08:53:47 -0800
From: "John W. Krahn" <someone@example.com>
Subject: Re: Want to write a script to do the batch conversion from domain name to IP.
Message-Id: <wm%gl.5232$eE2.754@newsfe08.iad>

Hongyi Zhao wrote:
> On Fri, 30 Jan 2009 05:17:00 -0800, "John W. Krahn"
> <someone@example.com> wrote:
> 
>> #!/usr/bin/perl
>> use warnings;
>> use strict;
>> use Socket;
>>
>> while ( <> ) {
>>     chomp;
>>     my ( $address, $port ) = split /:/ or next;
>>     my $number = inet_aton $address;
>>     my $ip     = inet_ntoa $number;
>>     print "$address:$port -> $ip:$port\n";
>>     }
>>
>> __END__
> 
> If I want to do the opposite thing, i.e., conversion from IP to domain
> name, what should I revise this script to do the trick.


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

while ( <> ) {
     chomp;
     my ( $address, $port ) = split /:/ or next;
     my $number = inet_aton $address;
     my $name    = gethostbyaddr $number, AF_INET;
     print "$address:$port -> $name:$port\n";
     }

__END__




John
-- 
Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov


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

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 2175
***************************************


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