[29352] in Perl-Users-Digest
Perl-Users Digest, Issue: 596 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jun 28 16:09:56 2007
Date: Thu, 28 Jun 2007 13:09:10 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 28 Jun 2007 Volume: 11 Number: 596
Today's topics:
Re: "Convert" perl command line to simple script <joe@inwap.com>
Re: FAQ 2.11 Perl Books <savagebeaste@yahoo.com>
Re: FAQ 2.11 Perl Books <mritty@gmail.com>
Re: FAQ 2.11 Perl Books <savagebeaste@yahoo.com>
Re: map woes <bik.mido@tiscalinet.it>
Re: map woes <joe@inwap.com>
new in CGI::Session::Driver::postgredsql nonexisting <ulvinge@gmail.com>
Re: new in CGI::Session::Driver::postgredsql nonexistin <gautam.chekuri@gmail.com>
Re: new in CGI::Session::Driver::postgredsql nonexistin <ulvinge@gmail.com>
Re: new in CGI::Session::Driver::postgredsql nonexistin <glex_no-spam@qwest-spam-no.invalid>
Re: new in CGI::Session::Driver::postgredsql nonexistin <ulvinge@gmail.com>
PERL and Active DIrectory gaz_5_m@yahoo.co.uk
Re: using wildcards with -e <bik.mido@tiscalinet.it>
Re: using wildcards with -e <savagebeaste@yahoo.com>
Re: using wildcards with -e <mritty@gmail.com>
Re: using wildcards with -e <savagebeaste@yahoo.com>
Re: using wildcards with -e <mritty@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 28 Jun 2007 11:55:30 -0700
From: Joe Smith <joe@inwap.com>
Subject: Re: "Convert" perl command line to simple script
Message-Id: <soadnWpuqp27mRnbnZ2dnUVZ_rfinZ2d@comcast.com>
Dr.Ruud wrote:
> bluez34me@gmail.com schreef:
>
>> perl -pi -e 's/..\index/index/g' *.html
>
> That "..\i" most l\ikely doesn't mean what you th\ink \it means\.
>
> (see perldoc -f quotemeta)
I'm inclined to believe the original poster made a typo. More sensible would be:
perl -pi -e 's/.\.index/index/g' *.html
-Joe
------------------------------
Date: Thu, 28 Jun 2007 11:12:53 -0700
From: "Clenna Lumina" <savagebeaste@yahoo.com>
Subject: Re: FAQ 2.11 Perl Books
Message-Id: <5eic1dF37emmkU1@mid.individual.net>
PerlFAQ Server wrote:
> Intermediate Perl (the "Alpaca Book")
I could swear I've always seen this referred to as the "llama" book? Or
are there two different books? (I've always known there to be a "camel"
and "llama" book as the top two Perl books.)
--
CL
------------------------------
Date: Thu, 28 Jun 2007 11:18:47 -0700
From: Paul Lalli <mritty@gmail.com>
Subject: Re: FAQ 2.11 Perl Books
Message-Id: <1183054727.620280.106330@k29g2000hsd.googlegroups.com>
On Jun 28, 2:12 pm, "Clenna Lumina" <savagebea...@yahoo.com> wrote:
> PerlFAQ Server wrote:
> > Intermediate Perl (the "Alpaca Book")
>
> I could swear I've always seen this referred to as the "llama"
> book? Or are there two different books? (I've always known
> there to be a "camel" and "llama" book as the top two
> Perl books.)
Two different books. The Llama is "Learning Perl" (currently in
fourth edition). The Alpaca is "Intermediate Perl". The Alpaca is a
direct sequel to the Llama, and picks up where the Llama left off.
The Camel is "Programming Perl", and is a reference, rather than a
tutorial like the Llama & Alpaca.
Paul Lalli
------------------------------
Date: Thu, 28 Jun 2007 11:51:26 -0700
From: "Clenna Lumina" <savagebeaste@yahoo.com>
Subject: Re: FAQ 2.11 Perl Books
Message-Id: <5eie9lF35fg48U1@mid.individual.net>
Paul Lalli wrote:
> On Jun 28, 2:12 pm, "Clenna Lumina" <savagebea...@yahoo.com> wrote:
>> PerlFAQ Server wrote:
>>> Intermediate Perl (the "Alpaca Book")
>>
>> I could swear I've always seen this referred to as the "llama"
>> book? Or are there two different books? (I've always known
>> there to be a "camel" and "llama" book as the top two
>> Perl books.)
>
> Two different books. The Llama is "Learning Perl" (currently in
> fourth edition). The Alpaca is "Intermediate Perl". The Alpaca is a
> direct sequel to the Llama, and picks up where the Llama left off.
>
> The Camel is "Programming Perl", and is a reference, rather than a
> tutorial like the Llama & Alpaca.
>
> Paul Lalli
Ah, thank you for clearing that up.
--
CL
------------------------------
Date: Thu, 28 Jun 2007 20:21:13 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: map woes
Message-Id: <juu783l8pqc5870d6oidc74364s03gjpip@4ax.com>
On Thu, 28 Jun 2007 10:23:16 -0700, "seven.reeds"
<seven.reeds@gmail.com> wrote:
> map {s/(\d+)\.news/$1/}
> grep(/\d+\.news/, readdir(NEWSDIR));
>
>The readdir and grep work as I expect. The result of the map is a
>list of "1"s, prolly the count of successful s// matches. Where am I
>messing this up?
In that this is not yet Perl 6 and you don't have a .subst method yet.
You have to work around instead.
map { (my $s=$_) =~ s/(\d+)\.news/$1/; $s }
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
------------------------------
Date: Thu, 28 Jun 2007 11:45:00 -0700
From: Joe Smith <joe@inwap.com>
Subject: Re: map woes
Message-Id: <x5-dnd8KqMotnBnbnZ2dnUVZ_uPinZ2d@comcast.com>
seven.reeds wrote:
> map {s/(\d+)\.news/$1/}
> The result of the map is a
> list of "1"s, prolly the count of successful s// matches.
Others have pointed out how to do it. I just want to point out the
comma operator, and how it could be used in this context.
map {s/(\d+)\.news/$1/, $_}
-Joe
------------------------------
Date: Thu, 28 Jun 2007 18:17:20 -0000
From: IDK <ulvinge@gmail.com>
Subject: new in CGI::Session::Driver::postgredsql nonexisting
Message-Id: <1183054640.581613.3680@o61g2000hsh.googlegroups.com>
I've been trying all day to make sessions in perl work.
This is the error message I get:
[Thu Jun 28 20:11:25 2007] login: (in cleanup) Can't locate
object method "new" via package "CGI::Session::Driver::postgredsql"
at /usr/share/perl5/CGI/Session.pm line 113 during global destruction.
>From the code:
*********************************************************
sub openDB
{
#open db
$dbh = DBI->connect("dbi:Pg:dbname=zerus;host=localhost;port=5432",
"*", "*");
if ( !defined $dbh ) {
die "Cannot connect to database!\n";
}
}
openDB;
$session = new CGI::Session("driver:postgredsql;id:md5", undef,
{Handle=>$dbh, $
$CGISESSID = $session->id();
print $session->header();
...
*********************************************************
I cannot find anything wrong with my code, and I've done exactly as
the docs say, so I suspect that there's something wrong with
Session.pm ...
If so, where should I turn to report this bug?
------------------------------
Date: Thu, 28 Jun 2007 18:38:39 -0000
From: goodgautam <gautam.chekuri@gmail.com>
Subject: Re: new in CGI::Session::Driver::postgredsql nonexisting
Message-Id: <1183055919.648052.126300@a26g2000pre.googlegroups.com>
On Jun 28, 11:17 pm, IDK <ulvi...@gmail.com> wrote:
> I've been trying all day to make sessions in perl work.
>
> This is the error message I get:
>
> [Thu Jun 28 20:11:25 2007] login: (in cleanup) Can't locate
> object method "new" via package "CGI::Session::Driver::postgredsql"
> at /usr/share/perl5/CGI/Session.pm line 113 during global destruction.
>
> >From the code:
>
> *********************************************************
> sub openDB
> {
> #open db
> $dbh = DBI->connect("dbi:Pg:dbname=zerus;host=localhost;port=5432",
> "*", "*");
>
> if ( !defined $dbh ) {
> die "Cannot connect to database!\n";
> }
>
> }
>
> openDB;
>
> $session = new CGI::Session("driver:postgredsql;id:md5", undef,
> {Handle=>$dbh, $
> $CGISESSID = $session->id();
> print $session->header();
> ...
> *********************************************************
>
> I cannot find anything wrong with my code, and I've done exactly as
> the docs say, so I suspect that there's something wrong with
> Session.pm ...
> If so, where should I turn to report this bug?
In your code the first param being passed to CGI::Session's
constructor reads : "driver:postgredsql;id:md5" ..
Is that a typo in the code too ( s~postgredsql~postgresql~; ) ? I
think it should be like driver:postgresql ...
http://search.cpan.org/dist/CGI-Session/lib/CGI/Session/Driver/postgresql.pm
------------------------------
Date: Thu, 28 Jun 2007 18:53:52 -0000
From: IDK <ulvinge@gmail.com>
Subject: Re: new in CGI::Session::Driver::postgredsql nonexisting
Message-Id: <1183056832.638992.183940@q75g2000hsh.googlegroups.com>
On Jun 28, 8:38 pm, goodgautam <gautam.chek...@gmail.com> wrote:
>
> In your code the first param being passed to CGI::Session's
> constructor reads : "driver:postgredsql;id:md5" ..
> Is that a typo in the code too ( s~postgredsql~postgresql~; ) ? I
> think it should be like driver:postgresql ...http://search.cpan.org/dist/CGI-Session/lib/CGI/Session/Driver/postgr...
Wow, that's it!
Thanks!
But now it gives me another error:
[Thu Jun 28 20:52:39 2007] login: (in cleanup) Can't connect to
data source '' because I can't work out what driver to use (it doesn't
seem to contain a 'dbi:driver:' prefix and the DBI_DRIVER env var is
not set) at /usr/share/perl5/CGI/Session/Driver/DBI.pm line 26
[Thu Jun 28 20:52:39 2007] login: (in cleanup) Can't call method
"commit" on unblessed reference at /usr/share/perl5/CGI/Session/Driver/
DBI.pm line 130 during global destruction.
------------------------------
Date: Thu, 28 Jun 2007 14:46:35 -0500
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: new in CGI::Session::Driver::postgredsql nonexisting
Message-Id: <4684101b$0$503$815e3792@news.qwest.net>
IDK wrote:
> On Jun 28, 8:38 pm, goodgautam <gautam.chek...@gmail.com> wrote:
>> In your code the first param being passed to CGI::Session's
>> constructor reads : "driver:postgredsql;id:md5" ..
>> Is that a typo in the code too ( s~postgredsql~postgresql~; ) ? I
>> think it should be like driver:postgresql ...http://search.cpan.org/dist/CGI-Session/lib/CGI/Session/Driver/postgr...
>
> Wow, that's it!
> Thanks!
>
>
> But now it gives me another error:
Now you should do some debugging before posting another error.
>
> [Thu Jun 28 20:52:39 2007] login: (in cleanup) Can't connect to
> data source '' because I can't work out what driver to use (it doesn't
> seem to contain a 'dbi:driver:' prefix and the DBI_DRIVER env var is
> not set) at /usr/share/perl5/CGI/Session/Driver/DBI.pm line 26
> [Thu Jun 28 20:52:39 2007] login: (in cleanup) Can't call method
> "commit" on unblessed reference at /usr/share/perl5/CGI/Session/Driver/
> DBI.pm line 130 during global destruction.
Read the docs for DBI's connect method, your data source isn't correct,
just as the error is telling you.
------------------------------
Date: Thu, 28 Jun 2007 20:04:53 -0000
From: IDK <ulvinge@gmail.com>
Subject: Re: new in CGI::Session::Driver::postgredsql nonexisting
Message-Id: <1183061093.669469.60320@q69g2000hsb.googlegroups.com>
On Jun 28, 9:46 pm, "J. Gleixner" <glex_no-s...@qwest-spam-no.invalid>
wrote:
> IDK wrote:
> > On Jun 28, 8:38 pm, goodgautam <gautam.chek...@gmail.com> wrote:
> >> In your code the first param being passed to CGI::Session's
> >> constructor reads : "driver:postgredsql;id:md5" ..
> >> Is that a typo in the code too ( s~postgredsql~postgresql~; ) ? I
> >> think it should be like driver:postgresql ...http://search.cpan.org/dist/CGI-Session/lib/CGI/Session/Driver/postgr...
>
> > Wow, that's it!
> > Thanks!
>
> > But now it gives me another error:
>
> Now you should do some debugging before posting another error.
>
>
>
> > [Thu Jun 28 20:52:39 2007] login: (in cleanup) Can't connect to
> > data source '' because I can't work out what driver to use (it doesn't
> > seem to contain a 'dbi:driver:' prefix and the DBI_DRIVER env var is
> > not set) at /usr/share/perl5/CGI/Session/Driver/DBI.pm line 26
> > [Thu Jun 28 20:52:39 2007] login: (in cleanup) Can't call method
> > "commit" on unblessed reference at /usr/share/perl5/CGI/Session/Driver/
> > DBI.pm line 130 during global destruction.
>
> Read the docs for DBI's connect method, your data source isn't correct,
> just as the error is telling you.
I've been debugging that exact error all day, but only recently got
some
progress and found that session.pm was buggy...
I can access other databases with the same DBI handle.
If I replace all the session code with:
$cgi = new CGI;
print header();
Everything works fine, but my goal is to use sessions.
------------------------------
Date: Thu, 28 Jun 2007 12:25:40 -0700
From: gaz_5_m@yahoo.co.uk
Subject: PERL and Active DIrectory
Message-Id: <1183058740.045092.304550@k79g2000hse.googlegroups.com>
Hi folks,
I was just wondering if anyone was using PERL to any great effect to
query Active DIrectory.
I'm in the middle of a fairly large project at work that requires
getting (constantly changing) info from AD. I was doing the same
things on a daily basis so thought it best to try and script it.
Is there any way to directly interrogate AD from within PERL. At the
moment, the only way I have been able to figure out is to use the
Windows Admin Pak to call:
open query, "dsquery group -name "x" | dsget group -members |";
from within the script and then use the piped output of the open
command to strip out the data that I want.
It works OK, just takes a hell of a long time!
The other issue I get is the default return size that is set in our AD
environment, 1000, so groups with any larger than 1000 users return
nothing.
I know you can get round this with VB, but PERL is my language of
choice! :-)
Anyone with any tips?
------------------------------
Date: Thu, 28 Jun 2007 20:17:26 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: using wildcards with -e
Message-Id: <lou783hfr2bjbb5c4ilk6ldbof21ums40h@4ax.com>
On Thu, 28 Jun 2007 10:12:29 -0700, Paul Lalli <mritty@gmail.com>
wrote:
>If you later change your "pattern" to actualy be a single file, your
>code will break. This is especially a problem if you're reading the
>pattern from the user. Observe:
Oh, yeah, and there' brace expansion too. Perhaps it was not that bad
to leave the -e in after all...
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
------------------------------
Date: Thu, 28 Jun 2007 11:49:29 -0700
From: "Clenna Lumina" <savagebeaste@yahoo.com>
Subject: Re: using wildcards with -e
Message-Id: <5eie60F38rphlU1@mid.individual.net>
Paul Lalli wrote:
> On Jun 27, 6:18 pm, "John W. Krahn" <d...@example.com> wrote:
>> Peter Makholm wrote:
>>> DMB <dumm...@hotmail.com> writes:
>>
>
>>>> if (-e $done_dir."*.DOC") {
>>>> print "found it\n";
>>>> } else {
>>>> print "nope\n";
>>>> }
>>
>>> You might want to look at the glob function which expands these
>>> kinds of wildcards into a list of files. So something like
>>
>>> if (grep { -e $_ } glob("$donedir/*.DOC") ) {
>>> print "Found it";
>>> }
>>
>> You shouldn't need the grep, this should work:
>>
>> if ( glob "$donedir/*.DOC" ) {
>> print "Found it";
>> }
>
> Danger, danger Will Robinson!! That bit of code will work only if
> both of the following are true:
> 1) The string actually does contain shell meta characters.
> 2) The block is not being used in a loop.
>
> If you later change your "pattern" to actualy be a single file, your
> code will break. This is especially a problem if you're reading the
> pattern from the user. Observe:
>
> ~/temp $ ls
> ~/temp $ perl -le'print for glob("foo.*")'
> ~/temp $ perl -le'print for glob("foo.bar")'
> foo.bar
> ~/temp $
>
> If the code is being called in a loop of some kind, and there are, say
> N files that match the pattern, your if statement will fail every (N
> +1)th time. Observe:
>
> ~/temp $ touch foo.1 foo.2 foo.3
> ~/temp $ perl -le'
> for (1..10) {
> if (glob("foo.*")) {
> print "yes"
> } else {
> print "no"
> }
> }
> '
> yes
> yes
> yes
> no
> yes
> yes
> yes
> no
> yes
> yes
I noticed that if you change:
if (glob("foo.*")) {
to [1]:
my @files = glob("foo.*");
if (@files) {
then you get:
$ ./test.pl
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
Changing the contents of the loop to [2]:
my @files = glob("foo.*");
print "[". join("]\n[", @files). "]\n\n";
prints the following 10 times:
[foo.1]
[foo.2]
[foo.3]
> All things considered, it's better to stick with the grep, and thereby
> call glob() in a scalar context, explicitly testing the existance of
> each return value.
>
> Paul Lalli
Grep works fine. But I want to know why Glob returns the way it does in
your original code?
In fact, if you change your original 'if' statement to:
if (()=glob("foo.*")) {
then 'yes' is printed every time as expected. Of course, it's being
forced into list context. It seems glob in scalar context is the
problem.
According to my Perl Pocket Reference (4th Ed.), pg 54:
glob expr ?
Returns a list of filenames that match the C-shell
pattern(s) in expr.
Perldoc says this:
$ perldoc -f glob
glob EXPR
glob Returns the value of EXPR with filename expansions
such as the standard Unix shell /bin/csh would do.
This is the internal function implementing the
"<*.c>" operator, but you can use it directly.
So if used in scalar context, shouldn't it be returning 3 (the list
count) in the example we're using?
It seems it shouldn't matter if it's being used in a loop or not, it
should be returning the same each time since what it's fetching is
unchanged?
Does this mean glob is broken in scalar context?
[1]
#!/usr/bin/perl
use strict;
for (1..10) {
my @d = glob("foo.*");
if (@d) {
#if (glob("foo.*")) {
print "yes"
}
else {
print "no"
}
print "\n";
}
__END__
[2]
#!/usr/bin/perl
use strict;
for (1..10) {
my @files = glob("foo.*");
print "[". join("]\n[", @files). "]\n\n";
}
__END__
--
CL
------------------------------
Date: Thu, 28 Jun 2007 11:59:07 -0700
From: Paul Lalli <mritty@gmail.com>
Subject: Re: using wildcards with -e
Message-Id: <1183057147.944469.203220@k29g2000hsd.googlegroups.com>
On Jun 28, 2:49 pm, "Clenna Lumina" <savagebea...@yahoo.com> wrote:
> Grep works fine. But I want to know why Glob returns the way it
> does in your original code?
Because glob() does something different depending on if its called in
scalar context or list context.
> In fact, if you change your original 'if' statement to:
>
> if (()=glob("foo.*")) {
>
> then 'yes' is printed every time as expected. Of course, it's being
> forced into list context. It seems glob in scalar context is the
> problem.
Yes.
> According to my Perl Pocket Reference (4th Ed.), pg 54:
>
> glob expr ?
> Returns a list of filenames that match the C-shell
> pattern(s) in expr.
Your pocket reference is wrong. It only returns a list in list
context.
> Perldoc says this:
>
> $ perldoc -f glob
> glob EXPR
> glob Returns the value of EXPR with filename expansions
> such as the standard Unix shell /bin/csh would do.
> This is the internal function implementing the
> "<*.c>" operator, but you can use it directly.
You either have an old and/or broken version of Perldoc, or you oddly
omitted a section. In my perldoc, there is another sentence between
those two:
In scalar
context, glob iterates through such filename
expansions, returning undef when the list is
exhausted.
> So if used in scalar context, shouldn't it be returning 3 (the list
> count) in the example we're using?
I don't know what makes you think that. Even if your version of the
doc was complete, it still doesn't say that. You may be falling into
the trap of believing that all list-returning functions return the
size of that list in scalar context. That's not true. Arrays
evaluated in scalar context return their size. That's it. You cannot
generalize "array" to "any expression returning a list." Any function
returning a list of values in list context is free to do anything else
in scalar context.
> Does this mean glob is broken in scalar context?
No. glob() behaves exactly as it's documented to behave in scalar
context. If your version of perldoc really is missing that critical
section, I suggest you check out the most recent version:
http://perldoc.perl.org/functions/glob.html
Paul Lalli
------------------------------
Date: Thu, 28 Jun 2007 12:12:07 -0700
From: "Clenna Lumina" <savagebeaste@yahoo.com>
Subject: Re: using wildcards with -e
Message-Id: <5eifgeF38kfn4U1@mid.individual.net>
Paul Lalli wrote:
> On Jun 28, 2:49 pm, "Clenna Lumina" <savagebea...@yahoo.com> wrote:
>
>> Grep works fine. But I want to know why Glob returns the way it
>> does in your original code?
>
> Because glob() does something different depending on if its called in
> scalar context or list context.
Would it not of been more logical to have it return the count of the
files it found, though?
>> In fact, if you change your original 'if' statement to:
>>
>> if (()=glob("foo.*")) {
>>
>> then 'yes' is printed every time as expected. Of course, it's being
>> forced into list context. It seems glob in scalar context is the
>> problem.
>
> Yes.
>
>> According to my Perl Pocket Reference (4th Ed.), pg 54:
>>
>> glob expr ?
>> Returns a list of filenames that match the C-shell
>> pattern(s) in expr.
>
> Your pocket reference is wrong. It only returns a list in list
> context.
That would seem to be the case.
>> Perldoc says this:
>>
>> $ perldoc -f glob
>> glob EXPR
>> glob Returns the value of EXPR with filename expansions
>> such as the standard Unix shell /bin/csh would do.
>> This is the internal function implementing the
>> "<*.c>" operator, but you can use it directly.
>
> You either have an old and/or broken version of Perldoc, or you oddly
> omitted a section. In my perldoc, there is another sentence between
> those two:
Hmm, I'm using Perl 5.6.1 on RH Linux and it does seem to lack that
section:
perldoc -f glob
glob EXPR
glob Returns the value of EXPR with filename expansions
such as the standard Unix shell /bin/csh would do.
This is the internal function implementing the
"<*.c>" operator, but you can use it directly. If
EXPR is omitted, "$_" is used. The "<*.c>" opera-
tor is discussed in more detail in the I/O Opera-
tors entry in the perlop manpage.
Beginning with v5.6.0, this operator is imple-
mented using the standard "File::Glob" extension.
See the File::Glob manpage for details.
(END)
> In scalar
> context, glob iterates through such filename
> expansions, returning undef when the list is
> exhausted.
Well that certainly explains it. FWIW, my pocket book (ISBN
0-596-00374-9) claims it covers Perl 5.8, so it's a little odd that they
left this rather important note out. Same goes for 5.6.1's Perldoc.
>> So if used in scalar context, shouldn't it be returning 3 (the list
>> count) in the example we're using?
>
> I don't know what makes you think that. Even if your version of the
> doc was complete, it still doesn't say that. You may be falling into
> the trap of believing that all list-returning functions return the
> size of that list in scalar context. That's not true. Arrays
> evaluated in scalar context return their size. That's it. You cannot
> generalize "array" to "any expression returning a list." Any function
> returning a list of values in list context is free to do anything else
> in scalar context.
Hmm, thanks for pointing that out. Yes it is easy to get into that way
of thinking, when many functions seem to return the count (ie, map,
grep) when used in scalar context.
>> Does this mean glob is broken in scalar context?
>
> No. glob() behaves exactly as it's documented to behave in scalar
> context.
Question retracted.
> If your version of perldoc really is missing that critical
> section, I suggest you check out the most recent version:
> http://perldoc.perl.org/functions/glob.html
Thank you, I have saved this.
--
CL
------------------------------
Date: Thu, 28 Jun 2007 12:45:00 -0700
From: Paul Lalli <mritty@gmail.com>
Subject: Re: using wildcards with -e
Message-Id: <1183059900.171419.291110@q75g2000hsh.googlegroups.com>
On Jun 28, 3:12 pm, "Clenna Lumina" <savagebea...@yahoo.com> wrote:
> Paul Lalli wrote:
> > On Jun 28, 2:49 pm, "Clenna Lumina" <savagebea...@yahoo.com>
> > wrote:
>
> >> Grep works fine. But I want to know why Glob returns the way
> >> it does in your original code?
>
> > Because glob() does something different depending on if its
> > called in scalar context or list context.
>
> Would it not of been more logical to have it return the count
> of the files it found, though?
Not really. Think of glob() as a means of doing readdir() via shell
expansion rather than opening the directory in Perl. That way, you
can do:
while (my $file = glob("*.pl")) {
#...
}
rather than:
open my $dh, "." or die $!;
while (my $file = readdir($dh)) {
next unless $file =~ /\.pl$/;
#...
}
If you want the count of the files, either keep track as you're
reading them in, or read them into an array first and then get the
count of that:
my @files = glob("*.pl");
my $count = @files;
(or, as you found, bastardize the empty-list-assignment feature:
my $count = () = glob("*.pl");
)
You'll note the context-based return value of glob() also matches the
context-based return value of readdir(). All files in list context,
"next" file in scalar context.
> > You either have an old and/or broken version of Perldoc, or
> > you oddly omitted a section. In my perldoc, there is another
> > sentence between those two:
>
> Hmm, I'm using Perl 5.6.1 on RH Linux and it does seem to lack that
> section:
I'd call that a documentation bug in 5.6.1. I verified the same thing
with a 5.6.1 version on solaris. The function works the same as it
does on 5.8, but the docs lack that rather important note.
> > Arrays evaluated in scalar context return their size. That's
> > it. You cannot generalize "array" to "any expression returning
> > a list." Any function returning a list of values in list
> > context is free to do anything else in scalar context.
>
> Hmm, thanks for pointing that out. Yes it is easy to get into
> that way of thinking, when many functions seem to return the
> count (ie, map, grep) when used in scalar context.
Oh, I agree. That's why I called it a trap. It gets easier to
remember once you start writing your own subroutines that do something
different depending on context, using the wantarray() function.
> > If your version of perldoc really is missing that critical
> > section, I suggest you check out the most recent version:
> >http://perldoc.perl.org/functions/glob.html
>
> Thank you, I have saved this.
You're welcome. Note that I generally recommend you use the version
of the documentation that matches your version of Perl, but in this
case, it's unfortunate that the docs themselves contain a bug..
Paul Lalli
------------------------------
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 596
**************************************