Download Wiley Curl Programming Bible
Transcript
b4942-1 Part01.F 4/29/02 9:01 AM Page 1 P Curl Content Language Fundamentals A R T I ✦ ✦ ✦ ✦ In This Part Chapter 1 Introducing Curl Chapter 2 How Curl Works Chapter 3 Building Your First Curl Applet with the SurgeTM Lab IDE Chapter 4 Working with Text Chapter 5 Building Tables Chapter 6 Adding Images, Backgrounds, and Borders Chapter 7 Working with Variables Chapter 8 Controlling Program Flow Chapter 9 Creating and Using Procedures Chapter 10 Creating Classes Chapter 11 Inheritance Chapter 12 Strings and Arrays Chapter 13 Handling Exceptions in Curl ✦ ✦ ✦ ✦ b4942-1 Part01.F 4/29/02 9:01 AM Page 2 c4942-1 ch01.F 4/29/02 9:01 AM Page 3 Introducing Curl T he 13 chapters in Part I provide basic information about Curl, its architecture, and its fundamental programming constructs. This chapter explains what Curl is, what businesses would benefit from using it, and how to phase it into your development process. Successive chapters discuss Curl’s architecture, the development tools, plus the usage of text, images, variables, control structures, procedures, and classes. Part II, Chapters 14–18, discusses how to build a user interface with Curl. Part III, Chapters 19–22, discusses Curl’s graphics and multimedia capabilities. Part IV, Chapters 23–25, discusses files and server communication. Part V, Chapters 26–28, discusses deploying applications. Part VI, Chapters 29–30, discusses XML and Web services. 1 C H A P T E R ✦ Curl Corporation provides the Surge plug-in and the Surge Lab IDE, for viewing and creating content written in the Curl Content Language. Users need the Surge plug-in to view Curl content in their browser. Authors will also need the Surge Lab IDE to create Curl content. The terms Curl Surge plug-in, Surge plug-in, and plug-in are used interchangeably, as are the terms Curl Content language, and just Curl. Curl, Surge, and Surge Lab are trademarks of Curl Corporation. This chapter is an overview of each component. Successive chapters cover each component in detail. Curl content language Curl combines the worlds of document markup, scripting, and object-oriented programming into a single language. Curl sprung from MIT’s famed Laboratory for Computer Science and later was spun off into Curl Corporation (www.curl.com). ✦ ✦ In This Chapter Understanding Curl technology Learning when to use Curl Integrating Curl into your development process ✦ What Is Curl Technology? ✦ ✦ ✦ ✦ c4942-1 ch01.F 4 4/29/02 9:01 AM Page 4 Part I ✦ Curl Content Language Fundamentals Curl was specifically designed to have a “gentle slope” learning curve. This means that no matter what your current knowledge of Curl, learning the next level of functionality is only slightly harder. At one end of the learning curve, Curl is a simple markup language that you can use in place of HTML. Authors can learn this aspect of Curl in a few minutes. Sliding up the curve a notch, you can create simple interface elements such as buttons and drop-down menus, with variables and local data validation. At the higher end of the learning curve, you can have object-oriented client/server applications with 3D graphics and other advanced tools. Borrowing from Dylan, an object-oriented language, Curl combines the flexibility of dynamic languages (Smalltalk, Scheme) with the speed of static-typed languages (C++, Eiffel). This combination also provides a gentle slope for development styles. When prototyping, Curl’s support of typeless variables and class variables provides flexibility and speed of development. For commercial code, you can add more static typing to improve correctness and execution speed. Surge Lab IDE The Surge Lab provides all the tools an author or developer needs to create Curl content. In theory, Curl content could be written in any text editor, but the Surge Lab provides much more functionality and it’s free. The Surge Lab includes its own syntax-aware Source Editor, API Viewer, Debugger, Object Inspector, and Release Tool for packaging Curl code. The Lab also allows you to run applets locally without needing a Web server. See Chapter 3 for a tutorial on the Surge Lab. Surge plug-in The Surge plug-in provides a sophisticated runtime environment for running Curl code securely. The plug-in includes a just-in-time (JIT) compiler and a unique security model that protects the user’s computer from the issues that have plagued mobile code in the past. Developers can affect the executable code by sending directives to the JIT compiler. These directives can control the trade-offs between the freedom of typeless programming versus code-safety and speed. The Surge plug-in also permits a single user to compile and run multiple versions of Curl. If the applet needs a newer version of Curl, the plug-in gives the user the option of downloading the newer module. This solves the thorny problem of backward compatibility that haunts many other plug-ins. Finally, the plug-in acts as the traffic cop that enforces many of Curl’s security features. These include ✦ Verifying an applet’s right to include or import Curl files from the host and comparing package versions to ensure compatibility c4942-1 ch01.F 4/29/02 9:01 AM Page 5 Chapter 1 ✦ Introducing Curl ✦ Preventing unprivileged applets from accessing local files ✦ Protecting intellectual property by prohibiting users from viewing the Curl source code See Chapter 2 for more details on Curl’s security features. Surge Control Panel The Surge Control Panel allows the user to control runtime options for security, debugging, and general preferences. This feature is most frequently used to allow applets from a particular host or directory to run with privileged access. Console window The Console window shows system warning and error messages. Also, any messages using the output command (normally, only used while debugging) appear in the Console window. Platform Independence Curl has been explicitly designed as a cross-platform language, suitable for reaching a wide audience across the Web. Surge only accepts Curl source code, either raw or compressed and preprocessed in .pcurl files, and compiles each applet on the target machine before running it. This local compilation, rather than reliance on externally produced executables or intermediate byte-codes, permits true platform independence. By compiling Curl on the client machine, Surge always knows the exact run-time environment the applet will face when executing. This allows Surge to take advantage of local machine resources, such as hardware graphics rendering, when possible, but does not depend on them. This late-binding aspect of the Curl/Surge architecture yields better platform independence and produces faster executables. Curl Corporation’s business approach to platform independence is equally crucial. They are creating the Surge run time for each platform themselves. This allows Curl Corporation to ensure that Surge works uniformly across all platforms and to avoid the Java debacle of “write once, debug everywhere.” Surge currently works on the vast majority of client machines, which is to say machines that run Microsoft Windows. Curl Corporation plans to make Surge available on Linux and Macintosh computers; eventually, it will be on PDAs and cell phones. Visit www.curl.com for updates on platform availability. Interoperability You don’t have to throw out your current Web site to use Curl. Curl content can be used alongside, or in place of, HTML. Curl can interoperate with other tools such as ASP, CGI scripts, and other development tools. 5 c4942-1 ch01.F 6 4/29/02 9:01 AM Page 6 Part I ✦ Curl Content Language Fundamentals Curl comes with the SAX (Simple API for XML) parser for XML documents. This means that Curl speaks XML as a native language, which makes Curl applets excellent front ends for XML data. This makes Curl an ideal user interface for the growing body of SOAP/Web services. See Chapter 29 for details on using SAX with Curl. See the CurlExamples site (www.curlexamples.com) for specific examples of working with other tools. Also, see the “Fitting into Your Infrastructure” section, later in this chapter, for tips on applying Curl to your current situation. Types of Curl Technology Solutions From a technical point of view, nearly any client-side application can be improved by using Curl. However, there are some significant usability and business considerations. Usability Curl’s interactive features and local response times generally make applications significantly easier and faster to use. This improved usability can give your application an edge against the competition. It may even permit you to charge premium pricing (until your competitor adopts Curl). Improved usability is one of the key reasons companies use Curl. As with any plug-in, however, there are the issues of startup time and installation that must be considered. In the 1.0 release, it can take 20 or more seconds, depending on your computer, to launch the Surge plug-in the first time (of course, you can always configure the plug-in to launch on computer startup). Furthermore, each user needs to have downloaded and installed the Surge plug-in, which takes a few minutes, depending on your connection speed. As Curl/Surge becomes more ubiquitous and Surge launch times decrease (the 2.0 version of the plug-in, due out in the summer of 2002, is supposed to launch 5 times faster than the current version), these will not be issues. For now, the Surge launch time needs to be considered as part of the application’s usability. Public sites that expect traffic from a wide range of users would probably not want to put Curl content on their home page, for example. Business models Before discussing when to use Curl, it is useful to segment the types of application in use today. There are two basic kinds of revenue-producing methods on the Web: transactions fees (eBay, Amazon, Dell) and subscription fees (Wall Street Journal, Communispace). c4942-1 ch01.F 4/29/02 9:01 AM Page 7 Chapter 1 ✦ Introducing Curl Things are never so neatly defined in practice, of course. A “free” site is essentially a subscription site with a fee of zero or with a marketing area designed to sell subscriptions or transactions; and an advertising site is essentially a transaction-fee site, except that the transactions that yield money are advertising impressions to the user. Sites can be further categorized by whether they allow public access or private access. These two dimensions segment Web applications into four quadrants shown in Table 1-1. The sections below discuss how each relates to Curl. Table 1-1 Business Models Transaction Revenue Subscription Revenue Public Users Amazon, eBay, Dell, etc. Wall Street Journal, Yahoo!*, etc. Private Users Fidelity, E*Trade, etc. Communispace, Paychex, Intranets*, etc. *Note that “free” sites, such as Yahoo!, or a corporate intranet, can be thought of as subscription sites with a fee of zero. Free corporate sites are marketing areas designed to sell subscriptions or transactions (offline or online). Public transaction sites Public transaction sites include such Web poster children as Amazon, eBay, and Dell. These sites are open to any Web user and make money when a user completes a transaction. From a technical and operational viewpoint, this category stands to gain the most from a Curl solution. Curl could save these high-traffic sites tremendous amounts of money in reduced server and bandwidth costs. Unfortunately, downloading the Surge plug-in presents the biggest challenge to public transaction sites. With so many new users each day, they simply can’t insure that the majority of users will have Surge installed on their computers. Short term, these sites may want to start a small Curl project and create a members-only segment of their site with a premium interface. Repeat users would be enticed to download Surge to gain access to more sophisticated features. In effect, this converts a segment of these sites to private transaction sites for frequent users. Every user they entice to use the Curl application saves them money. Equally important, as Surge becomes more prevalent, they will already have a Curl application waiting. They can maintain their market leadership while making a smooth transition. Public subscription sites Public subscription sites include paid sites, such as the Wall Street Journal online, and sites such as Yahoo! (Think of it as a free subscription.) Again, technically and operationally these sites would benefit tremendously from Curl. 7 c4942-1 ch01.F 8 4/29/02 9:01 AM Page 8 Part I ✦ Curl Content Language Fundamentals For usability, the more the subscription costs, the more likely the user will be interested in Curl. When the information is free and often available in multiple places, the user’s primary concern is usually saving time. Launching Surge slows them down. When the information is expensive, the user is more likely to be concerned about finding the best information. This is where the improved response time and usability of a Curl application is worth the time it takes to launch Surge. Free sites will probably have to wait until Surge is more readily available or until they can create a members-only area as discussed previously. Private transaction sites Private transaction sites include financial sites such as Fidelity, E*Trade, and home banking. Although these companies have a public component to their Web site, this category refers to the features provided for registered users. Here, users can examine their account, request information, and move money around or make other transactions. This is an excellent setting for Curl. Aside from the usual technical and operational benefits, most users are repeat users and would be happy to have a richer interface. Private subscription sites Private subscription sites include application service providers (ASPs) such as Communispace and Paychex. In these settings, users pay a flat fee to rent the ASP’s online application. Because users have already paid for the service, they are highly motivated to get the best use from the site. Installing Surge is easier since the users are known in advance. Since ASP providers have a fixed revenue stream each month, any reduction in cost is pure profit for them. This gives ASPs a strong incentive to use Curl. Corporate intranets fit into this category as well. Again, think of them as a subscription site that charges the flat fee of zero each month. This is further incentive to keep the cost down and the user productivity high because any wasted user time is wasting your own company’s money. Private subscription sites are the ideal business and technical situation to use Curl. Summary of Web business uses Web businesses fall into four main categories. Technically and operationally, all four categories could benefit from using Curl today. The viability of using Curl for your business depends on how prevalent Surge is among your users. Sites with private users (ASP services, intranets, financial services, and so on) are perfect for using Curl today. Public subscription sites, particularly those that charge premium prices, are also good Curl candidates. Public transaction sites, such as Amazon, would be better using Curl in a members-only capacity until Surge c4942-1 ch01.F 4/29/02 9:01 AM Page 9 Chapter 1 ✦ Introducing Curl is more ingrained with the public. Once converted to Curl, however, public transaction sites may have the biggest cost saving. Fitting into Your Infrastructure Adding any new tool to your arsenal requires both business and technical planning. As this is a technical book, most of the chapters focus on technical learning and planning. However, we start by discussing business planning here since it often sets the parameters and goals for technical planning. Business planning The gentle slope aspect of learning Curl makes it relatively easy to add it into your current development environment. Naturally, you will want to weigh the costs and benefits of using Curl. Many of the benefits of Curl were already covered in this book’s introduction, but a quick summary would be that Curl allows your applications to ✦ Provide a richer interface. ✦ Provide faster response time for your users in many cases. ✦ Provide better security for your applications. The idea of a richer interface merits a brief elaboration. Curl permits direct manipulation of the interface by your users, enabling them, for example, to resize tables, sort data, use configurators, and rotate graphics. This creates stickier application sessions. On the cost side, Curl has many advantages. In the heady dot-com boom days of the late 1990s, revenue was king and nobody cared about the cost, let alone profits. Those days are gone, gone, gone. Back in the real world, cost is a crucial aspect of any business’s profitability. When examining the cost of switching to a new technology, you would normally consider ✦ Cost of hiring and training Curl developers ✦ Cost of supporting dual development environments during transition ✦ Cost of Curl development tools ✦ Cost in time and dollars to develop and code projects in Curl ✦ Cost in time and dollars to test Curl applications ✦ Cost of deploying and operating a Curl application ✦ Cost of maintenance to fix problems and extend a Curl application 9 c4942-1 ch01.F 10 4/29/02 9:01 AM Page 10 Part I ✦ Curl Content Language Fundamentals The following sections examine each of these costs in more detail. Hiring and training The big win with Curl is that your development staff can focus on becoming expert in one client-side language rather than average in many languages. The cost of teaching developers Curl might be offset by not having to train each person in multiple languages. Curl is relatively easy to learn, so you won’t need to hire Curl-only developers. Anyone can quickly learn Curl’s text formatting features; HTML experience is not required. To use the programmatic aspects of Curl, experience with any objectoriented language (Java, C++, Eiffel, Smalltalk, Dylan) would suffice. Curl’s gentle slope learning curve means that whatever a person’s current experience with Curl, learning the next feature is only slightly harder. This is a remarkable accomplishment as compared to other languages, such as C++, that require huge time investments before one can become proficient. In addition to books like this, developers can learn Curl from the resources listed in “Finding Curl Information,” later in this chapter. Curl Corporation also offers handson training. Dual environment costs Often, switching to new tools comes with the hidden cost of maintaining your existing system while developing the new one. This means that, during the transition, you need to have staff expertise with many tools. You may even need to have separate hosting environments. Since Curl can usually be phased into your existing architecture, there is rarely a need to support dual development environments. (An exception might be public transaction sites.) Thus, this cost is often zero, or very low. “Technical planning,” later in this chapter, explains why this is true. Development costs Using Curl, development times are similar to — or faster than — normal, once you have trained developers. Partly, this is because Curl is a powerful language and partly this is because your developers spend less time working around the quirks of using multiple languages. Figure 1-1 shows how fast complexity grows when there are more tools involved. The graph in Figure 1-1 shows that accounting for the interactions between two tools is 10 times easier than having five tools and 21 times easier than having seven tools. For enhancements and bug fixes, having a uniform language makes it easier for one developer to modify another developer’s code. Plus, Curl development tools are free. 4/29/02 9:01 AM Page 11 Chapter 1 ✦ Introducing Curl 50 45 40 Number of Interactions c4942-1 ch01.F 35 30 25 20 15 10 5 0 1 2 3 4 5 6 7 Number of Tools 8 9 10 Figure 1-1: How more tools increase development complexity Another benefit is that Curl-based architectures are an extension of the well-studied client/server architectures that have been around for many years. This means that you are not on the bleeding edge of system architecture. Instead, you can benefit from years of industry experience in this area, further reducing risk on your project. Testing costs Testing current Web applications is a combinatorial mess. Figure 1-1 applies to testers as well as to developers. If testers tried to verify a complex application for every combination of operating system manufacturer and version, browser manufacturer and version, JVM version, JavaScript version, and so on, they would never finish. This is not slighting testers, but recognition that testing is an economic choice; testing each combination produces diminishing returns. How much are you willing to spend to be sure? How long is the market willing to wait for your product? What is the cost if you are wrong? With Curl applications, the major browser combinations were already tested by Curl Corporation when it tested Surge. Also, many Curl customers have gone before you. Yes, it’s a nightmare, but it’s their nightmare. Since Curl version heralds ensure your applet uses the correct version of Surge, all you need to do is test that your application works with a specific version of Surge. You can spend more time testing your application’s features and less on platform combinations. Operational costs It is generally cheaper to run Curl applications than applications based on other Web technologies. However, your saving will depend on how many of the following bullets apply to your site. Does your Web site: 11 c4942-1 ch01.F 12 4/29/02 9:01 AM Page 12 Part I ✦ Curl Content Language Fundamentals ✦ Deliver large pages that may include rich content? ✦ Do data-validation on the server? ✦ Dynamically create pages from templates on the server? ✦ Deliver pages that interact locally with the user (e.g., JavaScript)? ✦ Provide multiple views of the same data? ✦ Require large amounts of bandwidth to satisfy user demand? ✦ Require multiple servers to handle user load? ✦ Make money via subscription or transaction fees? The more yes answers you had to these questions, the more money Curl is likely to save you. Typically, for complex applications, Curl drastically reduces the amount of data being served from your hosts and uses less CPU time. This translates directly into lower monthly hosting and bandwidth costs. If you answered no to all of those questions, you probably have a simple online brochure or business card site with mostly static content. If so, then Curl may not be the best choice for your site. If your site is already small enough that you are using a single host with standard bandwidth, you will not save money on hosting or bandwidth. Then again, it’s possible you might be small enough to not trigger Curl Corporation’s billing threshold at all, in which case Curl would be free. Technical planning This section focuses on how best to phase Curl into your existing architecture. It is tempting to start with the minimal disruption to your development process and simply start by recoding your HTML pages into Curl. While this has simplicity on its side, it might not benefit you much, which could incorrectly give a negative first impression of Curl. Because, in the initial version of Curl, it can take a number of seconds to launch Surge, you want to make sure your users get a tangible benefit whenever they launch it. It generally would not make sense, for example, to have a Curl home page unless you expected your visitors to use your Curl-based features. Where does it hurt? To decide where to start, you should first identify the areas where Curl could provide the greatest benefits. Benefits come in many flavors: faster response times, richer features, cost savings, and so on. Naturally, you will have to decide which goal(s) to pursue, but as a guide you should gather the following information about your site: ✦ Which pages do users view the most? ✦ Which pages are the largest to download? ✦ Which features require the most CPU time on your servers? c4942-1 ch01.F 4/29/02 9:01 AM Page 13 Chapter 1 ✦ Introducing Curl ✦ Which features require the most disk accesses on your servers? ✦ Which features are currently most awkward for your users? ✦ Which pages have the most local user interaction? ✦ Which use cases require the most round-trips to the server? The answers create a “pain list” of what “hurts” most about each aspect of your site. Often, users are more elated by an improvement in a current feature than by the addition of a new one. (Note that Marketing usually feels the opposite way.) Users already expect new features to be perfect, so if you deliver a great new feature, they will be only modestly impressed. Users are more apt to be impressed when you fix a daily frustration. Existing features also have the advantage of being well understood, so you are less likely to stall on endless brainstorming aiming at perfection. Pages or features that appear on multiple pain lists should be strongly considered for your first foray into Curl. Analyzing the pain Now that you’ve identified the painful aspect of your site, you need to decide if Curl can help. If you have high-bandwidth pages, can Curl reduce them by using clientstored templates that only get loaded once? Where your server is using CPU time, could Curl use the client’s CPU? If your server accesses its disk to store user-state data, could Curl store the state information on the client? If you have multiple servers, what would it take to eliminate one of them? Most systems have a specific bottleneck that causes problems. Curl should be used to address one of the bottlenecks, if possible. Solve an easy one This does not mean that you should find the hardest problem you can find and tackle it first. The road of technology adoption is littered with roadkill from teams that overpromised and fell short. If your first attempt at Curl is a failure, you may not get a second chance. Instead, you should take the pain list and estimate how easy or hard it would be to solve the problem using Curl. Choose something that you think is easy or mediumhard for your first project. If everything on your pain list is hard to solve in Curl, pick a less critical feature even if it’s not on the list. Choosing a hard project for your first usage of any new tool is begging for trouble. The first project is always harder than it appears. Consider the longer term Since Curl can incrementally replace parts of your site, update the pain list periodically to help pick future priorities. Keep in mind that the notion of solving a current problem was only intended to cover your first use of Curl. After you are comfortable with Curl, new features or enhancements should be considered as well. 13 c4942-1 ch01.F 14 4/29/02 9:01 AM Page 14 Part I ✦ Curl Content Language Fundamentals Finding Curl Information As Curl has expanded into daily use around the world, various resources have emerged to help you understand Curl further. Below is a list of resources that can help you expand your knowledge of Curl. Curl Corporation First and foremost, you can get definitive information at the Curl Corporation Web sites. It you want it straight from the horse’s mouth, here is where to go. General information See www.curl.com for general information about Curl Corporation. This includes product information, press releases, Curl demos, and other general information about Curl Corporation. Developer information See www.curl.com/html/developers/developers.jspto connect directly to the Curl Developer section of the site. The Curl Developer area contains FAQs, tutorials, documentation, and discussion forums. The discussion forums are particularly helpful now that Internet newsgroups have devolved into spam. It is not unusual to get a response directly from Curl’s internal development team. curlBreaker.com curlBreaker.com is an independent organization that is committed to being “the place to go for Curl Content Language news, tips, etc.” They offer several different resources for Curl developers. Web magazine The curlBreaker.com Web magazine is an independent ‘zine for Curl developers. The magazine covers news from the Curl world with feature articles on various Curl topics. This is a great place to go to get independent news and analysis on Curlrelated topics. See www.curlBreaker.com for details. Developer listserv For Curl questions and discussions, curlBreaker.com also sponsors an e-mail listserv. This is a great way to rapidly push questions to a wide audience of Curl developers. Many Curl Corporation developers belong to this list and are frequent contributors. See www.curlBreaker.com/curlBreaker-l/devlist.html for more information. c4942-1 ch01.F 4/29/02 9:01 AM Page 15 Chapter 1 ✦ Introducing Curl Open source curlBreaker.com serves as the focal point for various open source projects using Curl. Open source project teams are always looking for interested developers. This is a great way to learn Curl and connect with other developers. Projects such as curlMail, a completely cross-platform e-mail client, and others can be found at www.curlBreaker.com/opensource/. CurlExamples CurlExamples is an independent community site for sharing, learning, and understanding Curl technology. As the name implies, this site is a treasure trove of small working examples that show off various aspects of Curl. Examples are searchable by title and categorized in many useful ways such as most recent, most viewed, and top rated. There is also a hierarchical list of topics such as Business, Games, Utilities, and so on. If you learn by example, this is the site for you. See www.curlexamples.com for more information. Open Directory Project The Open Directory Project, also known as DMOZ (Directory Mozilla), contains links to various Curl-related sites. In addition to listing the above sites, DMOZ also has a list of sites that use Curl. Since the DMOZ directory is always being updated, it’s a good idea to check back periodically to see what’s new. See DMOZ at dmoz.org/Computers/Programming/Languages/Curl/. Summary Curl permits a fundamental change in the way we think about Web applications, while still catering to the business realities of fitting in with current practices. Curl is an excellent choice today for private-user systems and might be a good choice for segments of public-user systems. Currently, designers should give particular thought to how the user’s launch of Surge fits into their application’s usability. As the installed base of the Surge plug-in increases, good uses for Curl will increase as well. Curl can phase into your current development process, making it a smooth, lowrisk, transition. The simplest implementations of Curl, while tempting, may not provide large enough benefits to justify the transition costs. You will be better off if you first create a pain list of potential improvements and compare them against the benefits of solving them in Curl. Then you can choose a relatively easy — but worthy — task for your first Curl project. Curl has become increasingly popular and many resources have sprung up, inside and outside of Curl Corporation, to help you succeed as a Curl developer. ✦ ✦ ✦ 15