develooper Front page | perl.fwp | Postings from December 2001

Re: Santa Hole 5 (wc.pl) Post Mortem

Thread Previous | Thread Next
From:
BooK
Date:
December 7, 2001 05:03
Subject:
Re: Santa Hole 5 (wc.pl) Post Mortem
Message ID:
1007730185.3c10be0958967@imp3-1.free.fr
En réponse à Piers Cawley <pdcawley@bofh.org.uk>:

> Ian Phillipps <igp_list2@tesco.net> writes:
> >> --- Tim Ayers ------------------------ 21
> >> #!/usr/bin/perl
> >> printf"%010d\n",$.,<>
> >
> > Harumph. I remember something like this approach flitting through my
> > mind, but, I thought, the $. would be evaluated before the <>, so it
> > would be zero.
> >
> > Moral: try it!
> >
> > Hmm... why isn't it zero?
> 
> Because perl passes arguments by reference. So $. gets shoved on the
> stack (as $.) and <> gets evaluated in an array context so that the
> results can be shoved on the stack. So, when printf comes to read the
> value of $. '<>' has already been evaluated.

So I guess

#!/usr/bin/perl
printf"%010d\n","$.",<>

would print 0000000000 whatever the file. Doing what Ian thought it would do.

-- 
 Philippe BRUHAT - BooK

 When you run from your problem, you make it that much harder for good
 fortune to catch you, as well.     (Moral from Groo The Wanderer #14 (Epic))

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About