[9795] in Perl-Users-Digest
Perl-Users Digest, Issue: 3388 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Aug 7 10:07:06 1998
Date: Fri, 7 Aug 98 07:00:24 -0700
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Fri, 7 Aug 1998 Volume: 8 Number: 3388
Today's topics:
Re: ANNOUNCE: Free Perl Books for 5.005 - CRC Errors in (Nathan V. Patwardhan)
Bug in while-loops? <hklein@ikp.uni-koeln.de>
Re: Can't Match Multi-Line Pattern <tchrist@mox.perl.com>
Re: Can't Match Multi-Line Pattern <tchrist@mox.perl.com>
Re: comp.lang.perl.announce redux <tchrist@mox.perl.com>
Re: Intriguing coderef question <tchrist@mox.perl.com>
math error <T.Bridgwater@mindgear.com>
Re: perlfaq - frequently asked questions about Perl (pa <tchrist@mox.perl.com>
Re: perlfaq - frequently asked questions about Perl (pa (Andrew M. Langmead)
Possible to pass Associative Arrays to Subroutines?? (Ben Howe)
Re: re first language <tchrist@mox.perl.com>
Re: re first language <matt@pcr7.pcr.com>
Set-Cookie <mikane@shell3.ba.best.com>
Re: Speed Up Perl (Andy Lester)
Re: Speed Up Perl (Andrew M. Langmead)
Re: Teaching Perl (Tad McClellan)
Re: typeglob - question (M.J.T. Guy)
Re: variable indirection <tchrist@mox.perl.com>
Special: Digest Administrivia (Last modified: 12 Mar 98 (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 7 Aug 1998 13:47:24 GMT
From: nvp@shore.net (Nathan V. Patwardhan)
Subject: Re: ANNOUNCE: Free Perl Books for 5.005 - CRC Errors in text version archive?
Message-Id: <6qf0hc$2m8@fridge.shore.net>
: In comp.lang.perl.misc,
: cicho@polbox.com (Marek Jedlinski) writes:
: :Suggested solution: since gzip is unix-centric anyway, please provide a
: :.zip archive version as well.
Bologna. Get Winzip and be happy. AND if you've got a working Perl,
you'll have the benefit of both Compress::Zlib and Archive::Tar which
should work under Win32. The CPAN module can/will invoke these when
you download/install modules.
--
Nate Patwardhan|root@localhost
"Fortunately, I prefer to believe that we're all really just trapped in a
P.K. Dick book laced with Lovecraft, and this awful Terror Out of Cambridge
shall by the light of day evaporate, leaving nothing but good intentions in
its stead." Tom Christiansen in <6k02ha$hq6$3@csnews.cs.colorado.edu>
------------------------------
Date: 07 Aug 1998 15:35:36 +0200
From: Heiko Klein <hklein@ikp.uni-koeln.de>
Subject: Bug in while-loops?
Message-Id: <3paf5grk2f.fsf@hydra.ikp.Uni-Koeln.DE>
Hi,
I have a program with a global variabel @Files.
In a subroutine, I don't use this variable, but with a while-loop over
an input-file, this @Files is changed.
The Subroutine:
sub tv_peaks {
my $tvout = shift;
my $file = shift;
my $nr = shift;
$tvout = "output";
my %peak;
open (TVOUT, "$tvout") or die "Can't open $tvout\n";
my $check = 0;
my $test;
foreach $test (@Files) {
print "$test\n";
}
########################################################################
# foreach $_ (<TVOUT>) {
while (defined($_ = <TVOUT>)) {
}
############################################################
foreach $test (@Files) {
print "$test\n";
}
if ($check == 0) {
print "$file doesn't fit to $nr! Input-Files no mfile-Spectra?\n";
exit 1;
}
close TVOUT;
return %peak;
}
After the while-loop between the #####-lines, I have undefined entries
in @Files.
When I change from while to foreach $_ ..., everything runs as it
should.
This is the same under Perl 5.004 (Solaris 2.5.1, Linux 2.0.35) and
Perl 5.003 (Linux, 2.0.15).
The complete programm with input-files is available under:
http://www.ikp.uni-koeln.de/~hklein/buggy.tgz
Hope it's just personal incompetence and not a serious bug,
Heiko
------------------------------
Date: 7 Aug 1998 13:38:08 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: Can't Match Multi-Line Pattern
Message-Id: <6qf000$8b4$1@csnews.cs.colorado.edu>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc, cberry@cinenet.net (Craig Berry) writes:
:Freidl explains them in glorious detail and emphasizes the "/m affects
:*only* ^$, /s affects *only* ." tack which I took in my previous message.
:However, all the same semantics are there in perlre.
But that's false: /s affects ^ and $ as well, insulating them from
unpleasant surprises out of $* settings.
--tom
#!/usr/bin/perl -00
use strict;
use FileHandle;
$| = 1;
my $DELAY = 2;
my $RANDOMIZE = 1;
my @questions;
while (<DATA>) {
next if /===/;
my %record;
(undef, %record) = split /^([^:\s]+):\s*/m;
for (@record{keys %record}) { s/\s+/ /g }
if ($record{Question}) { push @questions, \%record}
elsif($record{Answer} ) { push @{ $questions[-1]{Answers} }, \%record}
else { die "unknown record $_" }
}
rand_questions();
exit;
show_full_quiz();
####################################
sub rand_questions {
my $q = @questions[rand @questions];
for (my $qnum = 0; $qnum < $#questions; $qnum++) {
system('clear');
$q = $questions[$qnum];
question("Q".(1+$qnum), $q->{Question});
my $anum = 'a';
my @answers = scramble( (@{$q->{Answers}}) );
for my $a ( @answers ) {
answer($anum++, $a->{Answer});
}
delay(5);
print "\nANSWERS:\n\n";
for ( my $i = 0; $i <= $#answers; $i++ ) {
my $a = $answers[$i];
explain( ( 'a' .. 'z' )[$i], $a->{Correct} . $a->{Why} );
print "\n";
delay(2 + length($a->{Why})/50);
}
delay(5);
}
}
sub delay {
my $count = shift;
sleep(1 + $DELAY * $count);
}
sub rand_question {
my $q = @questions[rand @questions];
system('clear');
question('Q', $q->{Question});
my $anum = 'a';
my @answers = scramble( (@{$q->{Answers}}) );
for my $a ( @answers ) {
answer($anum++, $a->{Answer});
}
delay(3);
print "\nANSWERS:\n\n";
for ( my $i = 0; $i <= $#answers; $i++ ) {
answer( ( 'a' .. 'z' )[$i],
$answers[$i]{Correct} .
$answers[$i]{Why} );
print "\n";
delay(1);
}
print "\n";
delay(5);
}
sub show_full_quiz {
my $qnum = '01';
for my $q (scramble(@questions)) {
fwrite ($qnum++, $q->{Question});
my $anum = 'a';
for my $a ( scramble(@{$q->{Answers}}) ) {
fwrite(' '.$anum++.':', $a->{Answer});
}
print "\n";
}
}
sub scramble {
return @_ unless $RANDOMIZE;
my @list;
srand(time() ^ ($$ + ($$ << 15)));
push(@list, splice (@_, rand @_, 1)) while @_;
return @list;
}
sub fwrite {
my($num, $text) = @_;
sub question { STDOUT->format_name("Question"); &fwrite; }
sub answer { STDOUT->format_name("Answer"); &fwrite; }
sub explain { STDOUT->format_name("Explain"); &fwrite; }
format Question =
@>>: ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$num, $text
~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$text
.
format Answer =
@>>: ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$num, $text
~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$text
.
format Explain =
@>>: ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$num, $text
~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$text
.
write;
}
__DATA__
Question: How do you produce a reference to a list?
Type: References
Difficulty: 6/7 (Hard)
Answer: \@array
Correct: No.
Why: @array is not a list, but an array.
Answer: [ @array ]
Correct: No.
Why: That makes a reference to a newly allocated anonymous
array, and populates it with a copy of the contents
of @array.
Answer: \($s, @a, %h, &c)
Correct: No.
Why: The backslash operator is distributive across a list, and
produces a list in return, this being (\$s, \@a, \%h, \&c).
Well. In list context. In scalar context, it's a strange
way to get a reference to the function &c.
Answer: You can't.
Correct: Yes.
Why: A list is not an array, although is many places one may be
used for the other. An array has an AV allocated, whereas a
list is just some values on a stack somewhere. You cannot
alter the length of a list, for example, any more than
you could alter a number by saying something like 23++.
While an array contains a list, it is not a list itself.
========================================================================
Question: What happens when you return a reference to a private variable?
Type: References
Difficulty: 4/7 (Medium)
Answer: You get a core dump later when you use it.
Correct: No.
Why: Perl is not C or C++.
Answer: The underlying object is silently copied.
Correct: No.
Why: Even though the reference returned is for all intents
and purposes a copy of the original (Perl uses return
by reference), the underlying referent has not changed.
Answer: The Right Thing (tm).
Correct: Yes.
Why: Perl keeps track of your variables, whether dynamic or
otherwise, and doesn't free things before you're done using
them.
Answer: The compiler doesn't let you.
Correct: No.
Why: Perl seldom stops you from doing what you want to do,
and tries very hard to do what you mean to do. This
is one of those cases.
========================================================================
Question: Why aren't Perl's patterns regular expressions?
Type: Regular expressions
Difficulty: 3/7 (Medium)
Answer: Because Perl patterns have backreferences.
Correct: Yes.
Why: A regular expression by definition must be
able to determine the next state in the finite
automaton without requiring any extra memory
to keep around previous state. A pattern /([ab]+)c\1/
requires the state machine to remember old
states, and thus disqualifies such patterns
as being regular expressions in the classic sense
of the term.
Answer: Because Perl allows both minimal matching and maximal
matching in the same pattern.
Correct: No.
Why: The mere presence of minimal and maximal repetitions
does not disqualify a language from being "regular".
Answer: Because Perl uses a non-deterministic finite automaton
rather than a deterministic finite automaton.
Correct: No.
Why: Both NFAs and DFAs can be used to solve regular
expressions. Given an NFA, a DFA for it can be constructed,
and vice versa. For example, classical grep uses an NFA,
while classical egrep a DFA. Whether a pattern matches
a particular string doesn't change, but where the match
occurs may. In any case, they're both regular. However,
an NFA can also be modified to handle backtracking, while
a DFA cannot.
Answer: Because Perl patterns can have look-aheads assertions
and negations.
Correct: No.
Why: The `(?=foo)' and `(?!foo)' constructs no more violate
whether the language is regular than do `^' and `$',
which are also zero-width statements.
========================================================================
Question: What happens to objects lost in "unreachable" memory,
such as the object returned by Ob->new() in
`{ my $ap; $ap = [ Ob->new(), \$ap ]; }' ?
Type: Objects
Difficulty: 4/7 (Medium)
Answer: Their destructors are called when that interpreter thread
shuts down.
Correct: Yes.
Why: When the interpreter exits, it first does an exhaustive
search looking for anything that it allocated. This allows
Perl to be used in embedded and multithreaded applications
safely, and furthermore guarantees correctness of object
code.
Answer: Their destructors are called when the memory becomes unreachable.
Correct: No.
Why: Under the current implementation, the reference-counted
garbage collection system will not notice that the object
in $ap's array cannot be reached, because the array reference
itself never has its reference count go to zero.
Answer: Their destructors are never called.
Correct: No.
Why: That would be very bad, because then you could have objects
whose class-specific cleanup code didn't get called ever.
Answer: Perl doesn't support destructors.
Correct: No.
Why: A class's DESTROY function, or that of its base classes,
is called for any cleanup. It is not expected to deallocate
memory, however.
========================================================================
Question: How do you give functions private variables that
retain their values between calls?
Type: Subroutines, Scoping
Difficulty: 5/7 (Medium)
Answer: Perl doesn't support that.
Correct: No.
Why: It would be difficult to keep private state in a
function otherwise.
Answer: Include them as extra parameters in the prototype list,
but don't pass anything in at that slot.
Correct: No.
Why: Perl is not the Korn shell, nor anything like that.
If you tried this, your program probably wouldn't
even compile.
Answer: Use localized globals.
Correct: No.
Why: The local() operator merely saves the old value of a global
variable, restoring that value when the block in which the
local occurred exits. Once the subroutine exits, the
temporary value is lost. Before then, other functions
can access the temporary value of that global variable.
Answer: Create a scope surrounding that sub that contains lexicals.
Correct: Yes.
Why: Only lexical variables are truly private, and they will
persist even when their block exits if something still
cares about them. Thus:
{ my $i = 0; sub next_i { $i++ } sub last_i { --$i } }
creates two functions that share a private variable. The
$i variable will not be deallocated when its block goes
away because next_i and last_i need to be able to access it.
========================================================================
Question: What value is returned by a lone `return;' statement?
Type: Subroutines, Syntax
Difficulty: 3/7 (Medium)
Answer: The undefined value.
Correct: No.
Why: That would only be true in scalar context.
Answer: The empty list value ().
Correct: No.
Why: That would only be true in list context.
Answer: The undefined value in scalar context, and
the empty list value () in list context.
Correct: Yes.
Why: This way functions that wish to return failure
can just use a simple return without worrying about
the context in which they were called.
Answer: The result of the last evaluated expression in that
subroutine's block.
Correct: No.
Why: That's what happens when the function ends without
return being used at all.
========================================================================
Question: Assuming $_ contains HTML, which of
the following substitutions will remove all tags in it?
Type: Regular Expressions, WWW
Difficulty: 6/7 (Hard)
Answer: You can't do that.
Correct: Yes.
Why: If it weren't for HTML comments, improperly formatted
HTML, and tags with interesting data like <SCRIPT>, you
could do this. Alas, you cannot. It takes a lot
more smarts, and quite frankly, a real parser.
Answer: s/<.*>//g;
Correct: No.
Why: As written, the dot will not cross newline boundaries, and the
star is being too greedy. If you add a /s, then yes,
it will remove all tags -- and a great deal else besides.
Answer: s/<.*?>//gs;
Correct: No.
Why: It is easy to construct a tag that will cause this to fail,
such as the common `<IMG SRC='foo.gif' ALT="> ">' tag.
Answer: s/<\/?[A-Z]\w*(?:\s+[A-Z]\w*(?:\s*=\s*(?:(["']).*?\1|[\w-.]+))?)*\s*>//gsix;
Correct: No.
Why: For a good deal of HTML, this will actually work, but
it will fail on cases with annoying comments, poorly formatted
HTML, and tags like <SCRIPT> and <STYLE>, which can contain
things like `while (<FH>) {}' without those being counted
as tags. Comments that will annoy you include
<!-- <foo bar = "-->">
which will remove characters when it shouldn't; it's just
a comment followed by `">'. And even something like this:
<!-- <foo bar = "-->
Most browsers will get right, but the substitution will not.
And if you have improper HTML, you get into even more
trouble, like this:
<foo bar = "bleh" @>
text text text
<foo bar = "bleh">
in which case the .*? will gobble up way more than you
thought it would.
========================================================================
Question: Assuming both a local($var) and a my($var) exist,
what's the difference between ${var} and ${"var"}?
Type: Scope
Difficulty: 5/7 (Medium)
Answer: ${var} is the lexical variable $var, and
${"var"} is the dynamic variable $var.
Correct: Yes.
Why: Odd though it appears, this is how it works. Note that
because the second is a symbol table lookup, it is
disallowed under `use strict "refs"'. The words
global, local, package, symbol table, and dynamic
all refer to the kind of variables that local()
affects, whereas the other sort, those governed by
my(), are variously knows as private, lexical, or scoped
variable.
Answer: ${var} is the package variable $var, and
${"var"} is the scoped variable $var.
Correct: No.
Why: Try again. You're close.
Answer: There is no difference.
Correct: No.
Why: One is the scoped
variable, the other the package variable. Which is
which though?
Answer: ${var} is a package variable $var, and ${"var"}
a global variable $var.
Correct: No.
Why: There is no difference between a package variable and
a global variable. All package variables are globals,
and vice versa.
========================================================================
Question: Which of these is a difference between C++ and Perl?
Type: Objects, Scope, C++
Difficulty: 6/7
Answer: Perl can have objects whose data cannot be accessed
outside its class, but C++ cannot.
Correct: Yes.
Why: Perl can use closures with unreachable private data as
objects, and C++ doesn't support closures. Furthermore, C++
does support pointer arithmetic via `int *ip = (int*)&object',
allowing you do look all over the object. Perl doesn't have
pointer arithmetic. It also doesn't allow `#define private
public' to change access rights to foreign objects. On
the other hand, once you start poking around in /dev/mem,
no one is safe.
Answer: C++ can have objects whose data cannot be accessed
outside its class, but Perl cannot.
Correct: No.
Why: See the correct answer for why.
Answer: C++ supports multiple inheritance, but Perl does not.
Correct: No.
Why: Both support multiple inheritance.
Answer: C++ will not call destructors on objects that go out
of scope if a reference to that object still exists,
but Perl will.
Correct: No.
Why: Exchange "Perl" and "C++" in that answer, and you would
be telling the truth. C++ is too primitive to know when
an object is no longer in use, because it has no garbage
collection system. Perl does.
========================================================================
Question: What does Perl do if you try to exploit the execve(2)
race involving setuid scripts?
Type: Security, Folklore
Difficulty: 2/7 (Easy)
Answer: Reboots your machine.
Correct: No.
Why: An appealing idea, though, isn't it? After all, Perl does
possess super(user)powers at this point. You just never know
what it might do. In the interests of courtesy, though,
Perl stays out of your power supply just as it stays
out of your living room.
Answer: Sends mail to root and exits.
Correct: Yes.
Why: It has been said that all programs advance to the
point of being able to automatically read mail. While
not quite at that point (well, without having a module
loaded), Perl does at least automatically send it.
Answer: Runs the fake script with setuid perms.
Correct: No.
Why: That would be bad. Very Bad. What do you think we
are? A shell or something?
Answer: Runs the fake script, but without setuid perms.
Correct: No.
Why: It would be improper to run anything at all in the
face of such naughtiness.
========================================================================
Question: How do you print out the next line from a filehandle
with all its bytes reversed?
Type: I/O, Built-in Functions, Context
Difficulty: 5/7
Answer: print reverse <FH>
Correct: No.
Why: That reads all lines in FH, then reverses that list of
lines and passes the resulting reversed list off to print.
This is actually a very useful thing, and simulates
`tail -r' behavior but without the annoying buffer
limitations of that utility. Nonetheless, it's not
what we want for an answer to this question.
Answer: print reverse scalar <FH>
Correct: No.
Why: Although `scalar <FH>' did retrieve just the next line,
the reverse is still in the list context imposed on it by
print, so it takes its list of one element and reverses
the order of the list, producing exactly the next line.
An expensive way of writing `print scalar <FH>'.
Answer: print scalar reverse scalar <FH>
Correct: Yes.
Why: Surprisingly enough, you have to put both the reverse and
the <FH> into scalar context separately for this to work.
Answer: print scalar reverse <FH>
Correct: No.
Why: Although the first use of scalar inhibits the list context
being imposed on reverse by print, it doesn't carry through
to change the list context that reverse is imposing on <FH>.
So reverse catenates all its arguments
and does a byte-for-byte flip on the resulting string.
========================================================================
Question: Why is it hard to call this function: sub y { "because" }
Type: User Functions, Operators
Difficulty: 4/7 (Medium)
Answer: Because y is a kind of quoting operator.
Correct: Yes.
Why: The y/// operator is the sed-savvy synonym for tr///.
That means y(3) would be like tr(), which would be looking
for a second string, as in tr/a-z/A-Z/, tr(a-z)(A-Z),
or tr[a-z][A-Z].
Answer: It's not.
Correct: No.
Why: Most people don't call functions with ampersands anymore.
If they did, as in &y(), it wouldn't be so hard.
Answer: Because y is a predefined function.
Correct: No.
Why: y isn't really a function, per se. If it were, you
would never see y!abc!xyz!, since proper functions
do not like getting banged on that way.
Answer: Because it has no prototype.
Correct: No.
Why: Functions don't require prototypes in Perl.
========================================================================
Question: What does `$result = f() .. g()' really return?
Type: Operators
Difficulty: 5/7 (Medium)
Answer: The last number from the list of numbers
returned in the the range between f()'s return
value and g()'s.
Correct: No.
Why: That might work in list context, but never in scalar.
The list `..' operator is a totally different creature
than the scalar one. They're just spelled the same
way, kind of like when you can the rusty old can down
by the guys' can just because you can. Context,
as always, is critical.
Answer: Produces a syntax error.
Correct: No.
Why: You'd be amazed at how many things in Perl don't
cause syntax errors.
Answer: True if and only if both f() and g() are true,
or if f() and g() are both false, but returns false
otherwise.
Correct: No.
Why: That sounds more like a negated logical xor. A logical
xor is `!$a != !$b', so you've just described `!$a == !$b'.
Interesting, and perhaps even useful, but unrelated to
`..', our scalar range operator.
Answer: False so long as f() returns false, after
which it returns true until g() returns true,
and then starts the cycle again.
Correct: Yes.
Why: This is scalar not list context, so we have the bistable
flip-flop range operator famous in parsing of mail messages,
as in `$in_body = /^$/ .. eof()'. Except for the first
time f() returns true, g() is entirely ignored, and f()
will be ignored while g() later when g() is evaluated.
Double dot is the inclusive range operator, f() and
g() will both be evaluated on the same record. If you
don't want that to happen, the exclusive range operator,
triple dots, can be used instead. For extra credit,
describe this:
$bingo = ( a() .. b() ) ... ( c() .. d() );
========================================================================
Question: Why does Perl not have overloaded functions?
Type: User Functions
Difficulty: 4/7 (Medium)
Answer: Because it's too hard.
Correct: No.
Why: Just because it's hard isn't likely to rule out
something from being implemented -- someday.
Answer: Because you can inspect the argument count,
return context, and object types all by yourself.
Correct: Yes.
Why: In Perl, the number of arguments is trivially available
to a function via the scalar sense of @_, the return
context via wantarray(), and the types of the
arguments via ref() if they're references and
simple pattern matching like /^\d+$/ otherwise.
In languages like C++ where you can't do this, you
simply must resort to overloading of functions.
Answer: It does, along with overloaded operators as well
as overridden functions and methods.
Correct: No.
Why: Actually, Perl does support overloaded operators via `use
overload', overridden functions as in `use Cwd qw!chdir!',
and overridden methods via inheritance and polyphormism.
It just doesn't support functions automatically overloaded
on parameter signature or return type. Not that such
isn't longed
Answer: Because Perl doesn't have function prototypes.
Correct: No.
Why: Perl actually does have function prototypes, but this
isn't used for the traditional sort of prototype
checking, but rather for creating functions that
exactly emulate Perl's built-ins, which can implicitly
force context conversion or pass-by-reference without
the caller being aware of this.
========================================================================
Question: What does read() return at end of file?
Type: I/O, Built-in Functions
Difficulty: 2/7 (Easy)
Answer: undef
Correct: No.
Why: That would signal an I/O error, not normal end of file.
The circumfix operator <> returns undef when it reaches end
of file, but a normal read does not.
Answer: 0
Correct: Yes.
Why: A defined (but false) 0 value is the proper indication of the end of
file for read() and sysread().
Answer: "0 but true"
Correct: No.
Why: You're thinking of the ioctl() and fcntl() functions which
return this when the C version returned 0, reserving
undef for when the C version returns -1. For example,
`fcntl(STDIN,F_GETFL,1)' returns "0 but true" depending on
whether and how standard has been redirected. (The F_GETFL
flag was loaded from the Fcntl.pm module.)
Answer: "\0"
Correct: No.
Why: That's a string of length 1 consisting of the ASCII NUL
character, whose ord() is 0, which is false. The string,
however, is true. read() doesn't return strings, but
rather byte-counts.
========================================================================
Question: What does `new $cur->{LINK}' do? (Assume the
current package has no new() function of its own.)
Type: Objects
Difficulty: 6/7 (Hard)
Answer: $cur->{LINK}->new()
Correct: No.
Why: Just because it looks like a unary function doesn't
mean a method call parses like one. You just
want it to work this way. If you want that,
write that.
Answer: $cur->new()->{LINK}
Correct: Yes.
Why: The indirect object syntax only has a single token
lookahead. That means if new() is a method, it only
grabs the very next token, not the entire following
expression.
This is why `new $obj[23] arg' does't work, as well as
why `print $fh[23] "stuff\n"' does't work.
Mixing notations between the OO and IO notations is perilous. If you always use
arrow syntax for method calls, and nothing else, you'll
not be surprised.
Answer: new($cur->{LINK})
Correct: No.
Why: If the current package did in fact have its own new()
function, then this would be the right answer, but for the
wrong reasons. Within a class, it might appear to make no
difference since the new() subroutine would get its argument in $_[0]
called as a function or a method. However, a method call
can use inheritance, while a function call never does.
That means esoteric overridden new() methos would be
duped out of calling their derived class's constructor first,
and we wouldn't want that to happen, now would we?
Answer: $cur ? ($cur->{LINK}->new()) : (new()->{LINK})
Correct: No.
Why: Perl may be crazy, but it's not quite that crazy. Yet.
========================================================================
Question: What's the difference between /^Foo/s and /^Foo/?
Type: Regular Expressions
Difficulty: 5/7 (Medium)
Answer: There is no difference because /s only affects whether dot
can match newline.
Correct: No.
Why: /s does more than that.
Answer: The first would allow the match to cross newline boundaries.
Correct: No.
Why: /s only makes a dot able to cross a newline, and then
only if the string actually has a newline in it.
Answer: The first would match Foo other than at the start
of the record if the previous match were /^Foo/gcm,
new in the 5.004 release.
Correct: No.
Why: Although the /c modifier is indeed new as of 5.004 (and
is used with /g), this has no particular interaction with /s.
Answer: The second would match Foo other than at the start
of the record if $* were set.
Correct: Yes.
Why: The deprecated $* flag does double duty, filling the
roles of both /s and /m. By using /s, you suppress
any settings of that spooky variable, and force your
carets and dollars to match only at the ends of the
string and not at ends of line as well -- just as
they would if $* weren't set at all.
========================================================================
Question: What does length(%HASH) produce if you have thirty-seven
random keys in a newly created hash?
Type: Hashes, Built-in Functions, Context
Difficulty: 5/7 (Medium)
Answer: 2
Correct: No.
Why: You probably think it decided there were 37 keys, and
that length(37) is 2. Close, but not quite.
Answer: 5
Correct: Yes.
Why: length() is a built-in prototyped as sub length($),
and a scalar prototype silently changes aggregates into
radically different forms. The scalar sense of a hash is
false (0) if it's empty, otherwise it's a string representing
the fullness of the buckets, like "18/32" or "39/64".
The length of that string is likely to be 5. Likewise,
`length(@a)' would be 2 if there were 37 elements in @a.
Answer: 37
Correct: No.
Why: `length %HASH' is nothing at all like `scalar keys %HASH',
which is a good bit more useful, in general.
Answer: 74
Correct: No.
Why: `length %HASH' is nothing at all like the size of the
list of all the keys and values in %HASH.
========================================================================
Question: If EXPR is an arbitrary expression, what is the
difference between $Foo::{EXPR} and *{"Foo::".EXPR}
Type: Namespaces, Typeglobs
Difficulty: 6/7
Answer: The first can create new globs dynamically, but
the second cannot.
Correct: No.
Why: Although you can get package Foo's symbol table via
the hash %Foo::, you cannot usefully generate new
typeglobs (symbols) this way. You could copy
old ones into that slot, though, effectively doing
the Exporter's job by hand.
Answer: The second is disallowed under `use strict "refs"'.
Correct: Yes.
Why: Dereferencing a string with *{"STR"} is disallowed
under the refs stricture, although *{STR} would not
be. This is similar in spirit to the way ${"STR"}
is always the symbol table variable, while ${STR}
may be the lexical variable. If it's not a
bareword, you're playing with the symbol table
in a particular dynamic fashion.
Answer: The first only happens at runtime, the second at only compile time.
Correct: No.
Why: Assuming that the expressions don't get resolved at
compile time, this all has to wait until run time.
Something like *Foo::varname, however, would be looked
up at compile time.
Answer: One is just a regular hash, the other a typeglob access
for a strangely named variable.
Correct: No.
Why: The %Foo:: hash is always the symbol table associated
with package Foo; such a hash can hardly be called
regular. Both versions actually refer to the same
typeglob, although somewhat differently.
========================================================================
Question: When would `local $_' in a function ruin your day?
Type: Regular Expressions, Scoping
Difficulty: 6/7 (Hard)
Answer: When your caller was in the middle for a foreach(@a) loop.
Correct: No.
Why: This looks close to the bizarre phenomenon known as
variable suicide, but as of this writing, you should be
safe from it.
Answer: When your caller was in the middle for a while(<>) loop.
Correct: No.
Why: However, if you do a `while(<>)' and forget to first
localize $_, you'll hurt someone above you. That's
because even though foreach() implicitly localizes
$_, a while(<>) does not.
Answer: When your caller was in the middle for a while(m//g) loop
Correct: Yes.
Why: The /g state on a global variable is not protected by
running local on it. That'll teach you to stop using
locals. Too bad $_ can't be the target of a my() -- yet.
Answer: When $_ was imported from another module.
Correct: No.
Why: Doing a local() on an imported variable is not harmful.
Of course, in the case of $_, it's virtually unnecessary,
since $_ is always forced to mean the version in the
main package, that is, `$main::_'.
========================================================================
Question: How do you match one letter in the current locale?
Type: Regular Expressions
Difficulty: 4/7 (Medium)
Answer: /[a-zA-Z]/
Correct: No.
Why: You forgot the locale-specific letters.
Answer: /[a-z]/i
Correct: No.
Why: You still forgot the locale-specific letters. The /i
flag doesn't bring them in.
Answer: /[^\W_\d]/
Correct: Yes.
Why: We don't have full POSIX regexps, so you can't get at
the isalpha() <ctype.h> macro save indirectly. You ask
for one byte which is neither a non-alphanumunder, nor
an under, nor a numeric. That leaves just the alphas,
which is what you want.
Answer: /[:isalpha:]/
Correct: No.
Why: Lamentably, this reasonably standard syntax is not
yet supported in Perl.
--
Ken is very smart but also very opinionated. --Doug Gwyn
------------------------------
Date: 7 Aug 1998 13:35:58 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: Can't Match Multi-Line Pattern
Message-Id: <6qevru$6ls$6@csnews.cs.colorado.edu>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc,
cberry@cinenet.net (Craig Berry) writes:
:/s has only one effect -- changing how . is interpreted.
You forgot $*. Writing /^From:?/s is perfectly sensible.
m Treat string as multiple lines. That is, change "^" and "$"
from matching at only the very start or end of the string to the
start or end of any line anywhere within the string,
s Treat string as single line. That is, change "." to match
any character whatsoever, even a newline, which it normally would
not match.
The /s and /m modifiers both override the $* setting. That is,
no matter what $* contains, /s (without /m) will force "^" to
match only at the beginning of the string and "$" to match only
at the end (or just before a newline at the end) of the string.
Together, as /ms, they let the "." match any character whatsoever,
while yet allowing "^" and "$" to match, respectively, just after
and just before newlines within the string.
--tom
--
: The ksh scripts do not have a problem with it.
That's because ksh doesn't much mind opening up security holes. The
absence of taint checks is not exactly a feature.
Larry Wall in <1994Dec15.013044.16127@netlabs.com>
------------------------------
Date: 7 Aug 1998 13:16:09 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: comp.lang.perl.announce redux
Message-Id: <6qeump$6ls$5@csnews.cs.colorado.edu>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc,
Ian Boys <boys@aspentech.com> writes:
:Perhaps, yes. English is not the same language as American, as you
:rapidly find out if you try visiting for any significant time in that
:country :-)
Smiley aside, *please* stop this stupid statement. It's not true, and
you're confused. Americans and Briton both speak English. If you'd
like to find a different language, go to China or Peru. Or Wales. :-)
--tom
--
It was a book to kill time for those who liked it better dead.
------------------------------
Date: 7 Aug 1998 13:14:07 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: Intriguing coderef question
Message-Id: <6qeuiv$6ls$4@csnews.cs.colorado.edu>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc, mjtg@cus.cam.ac.uk (M.J.T. Guy) writes:
:Not _quite_ true. If you run under -d, the source is kept - see
:perldoc perldebug for details. Not that I particularly recommend
:doing it that way.
Sometimes clarity of explanation must overrule "oh by the ways".
I thought about this point, and decided to avoid its mention, as it
didn't appear particularly practical for this poster's purposes.
--tom
--
Basically, avoid comments. If your code needs a comment to be understood,
it would be better to rewrite it so it's easier to understand. --Rob Pike
------------------------------
Date: Fri, 07 Aug 1998 14:10:41 -0700
From: Tom Bridgwater <T.Bridgwater@mindgear.com>
Subject: math error
Message-Id: <35CB6D51.87196064@mindgear.com>
I've come across a strange problem with integer vs. floating point math
that seems like Perl 5.004 isn't handling correctly. The increment in
the following code doesn't work under certain circumstanes. I haven't
been able to pinpoint those circumstances exactly, but the code suggests
paramaters which do illustrate the problem.
If you decrease that first paramater below something in the mid-40000's
then it does work correctly, so suspect that there may be a problem with
overflow. I guess it could also have something to do with order of
operation.
If anyone is into debugging Perl itself, I'd really appreciate a tip
about what's going wrong here.
--TomB
sub domath {
$val = $_[0];
print " val: $val\n";
$val *= $_[1]/$_[2];
print " div: $val\n";
$val1 = $val;
$val2 = $val;
$val3 = $val;
$val1 = 1 + int $val1; # wrong
$val2 = int ++$val2; # wrong
$val3 = 1 + int $val3 . '.'; # works
print "inc1: $val1 (??)\n";
print "inc2: $val2 (??)\n";
print "inc3: $val3 (ok)\n";
}
print "Try '50000 23 5' to see the problem\n";
if (@ARGV < 3) { die "need three numbers\n"; }
&domath (@ARGV);
------------------------------
Date: 7 Aug 1998 13:07:55 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: perlfaq - frequently asked questions about Perl (part 0 of 9)
Message-Id: <6qeu7b$6ls$2@csnews.cs.colorado.edu>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc, pudge@pobox.com (Chris Nandor) writes:
:In addition to perldoc on Win32 and Shuck on Mac OS, POD is just a very
:simple text markup and is easily readable with any text editor.
Er, don't you mean pager? Why are you editing?
--tom
--
A power tool is not a toy. Unix is a power tool.
------------------------------
Date: Fri, 7 Aug 1998 13:43:28 GMT
From: aml@world.std.com (Andrew M. Langmead)
Subject: Re: perlfaq - frequently asked questions about Perl (part 0 of 9)
Message-Id: <ExBnGG.MKL@world.std.com>
"Curtis Jewell / Dennis Whalen" <cjdpcssd@ims-1.com> writes:
>What about those unlucky ones who do not have a man (Win32, Mac)? I've been
>looking for a man variant at all (much less that RUNS) on Win95 for a YEAR.
All the ports of perl have the standard documentation converted into a
format which should be convenient to access for the environment in
which it runs.
The Win32 ports of perl have the documentation converted into HTML
pages. (And in Microsoft's newest release of a Win32 operating system,
it has an HTML browser built in. Even previous releases they bundled
one.)
The Mac port has a program called "Shuck" which decodes POD
formatting. The MacPerl IDE will start Shuck with the appropriate file
when the programmer accesses the Help menu.
The OS/2 port has the documentation converted into .INF format.
--
Andrew Langmead
------------------------------
Date: Fri, 07 Aug 1998 05:20:49 -0800
From: benhowe51-NO@SPAM-hotmail.com (Ben Howe)
Subject: Possible to pass Associative Arrays to Subroutines??
Message-Id: <902495919.19577@wren.supernews.com>
Does anybody out there know how to pass associative arrays to subroutines without
having do deal with resorting everything when it arrives?
-**** Posted from Supernews, Discussions Start Here(tm) ****-
http://www.supernews.com/ - Host to the World's Discussions & Usenet
------------------------------
Date: 7 Aug 1998 13:43:08 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: re first language
Message-Id: <6qf09c$8b4$2@csnews.cs.colorado.edu>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc,
"Matt Heusser" <matt@pcr7.pcr.com> writes:
:Answer - you might want to look at other languages,
:but Assembler won't be one of them until you have
:a real good grasp of C.
I think you mean that the other way around. Knowing assembler
helps in learning C. I don't know about vice versa, though.
--tom
--
"The purpose of most computer languages is to lengthen your resume by
a word and a comma." --Larry Wall
------------------------------
Date: 7 Aug 1998 13:52:48 GMT
From: "Matt Heusser" <matt@pcr7.pcr.com>
Subject: Re: re first language
Message-Id: <01bdc20a$8572d9c0$47eb1bcc@XSTA71.pcr.com>
> I think you mean that the other way around. Knowing
> assembler helps in learning C. I don't know about
> vice versa, though.
<Anecdote Warning>
In my assembler course we had two guys who coded
C/C++. By the end of course, it was obvious that
they were having an easier time than the rest of us,
and that they'd learned to understand and appreciate
C a bit more. So, I think it works both way. ;-)
Matt H.
------------------------------
Date: 7 Aug 1998 13:41:03 GMT
From: <mikane@shell3.ba.best.com>
Subject: Set-Cookie
Message-Id: <6qf05f$obn$1@nntp1.ba.best.com>
I am trying to send a cookie with the following code but its
not working, can someone tell me why?
All I get is a page with:
"Set-Cookie: ID=m4lvrt; HOME=usa; expires:Friday, 7-Aug-98 23:48:56 GMT Hello world"
and no cookie.
Thanks
Mike
-----------------------------------------------------------
#!/bin/perl
print<<NEWPAGE;
Content-type: text/html
Set-Cookie: ID=m4lvrt; HOME=usa; expires:Friday, 7-Aug-98 23:48:56 GMT
<html>
<head>
<title></title></head>
<body>
Hello world
</body>
</html>
NEWPAGE
exit;
------------------------------
Date: 7 Aug 1998 13:34:28 GMT
From: petdance@maxx.mc.net (Andy Lester)
Subject: Re: Speed Up Perl
Message-Id: <6qevp4$np8$3@supernews.com>
: Does anyone know how to speed-up perl scripts ?
Yes.
: If got a quit large
: script running and it takes too long before it responds.
Figure out which part is taking the longest and get back to us.
xoxo,
Andy
--
--
Andy Lester: <andy@petdance.com> http://tezcat.com/~andy/
Chicago Shows List: <shows@ChicagoMusic.com> http://ChicagoMusic.com/
------------------------------
Date: Fri, 7 Aug 1998 13:52:13 GMT
From: aml@world.std.com (Andrew M. Langmead)
Subject: Re: Speed Up Perl
Message-Id: <ExBnv1.5Dv@world.std.com>
user name <usern@interaccess.nl> writes:
>Does anyone know how to speed-up perl scripts ? If got a quit large
>script running and it takes too long before it responds.
You could use the DProf module to find where your program is spending
the most time, and then you could look different ways of coding those
sections.
--
Andrew Langmead
------------------------------
Date: Fri, 7 Aug 1998 08:23:33 -0500
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: Teaching Perl
Message-Id: <l4veq6.bq2.ln@localhost>
Martin (minich@globalnet.co.uk) wrote:
: >Since I only have one day, and since none of the people
: >I will be working with have any knowledge of perl
: >at all, I am going to try to present a broad overview
: >of the basics of programming in perl. I'm currently
: >planning on presenting the following topics-
: I would recommend teaching the basic regexps, at least
: s///
That is not a regex.
That is an operator (which is why it is described in 'perlop'
rather than in 'perlre')
The regex is what goes between the first two delimiters.
I think most folks know what you meant even though you didn't
say what you meant ;-)
But technically correct terminology is needed when discussing
technical topics.
: and tr///
That does not use regexes _at all_!
If the original poster adds coverage of tr///, maybe he should
send that part to you ;-)
: as these are one of the great things that make
: Perl easy to use for parsing tasks that would be a lot
: more complex in other languages.
I agree that at least some regex stuff should be included in
an introduction to Perl.
--
Tad McClellan SGML Consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: 7 Aug 1998 13:03:03 GMT
From: mjtg@cus.cam.ac.uk (M.J.T. Guy)
Subject: Re: typeglob - question
Message-Id: <6qetu7$nk5$1@pegasus.csx.cam.ac.uk>
Jonathan Feinberg <jdf@pobox.com> wrote:
>root@am.westblaak.spirit.nl (root) writes:
>
>> if (defined (&{*getpid})) { print "\&getpid\n"; }
>
>As you found out, &{*foo} invokes sub foo.
Not when it's the argument to defined(). Did you try it?
And the original poster's problems are caused by the fact that POSIX.pm
uses AUTOLOAD, so subroutines are only loaded (and hence defined() )
when they are first called.
Note that you get similar problems when you use UNIVERSAL::can on
AUTOLOADed subroutines.
I've always regarded it as a design error that you can't activate the
AUTOLOAD mechanism without actually calling the subroutine. But it's
probably a bit late to change that.
Mike Guy
------------------------------
Date: 7 Aug 1998 13:10:38 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: variable indirection
Message-Id: <6qeuce$6ls$3@csnews.cs.colorado.edu>
In comp.lang.perl.misc, I wrote:
: my (%names;
my %names;
: $names{"a"} = 'good';
Well, that could be single-quotes around the 'a'
for the sake of small hobgoblins.
: my $b = 'a';
: print $var{$b};
print $names{$b};
--tom
--
Chip Salzenberg sent me a complete patch to add System V IPC (msg, sem and
shm calls), so I added them. If that bothers you, you can always undefine
them in config.sh. :-) --Larry Wall in <9384@jpl-devvax.JPL.NASA.GOV>
------------------------------
Date: 12 Jul 98 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Special: Digest Administrivia (Last modified: 12 Mar 98)
Message-Id: <null>
Administrivia:
Special notice: in a few days, the new group comp.lang.perl.moderated
should be formed. I would rather not support two different groups, and I
know of no other plans to create a digested moderated group. This leaves
me with two options: 1) keep on with this group 2) change to the
moderated one.
If you have opinions on this, send them to
perl-users-request@ruby.oce.orst.edu.
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.
To submit articles to comp.lang.perl.misc (and this Digest), send your
article to perl-users@ruby.oce.orst.edu.
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.
The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.
The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.
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 V8 Issue 3388
**************************************