[31542] in Perl-Users-Digest
Perl-Users Digest, Issue: 2801 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Feb 2 18:09:25 2010
Date: Tue, 2 Feb 2010 15:09:06 -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 Tue, 2 Feb 2010 Volume: 11 Number: 2801
Today's topics:
function within qq{} <catebekensail@yahoo.com>
Re: function within qq{} <someone@example.com>
Re: function within qq{} <catebekensail@yahoo.com>
Re: function within qq{} <uri@StemSystems.com>
Re: function within qq{} <john@castleamber.com>
Re: function within qq{} <uri@StemSystems.com>
Re: function within qq{} <ben@morrow.me.uk>
Re: function within qq{} <uri@StemSystems.com>
Re: function within qq{} <ben@morrow.me.uk>
Re: function within qq{} <uri@StemSystems.com>
Re: SSL read timeout <spamtotrash@toomuchfiction.com>
Want your opinion on @ARGV file globbing <jl_post@hotmail.com>
Re: Want your opinion on @ARGV file globbing <jimsgibson@gmail.com>
Re: Want your opinion on @ARGV file globbing <jurgenex@hotmail.com>
Re: Want your opinion on @ARGV file globbing <kst-u@mib.org>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 2 Feb 2010 08:01:14 -0800 (PST)
From: cate <catebekensail@yahoo.com>
Subject: function within qq{}
Message-Id: <983f6b92-cc9a-4312-aae4-c15890c7ce1b@3g2000yqn.googlegroups.com>
I know you can do this, I just can't find it.
How do you call a sub within a qq{} construct
qq{The common term for H2O is chem("H2O").}
Thank you
(As soon is this post is complete... I'll find it) :-)
------------------------------
Date: Tue, 02 Feb 2010 08:18:07 -0800
From: "John W. Krahn" <someone@example.com>
Subject: Re: function within qq{}
Message-Id: <4fY9n.29809$_96.14271@newsfe02.iad>
cate wrote:
> I know you can do this, I just can't find it.
> How do you call a sub within a qq{} construct
>
> qq{The common term for H2O is chem("H2O").}
perldoc -q "How do I expand function calls in a string"
John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway
------------------------------
Date: Tue, 2 Feb 2010 08:31:47 -0800 (PST)
From: cate <catebekensail@yahoo.com>
Subject: Re: function within qq{}
Message-Id: <26573ac1-4868-4310-a5b5-bf70939388ca@v25g2000yqk.googlegroups.com>
On Feb 2, 10:18=A0am, "John W. Krahn" <some...@example.com> wrote:
> cate wrote:
> > I know you can do this, I just can't find it.
> > How do you call a sub within a qq{} construct
>
> > qq{The common term for H2O is chem("H2O").}
>
> perldoc -q "How do I expand function calls in a string"
>
> John
> --
> The programmer is fighting against the two most
> destructive forces in the universe: entropy and
> human stupidity. =A0 =A0 =A0 =A0 =A0 =A0 =A0 -- Damian Conway
@{[mysub(1,2,3)]}
good grief. Thanks
------------------------------
Date: Tue, 02 Feb 2010 11:41:14 -0500
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: function within qq{}
Message-Id: <87k4uvbnf9.fsf@quad.sysarch.com>
>>>>> "c" == cate <catebekensail@yahoo.com> writes:
c> On Feb 2, 10:18 am, "John W. Krahn" <some...@example.com> wrote:
>> cate wrote:
>> > I know you can do this, I just can't find it.
>> > How do you call a sub within a qq{} construct
>>
>> > qq{The common term for H2O is chem("H2O").}
>>
>> perldoc -q "How do I expand function calls in a string"
>>
c> @{[mysub(1,2,3)]}
c> good grief. Thanks
and that is considered a poor construct. i never use it (or its scalar
cousin) nor do i recommend it. when i review code, i downgrade when i
see that used. it is simpler and better to just assign to a variable
before the string and then interpolate it. also it will likely be faster
as you don't need the reference/dereference. also it will only call your
code in a list context (even the scalar form!) which may be a
problem. assigning to your own variable allows you to control the
context as well.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Tue, 02 Feb 2010 10:50:25 -0600
From: John Bokma <john@castleamber.com>
Subject: Re: function within qq{}
Message-Id: <87vdef4m5q.fsf@castleamber.com>
cate <catebekensail@yahoo.com> writes:
> I know you can do this, I just can't find it.
> How do you call a sub within a qq{} construct
>
> qq{The common term for H2O is chem("H2O").}
>
> Thank you
>
> (As soon is this post is complete... I'll find it) :-)
perl -e '
sub chem {
my $term = shift;
return "*$term*";
}
print qq{The common term for H2O is ${\( chem("H2O") ) }};
'
use @{...} for list expressions, ${...} for scalar.
See perldoc perlref section Using References, item 4.
--
John Bokma j3b
Hacking & Hiking in Mexico - http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development
------------------------------
Date: Tue, 02 Feb 2010 12:02:16 -0500
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: function within qq{}
Message-Id: <871vh3bmg7.fsf@quad.sysarch.com>
>>>>> "JB" == John Bokma <john@castleamber.com> writes:
JB> cate <catebekensail@yahoo.com> writes:
>> I know you can do this, I just can't find it.
>> How do you call a sub within a qq{} construct
>>
>> qq{The common term for H2O is chem("H2O").}
>>
>> Thank you
>>
>> (As soon is this post is complete... I'll find it) :-)
JB> perl -e '
JB> sub chem {
JB> my $term = shift;
JB> return "*$term*";
JB> }
JB> print qq{The common term for H2O is ${\( chem("H2O") ) }};
JB> '
JB> use @{...} for list expressions, ${...} for scalar.
partly incorrect.
perl -le 'sub context {return "array" if wantarray} ; print "scalar ${\context()}"'
scalar array
that is an obscure bug that has been around a long time. the scalar form
still calls the sub in list context. this is one of the reasons i don't
recommend using this trick. and a trick it is. it isn't really meant to
be used like that but perl's syntax supports interpolating of
dereferenced expressions. the interpolate module supports this in a
better way and so does perl6.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Tue, 2 Feb 2010 17:50:14 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: function within qq{}
Message-Id: <m75m37-9c.ln1@osiris.mauzo.dyndns.org>
Quoth "Uri Guttman" <uri@StemSystems.com>:
> >>>>> "JB" == John Bokma <john@castleamber.com> writes:
>
> JB> cate <catebekensail@yahoo.com> writes:
> >> I know you can do this, I just can't find it.
> >> How do you call a sub within a qq{} construct
> >>
> >> qq{The common term for H2O is chem("H2O").}
> >>
> >> Thank you
> >>
> >> (As soon is this post is complete... I'll find it) :-)
>
> JB> perl -e '
> JB> sub chem {
> JB> my $term = shift;
> JB> return "*$term*";
> JB> }
> JB> print qq{The common term for H2O is ${\( chem("H2O") ) }};
> JB> '
>
> JB> use @{...} for list expressions, ${...} for scalar.
>
> partly incorrect.
>
> perl -le 'sub context {return "array" if wantarray} ; print "scalar
> ${\context()}"'
> scalar array
>
> that is an obscure bug that has been around a long time. the scalar form
> still calls the sub in list context.
It's not a bug. \($x, $y, $z) is perfectly valid, and returns a list of
refs. (This also means that \(@a) is not the same as \@a: one of the few
cases where parens *are* necessary to denote a list.)
Ben
------------------------------
Date: Tue, 02 Feb 2010 13:06:45 -0500
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: function within qq{}
Message-Id: <87pr4n8qbu.fsf@quad.sysarch.com>
>>>>> "BM" == Ben Morrow <ben@morrow.me.uk> writes:
BM> Quoth "Uri Guttman" <uri@StemSystems.com>:
>> >>>>> "JB" == John Bokma <john@castleamber.com> writes:
>>
JB> cate <catebekensail@yahoo.com> writes:
>> >> I know you can do this, I just can't find it.
>> >> How do you call a sub within a qq{} construct
>> >>
>> >> qq{The common term for H2O is chem("H2O").}
>> >>
>> >> Thank you
>> >>
>> >> (As soon is this post is complete... I'll find it) :-)
>>
JB> perl -e '
JB> sub chem {
JB> my $term = shift;
JB> return "*$term*";
JB> }
JB> print qq{The common term for H2O is ${\( chem("H2O") ) }};
JB> '
>>
JB> use @{...} for list expressions, ${...} for scalar.
>>
>> partly incorrect.
>>
>> perl -le 'sub context {return "array" if wantarray} ; print "scalar
>> ${\context()}"'
>> scalar array
>>
>> that is an obscure bug that has been around a long time. the scalar form
>> still calls the sub in list context.
BM> It's not a bug. \($x, $y, $z) is perfectly valid, and returns a list of
BM> refs. (This also means that \(@a) is not the same as \@a: one of the few
BM> cases where parens *are* necessary to denote a list.)
yes, but the "${\foo()}" idiom looks like it will be in scalar
context. that is the issue. you need to put a scalar() in there. whether
it is a true bug or not, i say this is one reason to not use this
trick. using a variable is simpler, probably faster and safer.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Tue, 2 Feb 2010 21:59:14 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: function within qq{}
Message-Id: <iqjm37-rr1.ln1@osiris.mauzo.dyndns.org>
Quoth "Uri Guttman" <uri@StemSystems.com>:
> >>>>> "BM" == Ben Morrow <ben@morrow.me.uk> writes:
>
> BM> It's not a bug. \($x, $y, $z) is perfectly valid, and returns a list of
> BM> refs. (This also means that \(@a) is not the same as \@a: one of the few
> BM> cases where parens *are* necessary to denote a list.)
>
> yes, but the "${\foo()}" idiom looks like it will be in scalar
> context. that is the issue. you need to put a scalar() in there. whether
> it is a true bug or not, i say this is one reason to not use this
> trick. using a variable is simpler, probably faster and safer.
Oh, I agree there. The right answer is of course some sort of template
system: sprintf will do for starters, and there are plenty on CPAN.
Ben
------------------------------
Date: Tue, 02 Feb 2010 17:19:59 -0500
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: function within qq{}
Message-Id: <87bpg746wg.fsf@quad.sysarch.com>
>>>>> "BM" == Ben Morrow <ben@morrow.me.uk> writes:
BM> Quoth "Uri Guttman" <uri@StemSystems.com>:
>> >>>>> "BM" == Ben Morrow <ben@morrow.me.uk> writes:
>>
BM> It's not a bug. \($x, $y, $z) is perfectly valid, and returns a list of
BM> refs. (This also means that \(@a) is not the same as \@a: one of the few
BM> cases where parens *are* necessary to denote a list.)
>>
>> yes, but the "${\foo()}" idiom looks like it will be in scalar
>> context. that is the issue. you need to put a scalar() in there. whether
>> it is a true bug or not, i say this is one reason to not use this
>> trick. using a variable is simpler, probably faster and safer.
BM> Oh, I agree there. The right answer is of course some sort of template
BM> system: sprintf will do for starters, and there are plenty on CPAN.
and what is wrong with my suggestio of a simple variable assignment
before the string is built? that is all the OP needs.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Tue, 2 Feb 2010 17:09:33 +0000 (UTC)
From: Kevin Collins <spamtotrash@toomuchfiction.com>
Subject: Re: SSL read timeout
Message-Id: <slrnhmgn2e.c82.spamtotrash@vai.unix-guy.com>
On 2010-02-02, C.DeRykus <derykus@gmail.com> wrote:
> On Feb 2, 2:26 am, Salvador Fandino <sfand...@yahoo.com> wrote:
>> C.DeRykus wrote:
>> > On Feb 1, 1:47 pm, Kevin Collins <spamtotr...@toomuchfiction.com>
>> > wrote:
>> >> Hi,
>>
>> >> I have a perl script running to query a SOAP process, but using a basic
>> >> Net::HTTPS connection. If the server takes more than 60 econds to respond, the
>> >> script fails with the following error:
>>
>> >> SSL read timeout: at /opt/CTperl-5.8.4/lib/site_perl/5.8.4/Net/HTTP/Methods.pm
>> >> line 226 Net::SSL::die_with_error('Net::HTTPS=GLOB(0x60000000000608a0)', 'SSL
>> >> read timeout') called at
>> >> /opt/CTperl-5.8.4/lib/site_perl/5.8.4/IA64.ARCHREV_0-LP64/Net/SSL.pm line 218
>> >> ...
>> >> ...
>>
>> >> My invocation looks like this:
>>
>> >> my $connection = Net::HTTPS->new(Host => $Server, timeout => 300) || die $@;
>>
>> try using "Timeout" instead!
>>
>
> You're right. Net::HTTP/Net::HTTPS inherit from IO::Socket::INET
> which specifies only an uppercase "Timeout" option.
>
> perl -MNet::HTTPS -wle "$c=Net::HTTPS->new(Timeout=>10);
> print $c->timeout"
> 10
Thanks, guys! I had found Timeout, but tried to invoke it as:
$connection->Timeout(300);
but that did not work. I never thought to pass it as an option! That did the
trick...
Thanks,
Kevin
------------------------------
Date: Tue, 2 Feb 2010 08:56:40 -0800 (PST)
From: "jl_post@hotmail.com" <jl_post@hotmail.com>
Subject: Want your opinion on @ARGV file globbing
Message-Id: <0e80dcfe-9b21-405b-aaf5-5ef1deba956b@x10g2000prk.googlegroups.com>
Hi,
Recently someone asked me to write a Perl script that would operate
on a bunch of input files specified at the command line. This script
was meant for a Unix-ish system, but I developed it on an MSWin
system.
Normally, when I write a Perl script that takes an arbitrary number
of input files, I will include the line:
@ARGV = map glob, @ARGV; # for DOS globbing
I do this because the DOS shell passes in wildcard designators
unexpanded -- that is, if "*.txt" is specified as the only argument,
@ARGV will have "*.txt" as its only element. Unix shells, on the
other hand, usually expand the wildcards, so the glob()bing doesn't
have to be done.
So by using the above line I ensure that the script will have the
same wildcard-expanding behavior whether it is run in DOS or in Unix.
However, if the above line is called when run under Unix, then
technically the wildcard expansions get run twice: Once at the
command line, and once in my script. This will be a problem if any of
the input files have wildcard characters or spaces in them. For
example, if I have a file named "a b", and I call my Perl script with
"perl script.pl a*", then "a*" expands to include "a b", but then the
glob() call in the script expands that to "a" and "b", ignoring file
"a b" altogether.
So to work around that problem, I wrote my script so that it only
did file globbing if it was running on a Windows platform, like this:
if ($^O =~ m/^MSWin/i)
{
@ARGV = map glob, @ARGV; # for DOS globbing
}
This way, input arguments won't get "double-globbed."
Happy with this, I sent my script to the person who needed it. He
responded by saying that "the argument list [was] too long." It turns
out that the wildcard expression he was using expanded out to nearly
16,000 files, which caused the Unix shell he was using to refuse to
run the resulting (long) command line.
So I made a quick change to my script: I removed the above if-
check and advised him to pass in the wildcarded arguments surrounded
by quotes. That way the shell wouldn't expand out the wildcards,
leaving Perl to do it.
That "work-around" worked great. But that led me to ask: In
future scripts, should I include the check for $^O before calling glob
()? If I don't, then the files risk being "double-globbed" on Unix
systems -- but if I do, then I run the risk of the shell refusing to
call the script (without an available work-around).
Of course, this is often a moot point, as more than 99% of the
input files I ever processed have no wildcard characters or spaces in
their filenames. But that's a guarantee I can't always make.
Perhaps I could still call glob() by default on all systems, but
include a command-line switch that forces that not to happen (in order
to prevent double-globbing). That way, the switch could be mostly
ignored, but it is there in case it's ever needed.
Or am I just overthinking this? After all, glob()bing @ARGV in all
instances (that is, regardless of platform) has never given me a
problem (yet). Maybe I should just leave it in (to be called all the
time) after all.
What are your opinions on this? Is there a convention you use that
addresses this issue? In there an alternate way you prefer to handle
it?
Your thoughts and opinions are welcome.
Thanks,
-- Jean-Luc
------------------------------
Date: Tue, 02 Feb 2010 09:21:24 -0800
From: Jim Gibson <jimsgibson@gmail.com>
Subject: Re: Want your opinion on @ARGV file globbing
Message-Id: <020220100921245748%jimsgibson@gmail.com>
In article
<0e80dcfe-9b21-405b-aaf5-5ef1deba956b@x10g2000prk.googlegroups.com>,
<"jl_post@hotmail.com"> wrote:
[problems using glob on Unix and Windows systems snipped]
Your problem is one of the reasons I never use glob to find files to
process. I always use File::Find and specify the top-level directory,
either as a default or a command-line parameter. Then, I can apply any
appropriate filters to the actual file name and directory.
--
Jim Gibson
------------------------------
Date: Tue, 02 Feb 2010 09:27:29 -0800
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Want your opinion on @ARGV file globbing
Message-Id: <sgngm591ob7tggtv44g8kffj2vj8t3a8kb@4ax.com>
"jl_post@hotmail.com" <jl_post@hotmail.com> wrote:
[...]
> Happy with this, I sent my script to the person who needed it. He
>responded by saying that "the argument list [was] too long." It turns
>out that the wildcard expression he was using expanded out to nearly
>16,000 files, which caused the Unix shell he was using to refuse to
>run the resulting (long) command line.
[...]
> Or am I just overthinking this? [...]
Yes, you are. There is nothing wrong with the original version of your
script and he has a problem with his shell, not with your Perl program.
The correct solution is the same as for any program on UNIX when the
shell complains about a too long arg list: use the find utility with the
execute option.
Actually your "fix" made it worse because your forced globbing in your
Perl program blocks the user from naming files with a star or a tilde in
their filenames.
If you really want to offer this changed behaviour then I would do it at
most as an additional option, controlled by a command line parameter.
Otherwise your script behaves different than any other Unix program and
this inconsistency will bite you sooner or later.
jue
------------------------------
Date: Tue, 02 Feb 2010 12:27:33 -0800
From: Keith Thompson <kst-u@mib.org>
Subject: Re: Want your opinion on @ARGV file globbing
Message-Id: <ln8wbb5qoa.fsf@nuthaus.mib.org>
Jürgen Exner <jurgenex@hotmail.com> writes:
> "jl_post@hotmail.com" <jl_post@hotmail.com> wrote:
> [...]
>> Happy with this, I sent my script to the person who needed it. He
>>responded by saying that "the argument list [was] too long." It turns
>>out that the wildcard expression he was using expanded out to nearly
>>16,000 files, which caused the Unix shell he was using to refuse to
>>run the resulting (long) command line.
> [...]
>> Or am I just overthinking this? [...]
>
> Yes, you are. There is nothing wrong with the original version of your
> script and he has a problem with his shell, not with your Perl program.
> The correct solution is the same as for any program on UNIX when the
> shell complains about a too long arg list: use the find utility with the
> execute option.
>
> Actually your "fix" made it worse because your forced globbing in your
> Perl program blocks the user from naming files with a star or a tilde in
> their filenames.
> If you really want to offer this changed behaviour then I would do it at
> most as an additional option, controlled by a command line parameter.
> Otherwise your script behaves different than any other Unix program and
> this inconsistency will bite you sooner or later.
Agreed. You should *not* do your own globbing by default on Unix.
If I type
prog 'foo bar'
and it processes the two files "foo" and "bar", that's extremely
counterintiutive behavior; it's also difficult to work around it
if I really want to process a file called "foo bar".
An option tell your program to do its own globbing wouldn't be
unreasonable, but personally I wouldn't use it; the right Unixish
solution is to use "find", "xargs", or something similar.
Or you could add an option to specify a file containing a list
of files. If you're processing 16,000 files, generating a list
of them isn't much of a burden.
I'm not sure what the default behavior should be on Windows.
Consistency between the Unix and Windows versions argues for not
doing your own globbing by default. Consistency between your program
and other Windows programs might argue for enabling it by default.
One thing you should look into: what does "glob" do with whitespace?
Many Windows file names contain spaces; you don't want to make it
difficult to process such files.
--
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: 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:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#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 2801
***************************************