C Traps
Cerebral C programmers should take note of the following:
Curlies are required for
ifandwhileblocks.You must use
elsifrather than “else if” or “elif”. Syntax like this:if (expression) { block; } else if (another_expression) { # WRONG another_block; }is illegal. The
elsepart is always a block, and a nakedifis not a block. You mustn’t expect Perl to be exactly the same as C. What you want instead is:if (expression) { block; } elsif (another_expression) { another_block; }Note also that “elif” is “file” spelled backward. Only Algol-ers would want a keyword that was the same as another word spelled backward.
The
breakandcontinuekeywords from C become in Perllastandnext, respectively. Unlike in C, these do not work within ado {} whileconstruct.For a long time Perl had no equivalent to C’s
switchstatement. Perl v5.10 introduced a “switch on steroids” with the fancy namegiven–when(since it’s a fancier construct). See Chapter 4. It’s easy to build your own, too; see and in Chapter 4.Variables begin with
$,@, or%in Perl.Comments begin with
#, not/*. Use pod for multiline comments.You can’t take the address of anything, although a similar operator in Perl is the backslash, which creates a reference. You get something that looks like an address when you stringify a reference, but you can’t really use it for anything.
ARGVmust be capitalized.$ARGV[0]is C’sargv[1], and C’sargv[0]ends up in$0. See Chapter 25.Syscalls such as
link,unlink, andrenamereturn true ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access