Page 76      All Pages  All Books
use English; # Import the module file called English.pm
# Try deleting a non-existent file
unlink ('/tmp/foo');
if ($ERRNO) { # Use $ERRNO instead of $!
print $ERRNO, "\n"; # Prints "No such file or directory" }
(We'll cover packages and the use statement in Chapter 6.) I think these well-known names should have been in there from the very beginning, instead of having to memorize funny-looking variables and an accompanying list of mnemonics. Some argue that you can use the same scheme for other languages ("use Dutch;"), but considering that the other system calls are in English anyway, I think that there's no point providing specific aliases for a small subset of the things you have to remember.
3.2.3 Aliasing Problem: Variable Suicide
Aliases, combined with the fact that local doesn't really create new variables (it temporarily slaps a new value onto a global variable), often leads to weird values of variables that presumably haven't even been touched. Consider the following simple case:
$x = 10; foo(*x); sub foo {
local(*y) = @_;
print "Before value of y : $y \n";
local($x) = 100;
print "After value of y : $y \n"; }
This prints the following:
Before value of y : 10 After value of y : 100
Can you resolve the mystery? Clearly, $y has not been touched between the two print statements, but its value seems to have changed. Hint: it reflects that of $x.
Let's trace the events in sequence: $x = 10;
local *y = *x; print "before value"
# Assign a value to global $x
# function called
# Save global *y's values. Alias it to *x
# Because of the alias, $y is the same as $x,
# hence this prints 10
# IMPORTANT: local saves $x's value (10)
# and substitutes 100. Note that it does not
# create a new $x variable
# replaced by 100
# But *y is still aliased to *x. Therefore,
# $y now contains 100
local $x = 100;
print "after value";

Page 76      All Pages  All Books