Count of items is EXCEPTION not RULE (was Re: Little perl annoyance #371: glob) 
Author Message
 Count of items is EXCEPTION not RULE (was Re: Little perl annoyance #371: glob)

kj0> I was hoping to get the number of files having a particular format by
kj0> using:

kj0>   my $n = glob("${prefix}*");

kj0> but NOOO!  Instead, in this case Perl puts the first element of the
kj0> array in $n, instead of the size of the array.

Why would you have thought this?  There are *more* operators that do
*not* return the "size" in a scalar context than do!  So "size" is the
exception, not the rule.


grep(), split() (with odd side effects) and I forget the other one or
two.  But many others don't, such as glob or getpwnam or comma or
literal slice or regexp match or filehandle read or.....

Remember - returning a size-count is the *exception*, not the rule!

print "Just another Perl hacker,"

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!



Thu, 27 Feb 2003 09:38:42 GMT  
 Count of items is EXCEPTION not RULE (was Re: Little perl annoyance #371: glob)

Quote:

>> Why would you have thought this?

kj0> My misguided Perl "intuition", I guess, which went something like
kj0> this: glob returns a list, a list is (sort of like?) an array,
kj0> evaluating an array in a scalar context gives the size of the array,
kj0> etc.  This might be ignorant but not entirely unreasonable.

It's that leap that seems to be the norm in most of the Perl docs out
there.  To blur a list with an array name is damaging, as you
discovered.  We're very careful in the classes we teach to distinguish
those.  Llama 3 will go even further to break them apart, as we've
found a whole new way to prevent people from ever thinking about

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!



Thu, 27 Feb 2003 10:56:12 GMT  
 Count of items is EXCEPTION not RULE (was Re: Little perl annoyance #371: glob)


Wed, 18 Jun 1902 08:00:00 GMT  
 Count of items is EXCEPTION not RULE (was Re: Little perl annoyance #371: glob)
On Sep 9, Randal L. Schwartz said:

Quote:
>Llama 3 will go even further to break them apart, as we've found a whole


Oh no... what new way is this?

--

PerlMonth - An Online Perl Magazine            http://www.perlmonth.com/
The Perl Archive - Articles, Forums, etc.    http://www.perlarchive.com/
CPAN - #1 Perl Resource  (my id:  PINYAN)        http://search.cpan.org/



Thu, 27 Feb 2003 11:24:50 GMT  
 Count of items is EXCEPTION not RULE (was Re: Little perl annoyance #371: glob)

Quote:
> On Sep 9, Randal L. Schwartz said:

> >Llama 3 will go even further to break them apart, as we've found a whole

> Oh no... what new way is this?

just wait and see ;)

unless Randal and rootbeer have come up with a new new way, we
introduce arrays in terms of single element access before we talk
about operations on the array as a whole. beat all of that into
the students heads before you show them a slice.

--
brian d foy                    
CGI Meta FAQ <URL:http://www.smithrenaud.com/public/CGI_MetaFAQ.html>
Perl Mongers <URL:http://www.perl.org/>



Thu, 27 Feb 2003 12:22:20 GMT  
 Count of items is EXCEPTION not RULE (was Re: Little perl annoyance #371: glob)
oooo, a Llama 3, cool.

Hrm, it's bad I already know Perl or I'd buy it.



Thu, 27 Feb 2003 13:41:43 GMT  
 Count of items is EXCEPTION not RULE (was Re: Little perl annoyance #371: glob)


Wed, 18 Jun 1902 08:00:00 GMT  
 Count of items is EXCEPTION not RULE (was Re: Little perl annoyance #371: glob)
hi,

Quote:


>>kj0> I was hoping to get the number of files having a particular format by
>>kj0> using:
>>kj0>   my $n = glob("${prefix}*");
>>kj0> but NOOO!  Instead, in this case Perl puts the first element of the
>>kj0> array in $n, instead of the size of the array.
>>Why would you have thought this?
> My misguided Perl "intuition", I guess, which went something like
> this: glob returns a list, a list is (sort of like?) an array,
> evaluating an array in a scalar context gives the size of the array,
> etc.  This might be ignorant but not entirely unreasonable.

if there is an array returned from a function, then
$n = func(); will give you always the size. but
in a function you can use wantarray() for checking,
if the sub was called in void, scalar or array context.
perldoc -f wantarray

think about
$line = <STDIN>;

glob() works like this.

tina

--
http://tinita.de    \  enter__| |__the___ _ _ ___
tina's moviedatabase \     / _` / _ \/ _ \ '_(_-< of
search & add comments \    \__,_\___/\___/_| /__/ perception
please don't email unless offtopic or followup is set. thanx



Fri, 28 Feb 2003 02:54:50 GMT  
 Count of items is EXCEPTION not RULE (was Re: Little perl annoyance #371: glob)

Tina> if there is an array returned from a function,

See, it's sloppy talk like that that gets us all in trouble.
You cannot return an array from a function.  The action of:




only a list or a scalar.

Maybe this is why the newbies get confused.  We have sloppy talk, and
then that gets quoted as real, and then the bad book authors start
using that, and then the newbies get very confused.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!



Fri, 28 Feb 2003 03:07:30 GMT  
 Count of items is EXCEPTION not RULE (was Re: Little perl annoyance #371: glob)
: I haven't yet seen a good (i.e. sufficiently memorable) account of the
: difference between arrays and lists in Perl.

A list is a temporary sequence of values.  Functions returns lists.
Functions are passed lists.  You can't change the length of a list.
Lists are very very transient things.

An array is a particular data structure in memory.  You can change the

Most of the time in Perl we use arrays because they act as containers
for lists.  We can store a list of values in an array, then fetch the
list of values when we call a function:



The trick is to remember that most things in Perl that act on multiple
values expect lists, not arrays.  The only exceptions are the functions
specifically for arrays: push, pop, shift, unshift, splice.  (There
might be more but they don't come to mind right now, he says hedging
his bets).

A rough analogy for C people is that lists are temporary things
allocated on the stack around function calls, while arrays are
malloc()ed things in the heap.

Cheers;

Nat



Fri, 28 Feb 2003 04:32:31 GMT  
 Count of items is EXCEPTION not RULE (was Re: Little perl annoyance #371: glob)

Quote:
> if there is an array returned from a function, then
> $n = func();
>will give you always the size. but

that is not true.  that is not even being close to true.  indeed,
previous posts and the documentation say otherwise.

Quote:
> in a function you can use wantarray() for checking,

certainly.  and, because that function exists, i can write

   sub foo
      {
      if( wantarray )
         {
         return ('This value', 'That value', 'Most used value')
         }
      else            
         {
         return 'Most used value'
         }
      }

which clearly contradicts what you claimed.

--
brian d foy                    
CGI Meta FAQ <URL:http://www.smithrenaud.com/public/CGI_MetaFAQ.html>
Perl Mongers <URL:http://www.perl.org/>



Fri, 28 Feb 2003 05:02:34 GMT  
 Count of items is EXCEPTION not RULE (was Re: Little perl annoyance #371: glob)
hi,

Quote:

>> if there is an array returned from a function, then
>> $n = func();
>>will give you always the size. but
> that is not true.  that is not even being close to true.  indeed,
> previous posts and the documentation say otherwise.
>> in a function you can use wantarray() for checking,
> certainly.  and, because that function exists, i can write
>    sub foo
>       {
>       if( wantarray )
>          {
>          return ('This value', 'That value', 'Most used value')
>          }
>       else            
>          {
>          return 'Most used value'
>          }
>       }
> which clearly contradicts what you claimed.

hm, maybe i didn't make this clear enough. i said:

Quote:
>> if there is an array returned from a function, then
>> $n = func();
>>will give you always the size. but

so that meant *if* the function returns *always* an array (or better,
as randal said, a list), *then*
$result = func();
will *always* give you the size.
your function does not always return a list, so there
is no contradiction.

or more logical, I said:
(sub returns always list) =>
             (call in scalar context will give you the size)

you assumed I said:
(sub returns a list or scalar) =>
             (call in scalar context will give you the size)
which is of course wrong.

if i know the wantarray function you could
assume i know what i can do with it =)

regards,
tina

--
http://tinita.de    \  enter__| |__the___ _ _ ___
tina's moviedatabase \     / _` / _ \/ _ \ '_(_-< of
search & add comments \    \__,_\___/\___/_| /__/ perception
please don't email unless offtopic or followup is set. thanx



Fri, 28 Feb 2003 05:35:37 GMT  
 Count of items is EXCEPTION not RULE (was Re: Little perl annoyance #371: glob)

Quote:

> I haven't yet seen a good (i.e. sufficiently memorable) account of the
> difference between arrays and lists in Perl.  In the 2nd edition of
> Programming Perl there is a section called "List Values and Arrays"
> (pp. 47-49) where the authors *hint* at the fact that lists and arrays
> are not equivalent, but they never explain what exactly is the
> difference.

The difference between a Perl list and a Perl array are at times
apparently just nominal.  For instance, you can index a Perl list as if
it were an array, eliminating the need for many temporary vars:

        print (qw/Sun Mon Tue Wed Thu Fri Sat/)[(localtime)[6]];
        print join(':', reverse( (localtime)[0..2] ) );

And of course, you can use an array almost anywhere you can use a list;
it's just not an array anymore.  However, you cannot use lists anywhere
you can use arrays; eg, push(), pop().

Any intricacies of the differences I better leave up to Larry, Randy,
or Tom (if they still follow this NG).  Beyond this I can't help you
much.  What I can say is that this blurred distinction between the
two is more a blessing than anything (to me, anyway).

Quote:
> I had a similar problem when learning C, because the first book I used
> did a very poor job clarifying the difference between pointers and
> arrays.  So for the first months as a C programmer I was always
> tripping, and then I read Harbison & Steele and all was clear.  But
> the Harbison & Steele of Perl is yet to be written, alas.

Trying to imagine a book that could fail to distinguish clearly between
C arrays and C pointers...

Alright, an unindexed C array simply returns a pointer to the first
element of the array.  Is this what you're referring to?

[Begin OT section]

Consider:

: #include <stdio.h>
:
: void main() {
:       char* str = "arbitrary";
:       printf("%c\n", str[1]);
: }

After running this little program, I was reminded of how interchangeable
C char arrays and char pointers are.  However, AFAIK this only applies
to char[] and char*.  I've found that indexing a dereferenced int*:

        int* x = new int(5);
        x[0];

...does not cause any errors.  But any attempt to _use_ the indexed pointer
caused a segmentation fault.

OTOH, the college professor who taught my C++ class was unaware of this
interchangeability. (Either that, or he didn't want to confuse
anyone...yet.)  =)

[End OT section]

--

I do not, for one, think that the problem was that the band was down.
I think that the problem may have been...that there was a Stonehenge
monument on the stage that was in danger of being crushed by a dwarf.
        -- David St. Hubbins, "This is Spinal Tap"



Fri, 28 Feb 2003 05:34:57 GMT  
 Count of items is EXCEPTION not RULE (was Re: Little perl annoyance #371: glob)

Quote:
> so that meant *if* the function returns *always* an array (or better,
> as randal said, a list), *then*

see Nat's post on the difference between arrays and lists.

Quote:
> $result = func();
> will *always* give you the size.
> your function does not always return a list, so there
> is no contradiction.

what the function returns depends on context.  a function
gets data through a list and returns data through a list
(perhaps one item).

Quote:
> or more logical, I said:
> (sub returns always list) =>
>              (call in scalar context will give you the size)

persisting in this sort of explanation will only cause confusion.
try this code:

   sub foo { return ( 1, 2, 4 ) }
   my $value = foo();
   print $value;

now, try to explain that result in terms of what you just said.

Quote:
> if i know the wantarray function you could
> assume i know what i can do with it =)

what you are saying is wrong.  i don't assume that you know
what you are doing when you are wrong.

--
brian d foy                    
CGI Meta FAQ <URL:http://www.smithrenaud.com/public/CGI_MetaFAQ.html>
Perl Mongers <URL:http://www.perl.org/>



Fri, 28 Feb 2003 05:47:39 GMT  
 Count of items is EXCEPTION not RULE (was Re: Little perl annoyance #371: glob)
hi,

Quote:

>> so that meant *if* the function returns *always* an array (or better,
>> as randal said, a list), *then*
> see Nat's post on the difference between arrays and lists.

yep.

Quote:
>> $result = func();
>> will *always* give you the size.
>> your function does not always return a list, so there
>> is no contradiction.
> what the function returns depends on context.  a function
> gets data through a list and returns data through a list
> (perhaps one item).
>> or more logical, I said:
>> (sub returns always list) =>
>>              (call in scalar context will give you the size)
> persisting in this sort of explanation will only cause confusion.
> try this code:
>    sub foo { return ( 1, 2, 4 ) }
>    my $value = foo();
>    print $value;
> now, try to explain that result in terms of what you just said.

nice effect, just read about this in perldata.
change that sub to

that is what i meant.

so what about
"if the sub returns a copy of an array"?


which creates a copy and returns the list and
not the actual array, to make this clear again)
then my statement would make sense, wouldn't it?

in your sub the value is assigned like
$result = (1, 2, 4);
in my sub:

so i shouldn't have said list instead of array;
i meant the list which is created as the
copy of the array.
phooey, i admit i was a little
surprised by your example...

tina

--
http://tinita.de    \  enter__| |__the___ _ _ ___
tina's moviedatabase \     / _` / _ \/ _ \ '_(_-< of
search & add comments \    \__,_\___/\___/_| /__/ perception
please don't email unless offtopic or followup is set. thanx



Fri, 28 Feb 2003 06:33:46 GMT  
 
 [ 26 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Little perl annoyance #371: glob

2. Need a little Help to have a blank space (I am not a programmer)

3. Need Perl help, but I am not a Perl Monger

4. Purl Gurl...I'm not learning any Perl, but I am laughing

5. setpgrp, setgid - not working, what am I missing?

6. Am I looking for something thats not there?

7. ppm fails now that I am on perl 5.6

8. newbie-perl or why am i blind to this simple

9. I am a perl beginner

10. Why I am glad I know perl

11. I am looking for PERL compiled for Apollo

12. Am curious about perl

 

 
Powered by phpBB® Forum Software