精华内容
下载资源
问答
  • 英语use to的使用

    2019-04-06 15:05:58
    2、be used to doing sth./be used to sth. 是指习惯于做某事(这里的to是介词) 3、be used to do sth. 这是一个被动语态,表示被用来做某事。对应的主动语态是:use sth. to do sth. 参考链接 ...

    1、used to do sth.

    表示过去常常做某事(现在不做了)

    2、be used to doing sth./be used to sth.

    是指习惯于做某事(这里的to是介词)

    3、be used to do sth.

    这是一个被动语态,表示被用来做某事。对应的主动语态是:use sth. to do sth.

    参考链接

    展开全文
  • “After five years of use and modification, the source for a successful software program is often completely unrecognizable from its original form, while a successful building after five years is ...
     
    
    11.07.2014
      |  8493 VIEWS  | 
    • src="http://widgets.dzone.com/links/widgets/zoneit.html?t=1&url=http%3A%2F%2Fjava.dzone.com%2Farticles%2Fdont-waste-time-writing&title=Don%27t%20Waste%20Time%20Writing%20Perfect%20Code" height="70" width="50" scrolling="no" frameborder="0" style="margin: 0px; padding: 0px; border-width: 0px; outline: 0px; font-size: 15px; vertical-align: baseline;">
    • src="http://www.facebook.com/plugins/like.php?href=http://java.dzone.com/articles/dont-waste-time-writing&send=false&layout=box_count&width=100&show_faces=false&action=like&colorscheme=light&font&height=60" scrolling="no" frameborder="0" allowtransparency="true" style="margin: 0px; padding: 0px; border-style: none; outline: 0px; font-size: 15px; vertical-align: baseline; width: 58px; height: 62px;">
    • 61
      Share
    • scrolling="no" frameborder="0" allowtransparency="true" src="http://badge.stumbleupon.com/badge/embed/5/?url=http%3A%2F%2Fjava.dzone.com%2Farticles%2Fdont-waste-time-writing" width="50" height="60" id="iframe-stmblpn-widget-1" style="margin: 0px; padding: 0px; border-width: 0px; outline: 0px; font-size: 15px; vertical-align: baseline; overflow: hidden;">
    • submit to reddit

    A system can last for 5 or 10 or even 20 or more years. But the life of specific lines of code, even of designs, is often much shorter: months or days or even minutes when you’re iterating through different approaches to a solution.

    Some code matters more than other code

    Researching how code changes over time, Michael Feathers has identified a power curve in code bases. Every system has code, often a lot of it, that is written once and is never changed. But a small amount of code, including the code that is most important and useful, is changed over and over again, refactored or rewritten from scratch several times.

    As you get more experience with a system, or with a problem domain or an architectural approach, it should get easier to know and to predict what code will change all the time, and what code will never change: what code matters, and what code doesn’t.

    Should we try to write Perfect Code?

    We know that we should write clean code, code that is consistent, obvious and as simple as possible.

    Some people take this to extremes, and push themselves to write code that is as beautiful and elegant and as close to perfect as they can get, obsessively refactoring and agonizing over each detail.

    But if code is only going to be written once and never changed, or at the other extreme if it is changing all the time, isn’t writing perfect code as wasteful and unnecessary (and impossible to achieve) as trying to write perfect requirements or trying to come up with a perfect design upfront?

    You Can't Write Perfect Software. Did that hurt? It shouldn't. Accept it as an axiom of life. Embrace it. Celebrate it. Because perfect software doesn't exist. No one in the brief history of computing has ever written a piece of perfect software. It's unlikely that you'll be the first. And unless you accept this as a fact, you'll end up wasting time and energy chasing an impossible dream.” 
    Andrew Hunt,  The Pragmatic Programmer: from Journeyman to Master

    Code that is written once doesn’t need to be beautiful and elegant. It has to be correct. It has to be understandable – because code that is never changed may still be read many times over the life of the system. It doesn't have to be clean and tight – just clean enough. Copy and paste and other short cuts in this code can be allowed, at least up to a point. This is code that never needs to be polished. This is code that doesn't need to be refactored (until and unless you need to change it), even if other code around it is changing. This is code that isn't worth spending extra time on.

    What about the code that you are changing all of the time? Agonizing over style and coming up with the most elegant solution is a waste of time, because this code will probably be changed again, maybe even rewritten, in a few days or weeks. And so is obsessively refactoring code each time that you make a change, or refactoring code that you aren't changing because it could be better. Code can always be better. But that’s not important.

    What matters is: Does the code do what it is supposed to do – is it correct and usable and efficient? Can it handle errors and bad data without crashing – or at least fail safely? Is it easy to debug? Is it easy and safe to change? These aren't subjective aspects of beauty. These are practical measures that make the difference between success and failure.

    Pragmatic Coding and Refactoring

    The core idea of Lean Development is: don’t waste time on things that aren't important. This should inform how we write code, and how we refactor it, how we review it, how we test it.

    Only refactor what you need to, in order to get the job done - what Martin Fowler calls opportunistic refactoring (comprehension, cleanup, Boy Scout rule stuff) and preparatory refactoring. Enough to make a change easier and safer, and no more. If you’re not changing the code, it doesn't really matter what it looks like.

    In code reviews, focus only on what is important. Is the code correct? Is it defensive? Is it secure? Can you follow it? Is it safe to change?

    Forget about style (unless style gets in the way of understandability). Let your IDE take care of formatting. No arguments over whether the code could be “more OO”. It doesn’t matter if it properly follows this or that pattern as long as it makes sense. It doesn't matter if you like it or not. Whether you could have done it in a nicer way isn’t important – unless you’re teaching someone who is new to the platform and the language, and you’re expected to do some mentoring as part of code review.

    Write tests that matter. Tests that cover the main paths and the important exception cases. Tests that give you the most information and the most confidence with the least amount of work. Big fat tests, or small focused tests – it doesn't matter, and it doesn't matter if you write the tests before you write the code or after, as long as they do the job.

    It’s not (Just) About the Code

    The architectural and engineering metaphors have never been valid for software. We aren’t designing and building bridges or skyscrapers that will stay essentially the same for years or generations. We’re building something much more plastic and abstract, more ephemeral. Code is written to be changed – that is why it’s called “software”.

    “After five years of use and modification, the source for a successful software program is often completely unrecognizable from its original form, while a successful building after five years is virtually untouched.” 
    Kevin Tate,  Sustainable Software Development

    We need to look at code as a temporary artefact of our work:

    …we're led to fetishize code, sometimes in the face of more important things. Often we suffer under the illusion that the valuable thing produced in shipping a product is the code, when it might actually be an understanding of the problem domain, progress on design conundrums, or even customer feedback. 
    Dan Grover,  Code and Creative Destruction

    Iterative development teaches us to experiment and examine the results of our work – did we solve the problem, if we didn’t, what did we learn, how can we improve? The software that we are building is never done. Even if the design and the code are right, they may only be right for a while, until circumstances demand that they be changed again or replaced with something else that fits better.

    We need to write good code: code that is understandable, correct, safe and secure. We need to refactor and review it, and write good useful tests, all the while knowing that some of this code, or maybe all of it, could be thrown out soon, or that it may never be looked at again, or that it may not get used at all. We need to recognize that some of our work will necessarily be wasted, and optimize for this. Do what needs to be done, and no more. Don’t waste time trying to write perfect code.

    展开全文
  • 原标题:500个英语语法固定搭配as +adj./adv.+as 和…一样so … as 像…一样sb... 对…来说很熟悉prevent/defend …(sb) from doing 远离回复【语法400题】到平台deter/stop …(sb) from doing 远离discourage…from...

    原标题:500个英语语法固定搭配

    as +adj./adv.+as 和…一样

    so … as 像…一样

    sb. be familiar with sth. 熟悉

    sth. be familiar to sb. 对…来说很熟悉

    prevent/defend …(sb) from doing 远离回复【语法400题】到平台

    deter/stop …(sb) from doing 远离

    discourage…from doing 劝某人打消做某事的念头,阻止某人做某事…

    keep…from doing/sth. 让某人远离

    an appraisal of…. 对…的估价

    identify … as 把…认定为

    be identified as… 被认定为

    preoccupation with 全神贯注

    be preoccupied with 全神贯注

    on account of 因为

    an/the account of 对于…的解释/描述

    account for 对…进行解释

    count on 依赖; 依靠;期望; 指望

    be determined to do 决定做

    determination to do 决定做

    at a distance 远离

    long since = ever since 自那以后

    long since gone 自那以后

    long since forgotten 自那以后被遗忘

    be certified as 被认证为

    a means of 一种…的方式

    offer of+内容 提供…

    offer for+对象 为…提供

    offer to do 提出…

    offer sb sth. 为某人提供

    offer sth. to sb. 为某人提供

    help sb. with sth 帮助

    help sb. to do 帮助

    help sb. do 帮助

    inauguration of …的创立

    fight back to 反击

    differ/different/difference from 与…不同

    differ in 在某方面不同

    be no different than 与…没有不同

    indifferent to 对…冷漠

    gain the respect of 赢得尊重

    respect for 对…的尊重

    vote against 投票反对

    hope to do 希望

    hope for sth 希望

    in the hope of 希望

    in hopes of 希望

    have hope in/on sb 对…怀有希望

    be honored by…as… 被人尊为…

    be absorbed in 被…吸引

    use…to do/for doing 用…来做

    use…as… 把…用作

    make use of 利用

    keep sth do/done/adj 保持

    keep pace with 跟上…的脚步

    be necessary for 对…来说…很必要

    be necessary for sb to do 对…来说…很必要

    threaten to do 威胁着要…

    a threat to doing/sth./sb. 对…的威胁

    take pride in 以…为自豪

    apprehension about 恐惧

    be sympathetic to/toward 对…同情

    have sympathy for 对…同情

    be attributed to 归结于

    attribute/ascribe/impute to 把…归因于

    accessible to/have access to 接近,对…可获得

    inaccessible to 对…很难接近

    regard…as…(没有to be) 把…视为

    be regarded as 被视为

    migrate from…to… 从…移民到哪儿

    compound in 在…复杂

    protest sth. 呼吁

    protest against 与…抗争

    protest for 为…抗争

    insist on/upon sth./doing sth. 坚持

    insistent on/upon 坚持

    insistence on/upon 坚持

    the decline of …的下降

    a decline in 在…的下降

    continue doing/to do 继续

    react to sth. 对…的反应

    the reaction in 在某方面的反应

    the reaction to 对…的反应

    adapt sth to 使…适应

    adapt to do 适应去…

    be adapted to n/doing 适应/习惯于…

    be adaptable to sth. 适应于某事

    far from 远不是可回复【S34】到ETS-China

    far away from 远离

    be intolerable to sb. 对…来说不能忍

    have tolerance for 对…可忍耐、对…的耐力

    available to 对…可获得

    engage in sth./doing 热衷于,从事于

    be engaged by 被…吸引

    be engaged in 热衷于,从事

    make it +adj.+to do 使…变得…

    dress in 穿着

    have an understanding of 对…的理解

    discharge…from 释放,解雇

    at the expense/cost of 以…为代价

    fall into decline 开始衰落

    specialize in 以…为专攻

    be famous as 以…著称

    be famous for 以…著称

    rise to fame as 赢得…的名声

    can/cannot help but do 只能

    evoke…by doing 通过…唤起

    an advocate of …的支持者

    advocate doing 支持

    assure sb of sth=assure sb that SVO 使…确信

    ensure sth/that SVO 确保

    disposal of 对…的处理

    award 奖 to sb. for ?sth. 因为...给某人颁奖

    receive an award for 因为…得奖

    set a record 创纪录

    be surprised by 被…惊到了

    be helpful for sb to do 做某事对某人有帮助

    be helpful in doing 在某方面有帮助

    meet up with 会见

    meet with 偶遇,遭受,会见

    concern about 关心

    be concerned about/with 关心

    have confidence in 对…有信心

    be confident about 对…有信心

    in one’s zeal to do 热情地做…

    the same…as… 和…一样

    see sb. do/doing/done 看到某人…

    see…as 把…视为

    be free to do 自由地做…

    be free of 远离

    free of 没有

    be common in 在…一样

    encourage sb. to do 鼓励某人做某事

    go about 处理

    go in for 有天赋

    place sb on… 给某人定罪

    look down 低头,看不起

    provide evidence of 提供证据

    be isolated from 远离,孤立于

    recognize…as… 把…认作

    be recognized as 被认作

    distinguish…from 与…区分开

    be distinguished from 与…区分开

    be distinguished by 以…与众不同

    have trouble/problem/difficulty (in) ?doing 在…有困难

    ability in 在某方面有能力

    familiarize sb with 让某人熟悉

    struggle for/against 为了/反对…而斗争

    struggle to do 挣扎着去做

    consist of 包括

    consistence/ consistent/ inconsistent ?with 和…一直/不一致

    hesitate to do 犹豫

    commit … to n/doing 把…奉献给

    the commitment to 对…的奉献

    consider doing=think about doing 考虑做某事

    be considered (to be /as) 被认为

    get a raise for doing 因…而得到提升

    open door to 对…打开大门

    be misinformed about 对…误会,被误导

    be irreparable to 对…无法修复

    insofar as 在…范围之内

    be jammed with 被…阻塞挤满

    the praise for …的奖赏

    in praise of 为了表彰

    a native of 当地人

    be native to 原产于

    the demand for …的需求

    tempt sb to do 诱惑

    respond to 针对…回应

    in response to 针对

    be responsible for 对…负责任

    be responsive to 对…做出反应

    the responsibility to do 做…的责任

    interest in/have interest ?in/interested in 对…的兴趣

    decide to do 决定

    decide against doing sth 决定不做

    in an attempt to do 尝试做…

    the attempt (of sb) to do 尝试

    attempt to do 尝试

    be similar to 和…相似

    tend to do 倾向于

    a tendency to do 倾向于

    out of 出于…由于

    be bored at doing/ to do 对…厌倦

    dwell on 老是想着,详述

    condemn … as 谴责…是

    condemn … for 谴责…因为

    stock up 进货

    the desire to do 做…的欲望

    the desire for 对…的期待

    curiosity about 对…的好奇心

    cause sb to do 导致某人做某事

    lost cause 徒劳无功的事

    be suspicious of 怀疑

    be averse to 与…相反

    aversion to 厌恶

    be vulnerable to 易受…感染

    ignorant of 对…的无知

    be congenial to 意气相投的,适合的

    rebellion against 对…的反抗

    work to do 工作

    work as 以…谋生

    the standard for …的标准

    the design of …的设计

    be disappointed to do 失望地做…

    be disappointed by 对…失望

    to one's disappointment 令某人失望

    the season for doing sth 做…的季节

    be visible as 以…形式被看到

    be visible to 对…可视

    be effective in 在某方面有效

    have an effect on 对…有效果

    come across as 给人以…的印象

    be rejected as 因…被拒绝

    be acclaimed as 被称为

    urge sb to do 敦促某人做某事

    urge to do 主张

    have sb/sth done 让…被…

    have sb do 让某人做

    the basis for …的基础

    portray … as 把…描述为

    be subject to sth/doing 屈从于

    opportunity for sth/to do sth …的机会

    argue for 为…争辩

    participate in 参加

    succeed (in) doing 在…成功

    success in sth 成功

    succeed sb as 继承某人的身份

    mistrust of 对…的误解

    ambivalence about 对…的举棋不定

    force … to do 强迫…做某事

    contribute to sth/doing sth 贡献给…

    in favor of 赞成,支持

    be careless in 在某方面粗心

    call for 呼吁

    on the grounds of sth 因为

    on the grounds that SVO 因为

    now that +SVO 既然,由于

    in that +SVO 既然,由于

    since that +SVO 既然,由于

    enjoy doing 喜欢

    opposite to 相反,对立(一般不指人)

    opposition to 反对

    be particular about 对…挑剔

    be characteristic of 代表了

    be typical of 代表了

    be representative of 代表了

    run into 偶遇

    be exempt from 免于

    be racked with 受…折磨

    in an effort to do 努力做…

    be mindful of 对…介意

    on the verge of 在…的边缘

    on the brink of 在…的边缘

    on the edge of 在…的边缘

    allow sb to do 允许某人做…

    in vain 徒劳无功

    do one's best to do 全力以赴

    notify sb of 通知

    await sb=wait for sb 等待某人

    proceed with 继续做某事

    uncertainty about 对…的不确定

    superfluous for 对…来说过剩

    prohibitive for 对…禁止

    feasible for 对…来说可行

    irrelevant to 和…不相关

    provide sth for sb 为…提供…

    provide sb with sth 为…提供…

    be invaluable for 对…很重要

    resist doing sth 拒绝做…

    suffer from 遭受

    aim sth at sth/doing sth 把…瞄准,目标定在…

    be aimed at sth/doing sth 把…瞄准,目标定在…

    aim to do 目标是做…

    catch off guard 猝不及防

    an/the impact on/upon 对…的影响

    result in 导致

    result from 源自

    be difficult for sb 对…很难

    in preparation for 为…做准备

    preparation for 对…的准备

    exchange A for B with sb 和某人用A换B

    derive … from 从…提炼出

    be popular with sb 在…人中非常流行

    care for 关照,照顾

    care about 关心,关注

    recipe for …的制作方法

    have experience in 在…有经验

    be experienced in 在…经验丰富

    take sth for granted 认为…理所应当

    be busy with 忙于…

    live on sth 以…为食

    be rooted in 源于

    be attuned to 适应

    make room for 为…留空间

    major in 主修

    ability in 在某方面有能力

    able/ ability/ unable/ inability +to ?do …的能力

    capable/ capability/ incapable/ ?incapability of …的能力

    http://www.onebest.cn/show-87-3999-1.html

    capacity of 容量

    capacity for 能力

    stimulate sb. to do 激励某人做某事

    complain about 抱怨

    complaint about 对…的抱怨

    complain to sb. about/of sth. 向某人抱怨某事

    venture into 冒险去…

    concentrate on/upon 集中精力

    spend…(in) doing/on sth 为某事花费…

    revere sb for… 因为…尊重某人

    be revered as 作为…身份收人尊重

    be revered for 以…收人尊重

    be involved in 涉及…

    range from … to … 范围从…到…

    damage to sth 对…的影响

    on schedule 按时

    leave BJ 离开北京

    leave for BJ 出发去北京

    leave…to do 出发去做…

    leave A for B to do sth 离开A去B做

    introduction of/to/into sth. 对…的介绍

    culminate with 在…达到顶峰

    return to do 回去做某事

    from … to 从…到…(时间范围或变化)

    from … until 从…到…(只有时间)

    seek out 找出;物色;找到

    think it +adj +to do sth (it是形式宾语=to do)

    think of … as… 把…认为是…

    over the past 10 years (现在完成时)

    over 10 years later/ago (一般过去时)

    refer to 参照

    pertain to 属于

    be related to 与…相关

    the number of XX的数量

    a number of 很多

    be likely to do 有可能

    compare A with B 把A和B做对比

    compare A to B 把A比作B(也可作比较)

    (be)contrary to 与…相反

    in contrast=by contrast 后面都可以接with

    in comparison=by comparison 通过对比

    too + adj (+ a +n.)+to do 太…而不能

    talent/gift for 才能

    faculty of 才能

    be known to be=as 作为…而出名(身份)

    be known for/to do 因为…而出名(原因)

    ward off=avoid 避开,驱赶

    avoid doing sth. 避免

    lack sth 缺少

    lack (in) doing sth 缺少

    a lack of 缺少

    prefer A to B 比起B,更喜欢A

    prefer doing to doing 喜欢…多于…

    prefer to do 后面一般没有比较内容了

    A coincide with B 巧合发生

    associate A with B 把A与B相关联

    A is associated with B A和B相连

    on display 展出的

    be traced to 追溯到

    inform sb about/of 告知

    inform against 检举

    information about/on 关于…的信息

    at one's peril 后果自负

    do rather than do 而不是

    doing rather than doing 而不是

    to do rather than (to) do 而不是

    enrich sth with 用…丰富…

    better off 情况好转

    For all +n., SVO. 尽管…但是…

    depend/dependent/dependence + ?on/upon…for 依靠…为了…

    combination … with 把…和…结合

    the combination of A and B A和B结合

    the obsession with 对…的痴迷

    be obsessed with 迷恋于

    arrive in 到达

    arrive at 到达

    vary with 随着…而改变

    vary in 在…方面各不相同

    elaborate on 详细说明

    reside in 居住在

    at the risk of 冒着…的风险

    It takes/took/will take sb some ?time/money to do sth. to do sth花费某人…

    www.onebest.cn 沃邦教育官网

    require sb to do 要求某人做某事

    be required for 对…必需的

    require that sb (should) do sth 要求某人做某事

    elect sb + (position/identity) 中间不加to be或as

    call in sick 请病假

    have shortage in 在…缺少…

    in turn 依次,轮流地/相应地

    turn out to be 结果

    turn to … for 找到…为了…

    prize sth for 以…定价

    be prized for 因为…而被标价(看重)以…定价

    pose challenge to 对…提出挑战

    the challenge to sth 对…的挑战

    the reason for …的原因

    for some reason 因为某种原因

    be affiliated with 与…相关

    in time 及时

    on time 准时

    criticize sb for sth/doing sth 批评某人某事

    be criticized for 被批评因为…

    failure in 在…的失败

    the failure to do 失败

    be worried about 担心

    appear to be 看起来

    be impressed with 对…印象深刻

    the first person to do/ to be done 第一个…的人

    an expert at …在…的专家

    expertise in 在…的专业能力

    draw upon 利用/描述/依靠

    draw on 利用/穿上/渐渐来临/鼓励/依靠

    draw from 从…得到根据

    be drawn to 被…吸引

    whether … or not (没有whether … or ?else)

    impatient with 对…没有耐心

    be accustomed to 习惯于

    object to 反对

    make remark about 对…评论

    come in contact with 和…产生接触

    be sensitive to sth/ doing sth 对…敏感

    no sooner … than 一…就…

    gaze upon 看到,望见

    be disruptive to 对…是破坏性的

    be celebrated as 被歌颂为

    be made from 用…材料制成(看不出成分)

    take offence/offense at 对…感到愤怒

    in need of 需要

    the need for 对…的需求

    The more …, the more… . 越…越

    be deluged with 泛滥着…充斥着

    a deluge of 大量

    a way to do 做…的方式

    a way of doing 做…的方式

    launch into=enter into 进入到

    take refuge in 在…避难

    audition for 为…试镜

    love for 对…的热爱

    the love of 对…的喜爱

    agree to do 同意

    agree on 在某方面达成一致

    agree with sb 同意某人

    agreement to do/on sth 在…的一致

    legend has it that SVO. 有传说说…

    rumor has it that SVO. 有流言说…

    guard against (保卫)防止

    expect … to be 期待…是

    expect to do 期待…做

    apply to sth/apply…to sth. 适用于…/把…应用于

    apply to do sth 申请做某事

    apply for 申请

    apply to … for… 向…申请…

    bring about 引起

    protect … from 保护…远离

    be tailored to 订制的

    take the place of 取代

    limit sb to doing 把某人限制在只做某事

    find it adj +to do sth 发现做某事是…的

    set out to do sth 开始/着手做某事,打算

    enter … in 给…报名参加

    the plans for 对…的计划

    plan to do 计划

    base … on/ be based on/upon 以…为基础

    collaborate with 合作

    be engrossed in 沉迷在

    unique to 专属于

    be acquainted with 熟悉

    know of 了解

    be jealous of 对…嫉妒

    strive to do 奋斗

    be rich with 富含…,在…很多

    the quest for 对…的需求

    equate … with 把…和…等同

    function as 起… 作用

    solution to n/doing 对…的解决方案

    insulate against 绝缘

    wear out 精疲力尽

    be devoid of 没有…, 缺少…

    be inclined to do 倾向于

    be aware of 意识到

    be noted for 以…出名

    sway (away) from 动摇

    subsist on 以…为食

    be harmful to 对…有害

    describe ... as 把…描述得…

    take charge of 负责

    be distinct from 和…不同

    make sense to 对…有意义

    make sense of 理解…

    be recommended for 推荐给…

    be named after 以…命名

    watch sb do/doing 观看某人做某事

    tell of 讲述

    be comparable to 和…可以相比

    if not for 要不是

    人 win … for … 以…赢得…奖

    作品win 人奖 作品为人赢得…奖

    so far 迄今为止

    spot sb doing 看到某人做某事

    diatribe against 对…的诽谤

    meditation on 对…的思考

    rely on 以来

    finish doing 完成

    view … as 把…视为

    the remedy for 对…的治疗

    in recognition of 为了认可…表彰…

    present sb with … 给某人颁奖

    be worth doing 值得

    be worth sth 值得

    be worthy of 值得

    belong to 属于

    delay for 为…延迟

    convert sth. to sth.

    convert … into … 把…转化为

    be converted to/into 转化

    teem with 充满了

    devote … to sth./doing 把…奉献给

    be renowned for 以…著称

    be convinced of/that 被说服

    convince sb of sth 说服某人某事

    convince sb that SVO 说服某人某事

    remind sb of 提醒某人某事

    begin to do=begin doing 开始

    start to do=doing 开始…

    so … as to do 太…以致于

    so as to do 以便于

    enable sb to do 使某人做…

    a mass of 一坨

    combine with 合成

    up to sb 由某人决定

    resistance to 对…的抵抗

    be inured to 习惯于

    be repentant about 对…后悔

    be fractious about 对…愤怒

    expend … in doing 在…花费

    attest to sth 证明

    cope with 处理

    appeal to 对…有吸引力

    replace A with B 用B取代A

    be appreciative of 感激

    be proud of 自豪

    be hungry for 渴望

    share … with 和…分享

    stick to 粘贴

    be modeled after 按照…的模型制作

    mean to do 有意为之

    be meant to do 应该是

    mean doing 意味着

    neither…nor… 既不…也不

    either…or… …或…

    be grateful for doing 为…感谢

    今天的内容就先分享到这里,希望能对孩子的学习有所帮助!如果您的孩子有记忆力差,学习成绩提不高,学习方法不正确,严重偏科等这些问题,或者需要更多详细的学习资料(有PDF高清版本,可直接下载打印)都可以在微信上问我,我都会免费给各位家长一一解答。

    

    可在微信搜索栏上输入“ CN95224”,添加我的微信,免费查看更多关于教育、学习方法、提分技巧、培养学习兴趣的资料!帮助孩子找到适合的学习方法。老师也会不定期开设免费的公益课,家长可以让孩子在休息的时候来听听,一定会让孩子受益匪浅返回搜狐,查看更多

    责任编辑:

    展开全文
  • Perl 模块安装 参考: 各个平台下 Perl 模块安装总结 官网介绍 Error: Can’t create ‘/Library/Perl/...use strict; use warnings; sub main { print "Hello World!\n"; } main(); Tutorial3.pl use strict...

    Perl Notes from Udemy

    Perl 模块安装

    参考:
    各个平台下 Perl 模块安装总结
    官网介绍
    Error: Can’t create ‘/Library/Perl/5.18/App’:改用sudo

    Section 1: Basic Perl: Getting Started

    T2. Hello World

    use strict;
    use warnings;
    
    sub main {
    	print "Hello World!\n";
    }
    
    main();
    

    T3. Downloading Text and Images With Perl

    use strict;
    use warnings;
    
    use LWP::Simple;
    
    sub main {
    	
    	print "Downloading ...\n";
    	# Download the specified HTML page and print it to the console.
    	# print get("http://www.caveofprogramming.com/");
    	
    	# Download the Cave of Programming home page HTML and save it to "home.html"
    	# getstore("http://www.caveofprogramming.com/", "home.html");
    	
    	# Download logo.png from the Internet and store it in a file named "logo.png"
    	my $code = getstore('http://www.caveofprogramming.com/wp-content/themes/squiffy/images/logo.png', "logo.png");
    	
    	# Did we get code 200 (successful request) ?
    	if($code == 200) {
    		print "Success\n";
    	}
    	else {
    		print "Failed\n";
    	}
    	
    	print "Finished\n";
    }
    
    main();
    

    T4. Arrays and Checking Whether Files Exist

    use strict;
    use warnings;
    
    $|=1;
    
    sub main {
    
     	# An array of file names. Note the "@"" for array
    	my @files = (
    		'/Users//Desktop/Perl/Tutorial2.pl',
    		'/Users//Desktop/Perl/Tutorial3.pl',
    		'/Users//Desktop/Perl/Tutorial4.pl',
    	);
    
    	# foreach loops through the array, setting the loop
    	# variable ($file in this case) to each value in turn.
    	
    	foreach my $file (@files) {
    		
    		# -f tests if a file exists.
    		if ( -f $file ) {
    			print "Found file: $file\n";
    		}
    		else {
    			print "File not found: $file\n";
    		}
    	}
    
    }
    
    main();
    

    T5. Reading Files and Beginning Regular Expressions

    use strict;
    use warnings;
    
    # turn off output buffering
    $|=1;
    
    sub main {
    	my $file = '/Users//Desktop/Perl/egg.txt';
    	
    	# Either successfully open the file or else die (stop the program)
    	open(INPUT, $file) or die("Input file $file not found.\n");
    	
    	# Read each line from the file, one line at a time.
    	while(my $line = <INPUT>) {
    		if($line=~/egg/) {
    			print "$line\n";
    		}
    	}
    
    	close(INPUT);
    }
    
    main();
    

    T6. Writing Files and Replacing Text

    use strict;
    use warnings;
    
    $|=1;
    
    sub main {
    	
    	# open $input for reading
    	my $input = '/Users//Desktop/Perl/egg.txt';
    	open(INPUT, $input) or die("Input file $input not found.\n");
    	
    	# open $output for writing
    	my $output = 'output.txt';
    	open(OUTPUT, '>'.$output) or die "Can't create $output.\n";
    	
    	# Read the input file one line at a time.
    	while(my $line = <INPUT>) {
    		
    		# If this line has the word "egg" in it, write it
    		# to the output file, otherwise ignore it.
    		# \b matches the edges (boundaries) of words.
    		if($line =~ /\begg\b/) {
    			$line =~ s/\begg\b/dinosaur/ig; # i: case insensitive, g: global
    			print OUTPUT $line;
    		}
    	}
    
    	close(INPUT);
    	close(OUTPUT);
    }
    
    main();
    

    T7. Wildcards in Regular Expressions

    use strict;
    use warnings;
    
    $|=1;
    
    sub main {
    	my $file = 'C:\tutorial\perl\mymanjeeves.txt';
    	
    	open(INPUT, $file) or die("Input file $file not found.\n");
    	
    	while(my $line = <INPUT>) {
    		
    		# The dot matches any character, even space or punctuation
    		# e.g. the example below matches
    		# I was
    		# I said
    		# If an
    		# I take 
    		# etc.
    		# (only five characters including the space are actually matched by the epxression)
    		if($line =~ /I..a./) {
    			print $line;
    		}
    	}
    
    	close(INPUT);
    }
    
    main();
    

    T8. Groups: Finding Out What You Actually Matched

    use strict;
    use warnings;
    
    $|=1;
    
    sub main {
    	my $file = 'C:\tutorial\perl\mymanjeeves.txt';
    	
    	open(INPUT, $file) or die("Input file $file not found.\n");
    	
    	while(my $line = <INPUT>) {
    
    		# Surround the bits you want to "capture" with round brackets
    		if($line =~ /(I..a.)(...)/) {
    			# The stuff matched by the first set of round brackets is now in $1
    			# The stuff matched by the second set is in $2
    			print "First match: '$1'; second match:'$2'\n";
    		}
    	}
    
    	close(INPUT);
    }
    
    main();
    

    T9. Quantifiers: Greedy vs. Non-Greedy

    use strict;
    use warnings;
    
    $|=1;
    
    sub main {
    	my $file = 'C:\tutorial\perl\mymanjeeves.txt';
    	
    	open(INPUT, $file) or die("Input file $file not found.\n");
    	
    	while(my $line = <INPUT>) {
    		
    		# * matches zero or more of the preceding character; e.g.
    		# d* matches zero or more d's 7* zero or more 7's, etc.
    		# .* matches zero or more of any character, as many as possible
    		# .*? matches zero or more of any character, as few as possible
    		
    
    		if($line =~ /(s.*?n)/) {
    			print "$1\n";
    		}
    	}
    
    	close(INPUT);
    }
    
    main();
    

    If you want to match a ., remember to use \..

    T10. Escape Sequences

    use strict;
    use warnings;
    
    $|=1;
    
    sub main {
    	
    	# \d digit
    	# \s space
    	# \S non-space
    	# \w alphanumeric
    	
    	# Some examples; in the following examples,
    	# each example shows the text, the regular expression
    	# and the output, in that order.
    	
    	# Digits:
    	# 'I am 117 years old tomorrow.'
    	# (\d+)
    	# Matched: '117'
    	
    	# Space (will also match a tab)
    	# I am 117 years old tomorrow.
    	# (am\s*\d+)
    	# Matched: 'am 117'
    	
    	# \S (non space -- note, capital 'S')
    	# 'I am 117 years old tomorrow.'
    	# (y\S+)
    	# Matched: 'years'
    	
    	# Alphanumeric, including underscore
    	# \w
    	# 'Iam117yearsold_tomorrow.'
    	# (y\w*)
    	# Matched: 'yearsold_tomorrow'
    	
    	my $text = 'I am 117 years old tomorrow.';
    	
    	if($text =~ /(y\S+)/) {
    		print "Matched: '$1'\n";
    	}
    
    	
    	
    }
    
    main();
    

    T11. Numeric Quantifiers

    use strict;
    use warnings;
    
    $|=1;
    
    sub main {
    	
    	# * zero or more of the preceding character, as many as possible
    	# + one or more of the preceding, as many as possible
    	# *? zero or more of the preceding character, as few as possible
    	# +? one or more of the preceding, as few as possible
    	# {5} five of the preceding
    	# {3,6} at least three and at most 6
    	# {3,} at least three 
    
    	my $text = 'DE$75883';
    	
    	if($text =~ /(DE\$\d{3,})/) {
    		print "Matched: '$1'\n";
    	}
    }
    
    main();
    

    Section 2: More on Reading Files Line By Line: Tips, Tricks and Vital Knowledge

    T13. Split and reading csv files

    use strict;
    use warnings;
    
    $|=1;
    
    sub main {
    	my $input = 'test.csv';	
    	# execute if the condition is FALSE
    	unless(open(INPUT, $input)) { 
    		die "\nCannot open $input\n";
    	}
    	<INPUT>; # get rid of header
    	# if not use $line, then can use $_
    	while(my $line = <INPUT>) { 
    		my @values = split ',', $line;
    		print $values[1] . "\n";
    	}
    	close INPUT;
    }
    
    main();
    

    T14. Join and Viewing Data Using Data::Dumper

    use strict;
    use warnings;
    
    use Data::Dumper;
    
    $|=1;
    
    sub main {
    	my $input = 'test.csv';
    	unless(open(INPUT, $input)) {
    		die "\nCannot open $input\n";
    	}	
    	<INPUT>;	
    	while(my $line = <INPUT>) {		
    		my @values = split ',', $line;
    		# print join '|', @values;		
    		print Dumper(@values);
    	}	
    	close INPUT;
    }
    
    main();
    

    Execution output:

    $VAR1 = 'Isaac Newton';
    $VAR2 = '99.10';
    $VAR3 = '15051999
    ';
    $VAR1 = 'Albert Einstein';
    $VAR2 = '13.20';
    $VAR3 = '11062012
    ';
    $VAR1 = 'Carl Scheele';
    $VAR2 = '66.23';
    $VAR3 = '01012000
    ';
    $VAR1 = 'Rene Descartes';
    $VAR2 = '0.57';
    $VAR3 = '10072033
    ';
    

    The problem is that at the end of each line, there is a \n which is the reason why '; is printed out at the next line.

    T15. Chomp and Removing Spaces in Splits

    use strict;
    use warnings;
    
    use Data::Dumper;
    
    $|=1;
    
    sub main {	
    	my $input = 'test.csv';	
    	unless(open(INPUT, $input)) {
    		die "\nCannot open $input\n";
         }
    	<INPUT>;	
    	while(my $line = <INPUT>) {		
    		chomp $line; # chomp
    		# get rid of space
    		my @values = split /\s*,\s*/, $line; 		
    		print Dumper(@values);
    	}
    	close INPUT;
    }
    
    main();
    

    chomp()
    Using the Perl chomp() function
    perl中chomp的使用

    T16. “Pushing” Onto Arrays

    use strict;
    use warnings;
    
    use Data::Dumper;
    
    $|=1;
    
    sub main {
    	
    	my @array;		
    	push @array, 'apple';
    	push @array, 'banana';
    	push @array, 'peach';
    	
    	foreach my $element(@array) {
    		print $element . "\n";
    	}
    }
    
    main();
    

    T17. Array of arrays

    use strict;
    use warnings;
    use Data::Dumper;
    
    my @animals = ('dog', 'cat', 'rabbit');
    my @fruits = ('apple', 'banana', 'orange');
    
    my $fruits_ref = \@fruits; # create a reference
    print $fruits_ref->[0] . "\n";
    
    my @values;
    
    push @values, \@animals;
    push @values, \@fruits;
    
    print Dumper(@values);
    

    Output:

    apple
    $VAR1 = [
              'dog',
              'cat',
              'rabbit'
            ];
    $VAR2 = [
              'apple',
              'banana',
              'orange'
            ];
    
    use strict;
    use warnings;
    use Data::Dumper;
    
    $|=1;
    
    sub main {
    	my $input = 'test.csv';
    	unless(open(INPUT, $input)) {
    		die "\nCannot open $input\n";
    	}	
    	<INPUT>;	
    	my @lines;
    	while(my $line = <INPUT>) {		
    		chomp $line;		
    		my @values = split /\s*,\s*/, $line;
    		push @lines, \@values;
    	}
    	close INPUT;
    	print $lines[3][1] . "\n";
    	foreach my $line(@lines) {
    		print Dumper($line);
    		print "Name " . $line->[0] . "\n";
    	}
    }
    
    main();
    

    Output:

    0.57
    $VAR1 = [
              'Isaac Newton',
              '99.10',
              '15051999'  
            ];
    Name Isaac Newton
    $VAR1 = [
              'Albert Einstein',
              '13.20',
              '11062012'
            ];
    Name Albert Einstein
    $VAR1 = [
              'Carl Scheele',
              '66.23',
              '01012000'
            ];
    Name Carl Scheele
    $VAR1 = [
              'Rene Descartes',
              '0.57',
              '10072033'
            ];
    Name Rene Descartes
    

    T18. Hashes: Lookup Tables in Perl

    use strict;
    use warnings;
    use Data::Dumper;
    
    $| = 1;
    
    sub main {
    	my %months = (
    		1 => "Jan",
    		5 => "May",
    		7 => "Jul",
    	);
    	
    	print $months{5} . "\n";
    	my %days;
    	
    	$days{"Sunday"} = 1;
    	$days{"Monday"} = 2;
    	$days{"Friday"} = 6;
    	
    	my $day = $days{"Friday"};	
    	print "Friday is day $day\n";
    }
    
    main();
    

    the order of hash can not be relied on.

    T19. Iterating over Hashes

    use strict;
    use warnings;
    use Data::Dumper;
    
    $| = 1;
    
    sub main {
    	my %foods = (
    		"mice" => "cheese",
    		"dogs" => "meat",
    		"birds" => "seeds",
    	);	
    	
    	# We can define an array of variables like this:
    	# round brackets are required
    	my ($one, $two, $three) = (13, 21, 38);
    	print "The value of \$two is $two\n";
    	while( my ($key, $value) = each %foods) {
    		print "$key: $value\n";
    	}
    	
    	# sort the key according to the order of A-Z
    	foreach my $key(sort keys %foods) {
    		my $value = $foods{$key};
    		
    		print "$key = $value\n";
    	}
    
    }
    
    main();
    

    Output:

    The value of $two is 21
    dogs: meat
    birds: seeds
    mice: cheese
    birds = seeds
    dogs = meat
    mice = cheese
    

    T20. Array of Hashes

    use strict;
    use warnings;
    use Data::Dumper;
    
    $| = 1;
    
    sub main {
    	my %hash1 = (
    		"cat" => "meat",
    		"birds" => "seeds",
    		"fish" => "worms",
    	);
    	
    	my @test;
    	# We could push a reference to a hash onto an array.
    	push @test, \%hash1;	
    	# We could also just refer to the element after the end of the array
    	# and by setting it, create it:
    	$test[1] = \%hash1;	
    	print $test[0]{"birds"} . "\n";
    	print $test[1]{"fish"} . "\n";
    }
    
    main();
    

    Output:

    seeds
    worms
    

    T21. Storing CSV Data in a Data Structure

    use strict;
    use warnings;
    use Data::Dumper;
    
    $|=1;
    
    sub main {
    	
    	my $input = 'test.csv';	
    	unless(open(INPUT, $input)) {
    		die "\nCannot open $input\n";
    	}
    
    	<INPUT>;
    
    	my @data;
    
    	while(my $line = <INPUT>) {
    		chomp $line;
    		my ($name, $payment, $date) = split /\s*,\s*/, $line;
    		my %values = (
    			"Name" => $name,
    			"Payment" => $payment,
    			"Date" => $date,
    		);
    		push @data, \%values;
    	}
    	
    	close INPUT;
    	foreach my $data(@data){
    		print $data -> {"Payment"} . "\n";
    	}
    	print "Descartes: " . $data[3]-> {"Name"} . "\n";
    	print "Descartes: " . $data[3]{"Name"} . "\n";	
    }
    
    main();
    

    T22. Validating CSV Data

    use strict;
    use warnings;
    use Data::Dumper;
    
    $|=1;
    
    sub main {
    	
    	my $input = 'test.csv';	
    	unless(open(INPUT, $input)) {
    		die "\nCannot open $input\n";
    	}
    
    	<INPUT>;
    
    	my @data;
    
    	LINE: while(my $line = <INPUT>) {
    		chomp $line;
    		my @values = split /\s*,\s*/, $line;
    		if (scalar(@values) < 3){
    			print "Invalid line: $line\n";
    			next LINE;
    		}
    
    		foreach my $value(@values){
    			if ($value eq ''){
    				print "Invalid line: $line\n";
    				next LINE;
    			}
    		}
    
    		my ($name, $payment, $date) = @values;
    		my %values = (
    			"Name" => $name,
    			"Payment" => $payment,
    			"Date" => $date,
    		);
    		push @data, \%values;
    	}
    	
    	close INPUT;
    	foreach my $data(@data){
    		print $data -> {"Payment"} . "\n";
    	}
    	print "Descartes: " . $data[3]-> {"Name"} . "\n";
    }
    
    main();
    

    Input:

    Name,Payment,Date
    Isaac Newton ,99.1,15051999
    Albert Einstein,13.2,11062012
    Carl Scheele,66.23,1012000
    Rene Descartes,0.57,10072033
    Sarah,10,
    ,,
    Nicolas,0,
    

    Output:

    Invalid line: Sarah,10,
    Invalid line: ,,
    Invalid line: Nicolas,0,
    99.1
    13.2
    66.23
    0.57
    Descartes: Rene Descartes
    

    T23. Cleaning CSV Data

    use strict;
    use warnings;
    use Data::Dumper;
    
    $|=1;
    
    sub main {
    	
    	my $input = 'test.csv';	
    	unless(open(INPUT, $input)) {
    		die "\nCannot open $input\n";
    	}
    
    	<INPUT>;
    
    	my @data;
    
    	LINE: while(my $line = <INPUT>) {
    		# replace the space at the beginning of the line 
    		$line =~ s/^\s*//; 
    		# replace the space at the end of the line
    		$line =~ s/\s*$//; 
    		# replace the space at the beginning and end of the line
    		# g stands for global, replace as many as possible
    		$line =~ s/^\s*|\s*$//g; 
    
    		$line =~ /\S+/ or next;
    		chomp $line;
    		my @values = split /\s*,\s*/, $line;
    		if (scalar(@values) < 3){
    			print "Invalid line: $line\n";
    			next LINE;
    		}
    
    		foreach my $value(@values){
    			if ($value eq ''){
    				print "Invalid line: $line\n";
    				next LINE;
    			}
    		}
    
    		my ($name, $payment, $date) = @values;
    		my %values = (
    			"Name" => $name,
    			"Payment" => $payment,
    			"Date" => $date,
    		);
    		push @data, \%values;
    	}
    	
    	close INPUT;
    	foreach my $data(@data){
    		print $data -> {"Payment"} . "\n";
    	}
    	print "Descartes: " . $data[3]-> {"Name"} . "\n";
    }
    
    main();
    

    Section 3: Web Scraping and More Regular Expressions

    T25. Basic Web Scraping

    use strict;
    use warnings;
    
    use LWP::Simple;
    
    $|=1;
    
    sub main {
    	
    	my $content = get("http://www.caveofprogramming.com");
    	
    	unless(defined($content)) {
    		die "Unreachable url\n";
    	}
    
    	# m stands for match
    	# '' means there are no escape characters
    	if($content =~ m'<a class="mainlink" href=".+?">(.+?)</a>') {
    		my $title = $1;
    		
    		print "Title: $title\n";
    	}
    	else {
    		print "\nTitle not found\n";
    	}
    	
    }
    
    main();
    

    T26. Character classes

    use strict;
    use warnings;
    
    $|=1;
    
    sub main {
    	
    	# Ranges
    	# [0-9] any number
    	# [A-Z] any uppercase letter (in the English alphabet)
    	
    	# Alternatives
    	# [ABC] -- list of alternates
    	# [\=\%] - simply list alternatives. Backslash any character that might have a special meaning in regex
    	# [A-Za-z_0-9] -- specify alternatives just by listing them; can include ranges.
    	
    	# [^0-9T\s] ^ Match anything EXCEPT the specified characters.
    	
    	
    	my $content = "The 39 Steps - a GREAT book - Colours_15 ==%== ABBCCBBCCABCA";
    
    	
    	if($content =~ /([^0-9T\s]{5,})/) {
    		print "Matched '$1'\n";
    	}
    	else {
    		print "No match\n";
    	}
    	
    }
    
    main();
    

    T27. Matching Repeatedly

    use strict;
    use warnings;
    
    use LWP::Simple;
    
    $| = 1;
    
    sub main {
    
    	my $content = get("http://www.caveofprogramming.com");
    
    	unless ( defined($content) ) {
    		die "Unreachable url\n";
    	}
    
    	# <a href="http://news.bbc.co.uk">BBC News</a>
    	# []<>
    
    	while (
    		$content =~
    		m| # Use a pipe character as the quote, since we don't need to use it inside the regex.
    
    		<\s*a\s+ # Match the opening <a, with or without space around the 'a'
    
    		[^>]* # Match any amount of gumpf, as long as it's not the closing angle bracket quote
    		
    		href\s*=\s* # Match href=, with or without space around the '='
    		
    		['"] # Match either a single or double quote
    		
    		([^>"']+) # Match any text, as long as it doesn't include a closing '>' bracket or a quote
    		
    		['"] # Close the quote
    		
    		[^>]*> # Match anything other than the closing bracket, followed by the closing bracket.
    	
    		\s*([^<>]*)\s*</ # Match the hyperlinked text; any characters other than angle brackets
    	
    		|sigx # s: match across new lines; i: case insensitive match; g: global (repeated) match; x: allow whitespace and comments 
    		
    		{
    			print "$2: $1\n";
    		}
    
    }
    
    main();
    

    T28. Collecting Repeated Matches All At Once

    use strict;
    use warnings;
    
    use LWP::Simple;
    
    $| = 1;
    
    sub main {
    
    	my $content = get("http://www.caveofprogramming.com");
    
    	unless ( defined($content) ) {
    		die "Unreachable url\n";
    	}
    
    	my @classes = $content =~ m|class="([^"']*?)"|ig;
    	
    	if(@classes == 0) {
    		print "No matches\n";
    	}
    	else {
    		foreach my $class(@classes) {
    			print "$class\n";
    		}
    	}
    }
    
    main();
    

    Perl 5 version 30.0 documentation defined
    defined函数

    Section 4: Building a Complete Progam: Command Line Options

    T29. Getting Command Line Options

    use strict;
    use warnings;
    use Data::Dumper;
    use Getopt::Std;
    
    $| = 1;
    
    sub main {
    	my %opts;
    	# : after f means receive the file name after -f
    	getopts('af:c', \%opts);
    	print Dumper(%opts);
    	my $file = $opts{'f'};
    	print "File: $file\n";
    }
    
    main();
    

    Output:

    (base) MacBook-Air:desktop $ perl tutorial29.pl -f test.xml -a -c
    $VAR1 = 'f';
    $VAR2 = 'test.xml';
    $VAR3 = 'c';
    $VAR4 = 1;
    $VAR5 = 'a';
    $VAR6 = 1;
    File: test.xml
    

    Perl Command Line Options
    Perl Argv – Retrieving Command-Line Arguments in Perl

    • Take a look at “More Intelligent Command-Line Option Processing Using Getopts” part to get the explaination of above script in detail.

    T30. Subroutines and Returning Values

    use strict;
    use warnings;
    use Data::Dumper;
    use Getopt::Std;
    
    $| = 1;
    
    sub main {
    	my %opts;
    	getopts('af:c', \%opts);
    	if(!checkusage()) {
    		usage();
    	} 
    }
    
    sub checkusage {
    	return 0;
    }
    
    sub usage {
    	print "incorrect options\n";
    }
    
    main();
    

    T31. Multi-Line Strings and Comments

    use strict;
    use warnings;
    use Data::Dumper;
    use Getopt::Std;
    
    $| = 1;
    
    =pod
    
    	This is ACME XML parser version 1.0
    	Use with care.
    	
    =cut
    
    sub main {
    	my %opts;
    	# Get command line options
    	getopts('af:c', \%opts);
    	
    	if(!checkusage()) {
    		usage();
    	} 
    }
    
    sub checkusage {
    	return 0;
    }
    
    sub usage {
    	print <<USAGE;
    	
    usage: perl main.pl <options>
    	-f <file name>	specify XML file name to parse
    	-a	turn off error checking
    
    example usage:
    	perl main.pl -f test.xml -a
    	
    USAGE
    	
    	exit();
    }
    
    main();
    

    Output:

    (base) MacBook-Air:desktop $ perl tutorial31.pl
    	
    usage: perl main.pl <options>
    	-f <file name>	specify XML file name to parse
    	-a	turn off error checking
    
    example usage:
    	perl main.pl -f test.xml -a
    

    T32. Passing Arguments to Subroutines

    use strict;
    use warnings;
    use Data::Dumper;
    use Getopt::Std;
    
    $| = 1;
    
    =pod
    
    	This is ACME XML parser version 1.0
    	Use with care.
    	
    =cut
    
    sub main {
    	my %opts;
    	# Get command line options
    	getopts('af:c', \%opts);
    	
    	if(!checkusage("Hello", 7)) {
    		usage();
    	} 
    }
    
    sub checkusage {
    	
    	# my $greeting = shift; or my $greeting = shift @_;
    	# my $number = shift; or my $number = shift @_;
    	# shift has @_ argument by default
    	# shift means getting a value from the beginning of an array
    	# push means pushing a value to the end of an array
    	# pop means popping out a value from the end of an array
    	
    	my ($greeting, $number) = @_;
    	print "$greeting number $number\n";	
    	
    	return 1;
    }
    
    sub usage {
    	print <<USAGE;
    	
    usage: perl main.pl <options>
    	-f <file name>	specify XML file name to parse
    	-a	turn off error checking
    
    example usage:
    	perl main.pl -f test.xml -a
    	
    USAGE
    	
    	exit();
    }
    
    main();
    

    T33. References to Hashes

    use strict;
    use warnings;
    use Data::Dumper;
    use Getopt::Std;
    
    $| = 1;
    
    =pod
    
    	This is ACME XML parser version 1.0
    	Use with care.
    	
    =cut
    
    sub main {
    	my %opts;
    	
    	# Get command line options
    	getopts('af:c', \%opts);
    	
    	if(!checkusage(\%opts)) {
    		usage();
    	} 
    	
    =pod
    	perl parse.pl -a -f test.xml -c
    	
    	a => 1
    	c => 1
    	f => test.xml
    =cut
    
    	my $opts_ref = \%opts;
    	
    	# Use hash directly.
    	print $opts{"f"} . "\n";
    	
    	# Use reference to hash
    	print $opts_ref->{"f"} . "\n";
    }
    
    sub checkusage {
    	# only one argument \%opts is passed to the subroutine
    	my $opts_ref = shift;
    	print $opts_ref->{"f"} . "\n"; 
    	return 1;
    }
    
    sub usage {
    	print <<USAGE;
    	
    usage: perl main.pl <options>
    	-f <file name>	specify XML file name to parse
    	-a	turn off error checking
    
    example usage:
    	perl main.pl -f test.xml -a
    	
    USAGE
    	
    	exit();
    }
    
    main();
    

    Output:

    (base) MacBook-Air:desktop $ perl tutorial33.pl -f test.xml -a -c
    test.xml
    test.xml
    test.xml
    

    T34. Checking Values in Hashes

    use strict;
    use warnings;
    use Data::Dumper;
    use Getopt::Std;
    
    $| = 1;
    
    =pod
    
    	This is ACME XML parser version 1.0
    	Use with care.
    	
    =cut
    
    sub main {
    	my %opts;
    	
    	# Get command line options
    	getopts('af:r', \%opts);
    	
    	if(!checkusage(\%opts)) {
    		usage();
    	} 
    	
    =pod
    	perl parse.pl -a -f test.xml -r
    	
    	a => 1
    	r => 1
    	f => test.xml
    =cut
    
    }
    
    sub checkusage {
    	my $opts = shift;
    	
    	my $a = $opts->{"a"};
    	my $r = $opts->{"r"};
    	my $f = $opts->{"f"};
    	
    	# Image a is optional; don't really need to refer to it here at all.
    	
    	# r is mandatory: it means "run the program"
    	# f is mandatory.
    	
    	unless(defined($r) and defined($f)) {
    		return 0;
    	}
    	
    	unless($f =~ /\.xml$/i) {
    		print "Input file must have the extension .xml\n";
    		return 0;
    	}
    	
    	return 1;
    }
    
    sub usage {
    	print <<USAGE;
    	
    usage: perl main.pl <options>
    	-f <file name>	specify XML file name to parse
    	-a	turn off error checking
    	-r run the program
    
    example usage:
    	perl main.pl -r -f test.xml -a
    	
    USAGE
    	
    	exit();
    }
    
    main();
    

    Section 5: Parsing XML and Complex Data Structures

    T35. Finding All Files in a Directory and Filtering Arrays

    use strict;
    use warnings;
    use Data::Dumper;
    use Getopt::Std;
    
    $| = 1;
    
    =pod
    
    	This is ACME XML parser version 1.0
    	Use with care.
    	
    =cut
    
    sub main {
    	my %opts;
    	
    	# Get command line options
    	getopts('d:r', \%opts);
    	
    	if(!checkusage(\%opts)) {
    		usage();
    		exit();
    	} 
    	
    	my $input_dir = $opts{"d"};
    	my @files = get_files($input_dir);
    	print Dumper(\@files);
    }
    
    sub get_files {
    	my $input_dir = shift;
    	unless(opendir(INPUTDIR, $input_dir)) {
    		die "\nUnable to open directory '$input_dir'\n";
    	}
    	# find all files in a directory
    	my @files = readdir(INPUTDIR);
    	closedir(INPUTDIR);
    	# filter out files end with ".xml"
    	@files = grep(/\.xml$/i, @files);
    	return @files;
    }
    
    sub checkusage {
    	my $opts = shift;
    	
    	my $r = $opts->{"r"};
    	my $d = $opts->{"d"};
    	
    	# Image a is optional; don't really need to refer to it here at all.
    	
    	# r is optional
    	# d is mandatory.
    	
    	unless(defined($d)) {
    		return 0;
    	}
    	
    	return 1;
    }
    
    sub usage {
    	print <<USAGE;
    	
    usage: perl main.pl <options>
    	-d <directory>	specify directory in which to find XML files.
    	-r run the program; process the files
    
    example usage:
    	# Process files in currect directory.
    	perl main.pl -d . -r
    	
    USAGE
    }
    
    main();
    

    If there is no return argument at the end of the subroutine, the subroutine will return its last line. If it’s a print function, then the returned value would be 1.

    T36. Processing Files One By One

    use strict;
    use warnings;
    use Data::Dumper;
    use Getopt::Std;
    
    $| = 1;
    
    =pod
    
    	This is ACME XML parser version 1.0
    	Use with care.
    	
    =cut
    
    sub main {
    	my %opts;
    	# Get command line options
    	getopts('d:r', \%opts);
    	if(!checkusage(\%opts)) {
    		usage();
    		exit();
    	} 
    	
    	my $input_dir = $opts{"d"};
    	my @files = get_files($input_dir);
    	process_files(\@files, $input_dir);
    }
    
    sub process_files {
    	my ($files, $input_dir) = @_;
    	
    	print "$input_dir\n";
    	print Dumper($files);
    	
    	# $files is a reference, so need to put @ before to make it an array
    	foreach my $file(@$files) {
    		process_file($file, $input_dir);
    	}
    }
    
    sub process_file {
    	my ($file, $input_dir) = @_;
    	
    	print "Processing $file in $input_dir ...\n";
    }
    
    sub get_files {
    	my $input_dir = shift;
    	
    	unless(opendir(INPUTDIR, $input_dir)) {
    		die "\nUnable to open directory '$input_dir'\n";
    	}
    	
    	my @files = readdir(INPUTDIR);
    	
    	closedir(INPUTDIR);
    	
    	@files = grep(/\.xml$/i, @files);
    	
    	return @files;
    }
    
    sub checkusage {
    	my $opts = shift;
    	my $r = $opts->{"r"};
    	my $d = $opts->{"d"};
    	
    	# Image a is optional; don't really need to refer to it here at all.
    	
    	# r is optional
    	# d is mandatory.
    	
    	unless(defined($d)) {
    		return 0;
    	}
    	return 1;
    }
    
    sub usage {
    	print <<USAGE;
    	
    usage: perl main.pl <options>
    	-d <directory>	specify directory in which to find XML files.
    	-r run the program; process the files
    
    example usage:
    	# Process files in currect directory.
    	perl main.pl -d . -r
    	
    USAGE
    }
    
    main();
    

    @$files means cast $files to the original type array, @{$files} can also be used.

    T37. Parsing XML with Regular Expressions

    use strict;
    use warnings;
    use Data::Dumper;
    use Getopt::Std;
    
    $| = 1;
    
    =pod
    
    	This is ACME XML parser version 1.0
    	Use with care.
    	
    =cut
    
    sub main {
    	my %opts;
    	
    	# Get command line options
    	getopts('d:r', \%opts);
    	
    	if(!checkusage(\%opts)) {
    		usage();
    		exit();
    	} 
    	
    	my $input_dir = $opts{"d"};
    
    	my @files = get_files($input_dir);
    	
    	process_files(\@files, $input_dir);
    }
    
    sub process_files {
    	my ($files, $input_dir) = @_;
    	
    	foreach my $file(@$files) {
    		process_file($file, $input_dir);
    	}
    }
    
    sub process_file {
    	my ($file, $input_dir) = @_;
    	
    	print "Processing $file in $input_dir ... \n";
    	
    	my $filepath = "$input_dir/$file";
    	
    	open(INPUTFILE, $filepath) or die "Unable to open $filepath\n";
    	
    	$/ = "</entry>";
    	
    	while(my $chunk = <INPUTFILE>) {
    		# there must be round brackets
    		# otherwise $band will get the value: True or False
    		my ($band) = $chunk =~ m'<band>(.*?)</band>';
    		
    		unless(defined($band)) {
    			next;
    		}
    		
    		my @albums = $chunk =~ m'<album>(.*?)</album>'sg;
    		
    		print " found " . scalar(@albums) . " for $band ...\n";
    		
    		print Dumper(@albums);
    	}
    	
    	close(INPUTFILE);
    	
    	
    }
    
    sub get_files {
    	my $input_dir = shift;
    	
    	unless(opendir(INPUTDIR, $input_dir)) {
    		die "\nUnable to open directory '$input_dir'\n";
    	}
    	
    	my @files = readdir(INPUTDIR);
    	
    	closedir(INPUTDIR);
    	
    	@files = grep(/\.xml$/i, @files);
    	
    	return @files;
    }
    
    sub checkusage {
    	my $opts = shift;
    	
    	my $r = $opts->{"r"};
    	my $d = $opts->{"d"};
    	
    	# Image a is optional; don't really need to refer to it here at all.
    	
    	# r is optional
    	# d is mandatory.
    	
    	unless(defined($d)) {
    		return 0;
    	}
    	
    	return 1;
    }
    
    sub usage {
    	print <<USAGE;
    	
    usage: perl main.pl <options>
    	-d <directory>	specify directory in which to find XML files.
    	-r run the program; process the files
    
    example usage:
    	# Process files in currect directory.
    	perl main.pl -d . -r
    	
    USAGE
    }
    
    main();
    

    Input: test.xml

    <xml>
    <entry>
        <band>The Basspluckers</band>
        <album>
            <name>Revenge of the Squirrels</name>
            <chartposition>434</chartposition>
        </album>
        <album>
            <name>Pluck My Bass</name>
            <chartposition>123</chartposition>
        </album>
    </entry>
    <entry>
        <band>The Dead Drunks</band>
        <album>
            <name>Spy in the House of Love</name>
            <chartposition>10</chartposition>
        </album>
        <album>
            <name>Get Out of My House of Love, You Spy</name>
            <chartposition>74</chartposition>
        </album>
    </entry>
    </xml>
    

    ** Output:**

    (base) MacBook-Air:test $ perl tutorial37.pl -d /Users/jiangfan/desktop/test
    Processing test.xml in /Users/jiangfan/desktop/test ... 
     found 2 for The Basspluckers ...
    $VAR1 = '
            <name>Revenge of the Squirrels</name>
            <chartposition>434</chartposition>
        ';
    $VAR2 = '
            <name>Pluck My Bass</name>
            <chartposition>123</chartposition>
        ';
     found 2 for The Dead Drunks ...
    $VAR1 = '
            <name>Spy in the House of Love</name>
            <chartposition>10</chartposition>
        ';
    $VAR2 = '
            <name>Get Out of My House of Love, You Spy</name>
            <chartposition>74</chartposition>
        ';
    

    Highlight:

    		# there must be round brackets
    		# otherwise $band will get the value: True or False
    		my ($band) = $chunk =~ m'<band>(.*?)</band>';
    

    Perl One-Liners | Perl命令行学习5 $/和$\变量
    Perl’s Special Variables
    Perl: Downloading and Parsing XML

    T38. Using XML::Simple, and Extracting Data from Complex Structures

    use strict;
    use warnings;
    use Data::Dumper;
    use Getopt::Std;
    use XML::Simple;
    
    $| = 1;
    
    =pod
    
    	This is ACME XML parser version 1.0
    	Use with care.
    	
    =cut
    
    sub main {
    	my %opts;
    	
    	# Get command line options
    	getopts('d:r', \%opts);
    	
    	if(!checkusage(\%opts)) {
    		usage();
    		exit();
    	} 
    	
    	my $input_dir = $opts{"d"};
    
    	my @files = get_files($input_dir);
    	
    	process_files(\@files, $input_dir);
    }
    
    sub process_files {
    	my ($files, $input_dir) = @_;
    	
    	foreach my $file(@$files) {
    		process_file($file, $input_dir);
    	}
    }
    
    sub process_file {
    	my ($file, $input_dir) = @_;
    	
    	print "Processing $file in $input_dir ... \n";
    	
    	my $filepath = "$input_dir/$file";
    	
    	open(INPUTFILE, $filepath) or die "Unable to open $filepath\n";
    	
    	undef $/;
    	
    	my $content = <INPUTFILE>;
    	
    	close(INPUTFILE);
    	
    	print $content;
    	
    	my $parser = new XML::Simple;
    	
    	my $dom = $parser->XMLin($content);
    	
    	print Dumper($dom);
    	
    
    	foreach my $band(@{$dom->{"entry"}}) {
    		print Dumper($band->{"band"});
    	}
    }
    
    sub get_files {
    	my $input_dir = shift;
    	
    	unless(opendir(INPUTDIR, $input_dir)) {
    		die "\nUnable to open directory '$input_dir'\n";
    	}
    	
    	my @files = readdir(INPUTDIR);
    	
    	closedir(INPUTDIR);
    	
    	@files = grep(/\.xml$/i, @files);
    	
    	return @files;
    }
    
    sub checkusage {
    	my $opts = shift;
    	
    	my $r = $opts->{"r"};
    	my $d = $opts->{"d"};
    	
    	# Image a is optional; don't really need to refer to it here at all.
    	
    	# r is optional
    	# d is mandatory.
    	
    	unless(defined($d)) {
    		return 0;
    	}
    	
    	return 1;
    }
    
    sub usage {
    	print <<USAGE;
    	
    usage: perl main.pl <options>
    	-d <directory>	specify directory in which to find XML files.
    	-r run the program; process the files
    
    example usage:
    	# Process files in currect directory.
    	perl main.pl -d . -r
    	
    USAGE
    }
    
    main();
    

    Output:

    (base) MacBook-Air:test jiangfan$ perl tutorial38.pl -d /Users/jiangfan/desktop/test
    Processing test.xml in /Users/jiangfan/desktop/test ... 
    <xml>
    <entry>
        <band>The Basspluckers</band>
        <album>
            <name>Revenge of the Squirrels</name>
            <chartposition>434</chartposition>
        </album>
        <album>
            <name>Pluck My Bass</name>
            <chartposition>123</chartposition>
        </album>
    </entry>
    <entry>
        <band>The Dead Drunks</band>
        <album>
            <name>Spy in the House of Love</name>
            <chartposition>10</chartposition>
        </album>
        <album>
            <name>Get Out of My House of Love, You Spy</name>
            <chartposition>74</chartposition>
        </album>
    </entry>
    </xml>$VAR1 = {
              'entry' => [
                         {
                           'band' => 'The Basspluckers',
                           'album' => {
                                      'Revenge of the Squirrels' => {
                                                                    'chartposition' => '434'
                                                                  },
                                      'Pluck My Bass' => {
                                                         'chartposition' => '123'
                                                       }
                                    }
                         },
                         {
                           'band' => 'The Dead Drunks',
                           'album' => {
                                      'Get Out of My House of Love, You Spy' => {
                                                                                'chartposition' => '74'
                                                                              },
                                      'Spy in the House of Love' => {
                                                                    'chartposition' => '10'
                                                                  }
                                    }
                         }
                       ]
            };
    $VAR1 = 'The Basspluckers';
    $VAR1 = 'The Dead Drunks';
    

    T39. Extracting Data from Complex Structures: A Complete Example

    use strict;
    use warnings;
    use Data::Dumper;
    use Getopt::Std;
    use XML::Simple;
    
    $| = 1;
    
    =pod
    
    	This is ACME XML parser version 1.0
    	Use with care.
    	
    =cut
    
    sub main {
    	my %opts;
    	
    	# Get command line options
    	getopts('d:r', \%opts);
    	
    	if(!checkusage(\%opts)) {
    		usage();
    		exit();
    	} 
    	
    	my $input_dir = $opts{"d"};
    
    	my @files = get_files($input_dir);
    	
    	process_files(\@files, $input_dir);
    }
    
    sub process_files {
    	my ($files, $input_dir) = @_;
    	
    	foreach my $file(@$files) {
    		process_file($file, $input_dir);
    	}
    }
    
    sub process_file {
    	my ($file, $input_dir) = @_;
    	
    	print "Processing $file in $input_dir ... \n";
    	
    	my $filepath = "$input_dir/$file";
    	
    	open(INPUTFILE, $filepath) or die "Unable to open $filepath\n";
    	
    	undef $/;
    	
    	my $content = <INPUTFILE>;
    	
    	close(INPUTFILE);
    	
    	print $content;
    	
    	my $parser = new XML::Simple;
    	
    	my $dom = $parser->XMLin($content, ForceArray => 1);
    	
    	print Dumper($dom);
    	
    
    	foreach my $band(@{$dom->{"entry"}}) {
    		my $band_name = $band->{"band"}->[0];
    		
    		print "\n\n$band_name\n";
    		print "============\n";
    		
    		my $albums = $band->{"album"};
    		
    		foreach my $album(@$albums) {
    			my $album_name = $album->{"name"}->[0];
    			my $chartposition =  $album->{"chartposition"}->[0];
    			
    			print "$album_name: $chartposition\n";
    		}
    	}
    }
    
    sub get_files {
    	my $input_dir = shift;
    	
    	unless(opendir(INPUTDIR, $input_dir)) {
    		die "\nUnable to open directory '$input_dir'\n";
    	}
    	
    	my @files = readdir(INPUTDIR);
    	
    	closedir(INPUTDIR);
    	
    	@files = grep(/\.xml$/i, @files);
    	
    	return @files;
    }
    
    sub checkusage {
    	my $opts = shift;
    	
    	my $r = $opts->{"r"};
    	my $d = $opts->{"d"};
    	
    	# Image a is optional; don't really need to refer to it here at all.
    	
    	# r is optional
    	# d is mandatory.
    	
    	unless(defined($d)) {
    		return 0;
    	}
    	
    	return 1;
    }
    
    sub usage {
    	print <<USAGE;
    	
    usage: perl main.pl <options>
    	-d <directory>	specify directory in which to find XML files.
    	-r run the program; process the files
    
    example usage:
    	# Process files in currect directory.
    	perl main.pl -d . -r
    	
    USAGE
    }
    
    main();
    

    T40. Building Complex Data Structures

    use strict;
    use warnings;
    use Data::Dumper;
    use Getopt::Std;
    use XML::Simple;
    
    $| = 1;
    
    =pod
    
    	This is ACME XML parser version 1.0
    	Use with care.
    	
    =cut
    
    sub main {
    	my %opts;
    	
    	# Get command line options
    	getopts('d:r', \%opts);
    	
    	if(!checkusage(\%opts)) {
    		usage();
    		exit();
    	} 
    	
    	my $input_dir = $opts{"d"};
    
    	my @files = get_files($input_dir);
    	
    	process_files(\@files, $input_dir);
    }
    
    sub process_files {
    	my ($files, $input_dir) = @_;
    	
    	foreach my $file(@$files) {
    		my @bands = process_file($file, $input_dir);
    		
    		print Dumper(@bands);
    	}
    }
    
    sub process_file {
    	my ($file, $input_dir) = @_;
    	
    	print "Processing $file in $input_dir ... \n";
    	
    	my $filepath = "$input_dir/$file";
    	
    	open(INPUTFILE, $filepath) or die "Unable to open $filepath\n";
    	
    	undef $/;
    	
    	my $content = <INPUTFILE>;
    	
    	close(INPUTFILE);
    	
    	my $parser = new XML::Simple;
    	
    	my $dom = $parser->XMLin($content, ForceArray => 1);
    	
    	my @output;
    	
    	foreach my $band(@{$dom->{"entry"}}) {
    		my $band_name = $band->{"band"}->[0];
    		
    		my $albums = $band->{"album"};
    		
    		my @albums;
    		
    		foreach my $album(@$albums) {
    			my $album_name = $album->{"name"}->[0];
    			my $chartposition =  $album->{"chartposition"}->[0];
    			
    			push @albums, {
    				"name" => $album_name,
    				"position" => $chartposition,
    			};
    		}
    		
    		push @output, {
    			"name" => $band_name,
    			"albums" => \@albums,
    		};
    		
    	} # foreach band
    	
    	return @output;
    }
    
    sub get_files {
    	my $input_dir = shift;
    	
    	unless(opendir(INPUTDIR, $input_dir)) {
    		die "\nUnable to open directory '$input_dir'\n";
    	}
    	
    	my @files = readdir(INPUTDIR);
    	
    	closedir(INPUTDIR);
    	
    	@files = grep(/\.xml$/i, @files);
    	
    	return @files;
    }
    
    sub checkusage {
    	my $opts = shift;
    	
    	my $r = $opts->{"r"};
    	my $d = $opts->{"d"};
    	
    	# Image a is optional; don't really need to refer to it here at all.
    	
    	# r is optional
    	# d is mandatory.
    	
    	unless(defined($d)) {
    		return 0;
    	}
    	
    	return 1;
    }
    
    sub usage {
    	print <<USAGE;
    	
    usage: perl main.pl <options>
    	-d <directory>	specify directory in which to find XML files.
    	-r run the program; process the files
    
    example usage:
    	# Process files in currect directory.
    	perl main.pl -d . -r
    	
    USAGE
    }
    
    main();
    

    Section 6: Working with Databases

    T43. Connecting to a Database

    use strict;
    use warnings;
    use Data::Dumper;
    use Getopt::Std;
    use XML::Simple;
    
    use DBI;
    
    $| = 1;
    
    =pod
    
    	This is ACME XML parser version 1.0
    	Use with care.
    	
    =cut
    
    sub main {
    	my %opts;
    	
    	# Get command line options
    	getopts('d:r', \%opts);
    	
    	if(!checkusage(\%opts)) {
    		usage();
    		exit();
    	} 
    	
    	my $input_dir = $opts{"d"};
    
    	my @files = get_files($input_dir);
    	
    	my @data = process_files(\@files, $input_dir);
    	
    	print Dumper(@data);
    	
    	my $dbh = DBI->connect("dbi:mysql:bands", "john", "letmein");
    	
    	unless(defined($dbh)) {
    		die "Cannot connect to database.\n";
    	}
    	
    	print "Connected\n";
    	
    	$dbh->disconnect();
    }
    
    sub process_files {
    	my ($files, $input_dir) = @_;
    	
    	my @data;
    	
    	foreach my $file(@$files) {
    		push @data, process_file($file, $input_dir);
    	}
    	
    	return @data;
    }
    
    sub process_file {
    	my ($file, $input_dir) = @_;
    	
    	print "Processing $file in $input_dir ... \n";
    	
    	my $filepath = "$input_dir/$file";
    	
    	open(INPUTFILE, $filepath) or die "Unable to open $filepath\n";
    	
    	undef $/;
    	
    	my $content = <INPUTFILE>;
    	
    	close(INPUTFILE);
    	
    	my $parser = new XML::Simple;
    	
    	my $dom = $parser->XMLin($content, ForceArray => 1);
    	
    	my @output;
    	
    	foreach my $band(@{$dom->{"entry"}}) {
    		my $band_name = $band->{"band"}->[0];
    		
    		my $albums = $band->{"album"};
    		
    		my @albums;
    		
    		foreach my $album(@$albums) {
    			my $album_name = $album->{"name"}->[0];
    			my $chartposition =  $album->{"chartposition"}->[0];
    			
    			push @albums, {
    				"name" => $album_name,
    				"position" => $chartposition,
    			};
    		}
    		
    		push @output, {
    			"name" => $band_name,
    			"albums" => \@albums,
    		};
    		
    	} # foreach band
    	
    	return @output;
    }
    
    sub get_files {
    	my $input_dir = shift;
    	
    	unless(opendir(INPUTDIR, $input_dir)) {
    		die "\nUnable to open directory '$input_dir'\n";
    	}
    	
    	my @files = readdir(INPUTDIR);
    	
    	closedir(INPUTDIR);
    	
    	@files = grep(/\.xml$/i, @files);
    	
    	return @files;
    }
    
    sub checkusage {
    	my $opts = shift;
    	
    	my $r = $opts->{"r"};
    	my $d = $opts->{"d"};
    	
    	# Image a is optional; don't really need to refer to it here at all.
    	
    	# r is optional
    	# d is mandatory.
    	
    	unless(defined($d)) {
    		return 0;
    	}
    	
    	return 1;
    }
    
    sub usage {
    	print <<USAGE;
    	
    usage: perl main.pl <options>
    	-d <directory>	specify directory in which to find XML files.
    	-r run the program; process the files
    
    example usage:
    	# Process files in currect directory.
    	perl main.pl -d . -r
    	
    USAGE
    }
    
    main();
    

    T44. Inserting Data into a Database

    use strict;
    use warnings;
    use Data::Dumper;
    use Getopt::Std;
    use XML::Simple;
    
    use DBI;
    
    $| = 1;
    
    =pod
    
    	This is ACME XML parser version 1.0
    	Use with care.
    	
    =cut
    
    sub main {
    	my %opts;
    	
    	# Get command line options
    	getopts('d:r', \%opts);
    	
    	if(!checkusage(\%opts)) {
    		usage();
    		exit();
    	} 
    	
    	my $input_dir = $opts{"d"};
    
    	my @files = get_files($input_dir);
    	
    	my @data = process_files(\@files, $input_dir);
    	
    	add_to_database(\@data);
    	
    	print Dumper(@data);
    	
    	
    }
    
    sub add_to_database {
    	my $data = shift;
    	
    	my $dbh = DBI->connect("dbi:mysql:bands", "john", "letmein");
    	
    	unless(defined($dbh)) {
    		die "Cannot connect to database.\n";
    	}
    	
    	print "Connected to database.\n";
    	
    	my $sth = $dbh->prepare('insert into bands (name) values (?)');
    	
    	unless($sth) {
    		die "Error preparing SQL\n";
    	}
    	
    
    	foreach my $data(@$data) {
    		my $band_name = $data->{"name"};
    		
    		print "Inserting $band_name into database ...\n";
    		
    		unless($sth->execute($band_name)) {
    			die "Error executing SQL\n";
    		}
    	}
    	
    	$sth->finish();
    	
    	$dbh->disconnect();
    	
    	print "Completed.\n";
    }
    
    sub process_files {
    	my ($files, $input_dir) = @_;
    	
    	my @data;
    	
    	foreach my $file(@$files) {
    		push @data, process_file($file, $input_dir);
    	}
    	
    	return @data;
    }
    
    sub process_file {
    	my ($file, $input_dir) = @_;
    	
    	print "Processing $file in $input_dir ... \n";
    	
    	my $filepath = "$input_dir/$file";
    	
    	open(INPUTFILE, $filepath) or die "Unable to open $filepath\n";
    	
    	undef $/;
    	
    	my $content = <INPUTFILE>;
    	
    	close(INPUTFILE);
    	
    	my $parser = new XML::Simple;
    	
    	my $dom = $parser->XMLin($content, ForceArray => 1);
    	
    	my @output;
    	
    	foreach my $band(@{$dom->{"entry"}}) {
    		my $band_name = $band->{"band"}->[0];
    		
    		my $albums = $band->{"album"};
    		
    		my @albums;
    		
    		foreach my $album(@$albums) {
    			my $album_name = $album->{"name"}->[0];
    			my $chartposition =  $album->{"chartposition"}->[0];
    			
    			push @albums, {
    				"name" => $album_name,
    				"position" => $chartposition,
    			};
    		}
    		
    		push @output, {
    			"name" => $band_name,
    			"albums" => \@albums,
    		};
    		
    	} # foreach band
    	
    	return @output;
    }
    
    sub get_files {
    	my $input_dir = shift;
    	
    	unless(opendir(INPUTDIR, $input_dir)) {
    		die "\nUnable to open directory '$input_dir'\n";
    	}
    	
    	my @files = readdir(INPUTDIR);
    	
    	closedir(INPUTDIR);
    	
    	@files = grep(/\.xml$/i, @files);
    	
    	return @files;
    }
    
    sub checkusage {
    	my $opts = shift;
    	
    	my $r = $opts->{"r"};
    	my $d = $opts->{"d"};
    	
    	# Image a is optional; don't really need to refer to it here at all.
    	
    	# r is optional
    	# d is mandatory.
    	
    	unless(defined($d)) {
    		return 0;
    	}
    	
    	return 1;
    }
    
    sub usage {
    	print <<USAGE;
    	
    usage: perl main.pl <options>
    	-d <directory>	specify directory in which to find XML files.
    	-r run the program; process the files
    
    example usage:
    	# Process files in currect directory.
    	perl main.pl -d . -r
    	
    USAGE
    }
    
    main();
    

    T45. Deleting Data and Executing Dataless SQL Commands

    use strict;
    use warnings;
    use Data::Dumper;
    use Getopt::Std;
    use XML::Simple;
    
    use DBI;
    
    $| = 1;
    
    =pod
    
    	This is ACME XML parser version 1.0
    	Use with care.
    	
    =cut
    
    sub main {
    	my %opts;
    	
    	# Get command line options
    	getopts('d:r', \%opts);
    	
    	if(!checkusage(\%opts)) {
    		usage();
    		exit();
    	} 
    	
    	my $input_dir = $opts{"d"};
    
    	my @files = get_files($input_dir);
    	
    	my @data = process_files(\@files, $input_dir);
    	
    	add_to_database(\@data);
    	
    	print Dumper(@data);
    	
    	
    }
    
    sub add_to_database {
    	my $data = shift;
    	
    	my $dbh = DBI->connect("dbi:mysql:bands", "john", "letmein");
    	
    	unless(defined($dbh)) {
    		die "Cannot connect to database.\n";
    	}
    	
    	print "Connected to database.\n";
    	
    	my $sth = $dbh->prepare('insert into bands (name) values (?)');
    	
    	unless($sth) {
    		die "Error preparing SQL\n";
    	}
    	
    	$dbh->do('delete from bands') or die "Can't clean bands table.\n";
    	$dbh->do('delete from albums') or die "Can't clean bands table.\n";
    	
    	foreach my $data(@$data) {
    		my $band_name = $data->{"name"};
    		
    		print "Inserting $band_name into database ...\n";
    		
    		unless($sth->execute($band_name)) {
    			die "Error executing SQL\n";
    		}
    	}
    	
    	$sth->finish();
    	
    	$dbh->disconnect();
    	
    	print "Completed.\n";
    }
    
    sub process_files {
    	my ($files, $input_dir) = @_;
    	
    	my @data;
    	
    	foreach my $file(@$files) {
    		push @data, process_file($file, $input_dir);
    	}
    	
    	return @data;
    }
    
    sub process_file {
    	my ($file, $input_dir) = @_;
    	
    	print "Processing $file in $input_dir ... \n";
    	
    	my $filepath = "$input_dir/$file";
    	
    	open(INPUTFILE, $filepath) or die "Unable to open $filepath\n";
    	
    	undef $/;
    	
    	my $content = <INPUTFILE>;
    	
    	close(INPUTFILE);
    	
    	my $parser = new XML::Simple;
    	
    	my $dom = $parser->XMLin($content, ForceArray => 1);
    	
    	my @output;
    	
    	foreach my $band(@{$dom->{"entry"}}) {
    		my $band_name = $band->{"band"}->[0];
    		
    		my $albums = $band->{"album"};
    		
    		my @albums;
    		
    		foreach my $album(@$albums) {
    			my $album_name = $album->{"name"}->[0];
    			my $chartposition =  $album->{"chartposition"}->[0];
    			
    			push @albums, {
    				"name" => $album_name,
    				"position" => $chartposition,
    			};
    		}
    		
    		push @output, {
    			"name" => $band_name,
    			"albums" => \@albums,
    		};
    		
    	} # foreach band
    	
    	return @output;
    }
    
    sub get_files {
    	my $input_dir = shift;
    	
    	unless(opendir(INPUTDIR, $input_dir)) {
    		die "\nUnable to open directory '$input_dir'\n";
    	}
    	
    	my @files = readdir(INPUTDIR);
    	
    	closedir(INPUTDIR);
    	
    	@files = grep(/\.xml$/i, @files);
    	
    	return @files;
    }
    
    sub checkusage {
    	my $opts = shift;
    	
    	my $r = $opts->{"r"};
    	my $d = $opts->{"d"};
    	
    	# Image a is optional; don't really need to refer to it here at all.
    	
    	# r is optional
    	# d is mandatory.
    	
    	unless(defined($d)) {
    		return 0;
    	}
    	
    	return 1;
    }
    
    sub usage {
    	print <<USAGE;
    	
    usage: perl main.pl <options>
    	-d <directory>	specify directory in which to find XML files.
    	-r run the program; process the files
    
    example usage:
    	# Process files in currect directory.
    	perl main.pl -d . -r
    	
    USAGE
    }
    
    main();
    

    T46. Getting the IDs of Records You’ve Just Inserted

    use strict;
    use warnings;
    use Data::Dumper;
    use Getopt::Std;
    use XML::Simple;
    
    use DBI;
    
    $| = 1;
    
    =pod
    
    	This is ACME XML parser version 1.0
    	Use with care.
    	
    =cut
    
    sub main {
    	my %opts;
    	
    	# Get command line options
    	getopts('d:r', \%opts);
    	
    	if(!checkusage(\%opts)) {
    		usage();
    		exit();
    	} 
    	
    	my $input_dir = $opts{"d"};
    
    	my @files = get_files($input_dir);
    	
    	my @data = process_files(\@files, $input_dir);
    	
    	add_to_database(\@data);
    	
    	# print Dumper(@data);
    	
    	
    }
    
    sub add_to_database {
    	my $data = shift;
    	
    	my $dbh = DBI->connect("dbi:mysql:bands", "john", "letmein");
    	
    	unless(defined($dbh)) {
    		die "Cannot connect to database.\n";
    	}
    	
    	print "Connected to database.\n";
    	
    	my $sth_bands = $dbh->prepare('insert into bands (name) values (?)');
    	my $sth_albums = $dbh->prepare('insert into albums (name, position, band_id) values (?, ?, ?)');
    	
    	unless($sth_bands) {
    		die "Error preparing band insert SQL\n";
    	}
    	
    	unless($sth_albums) {
    		die "Error preparing album insert SQL\n";
    	}
    	
    	$dbh->do('delete from albums') or die "Can't clean bands table.\n";
    	$dbh->do('delete from bands') or die "Can't clean bands table.\n";
    	
    	foreach my $data(@$data) {
    		my $band_name = $data->{"name"};
    		my $albums = $data->{"albums"};
    		
    		print "Inserting $band_name into database ...\n";
    		
    		unless($sth_bands->execute($band_name)) {
    			die "Error executing SQL\n";
    		}
    		
    		my $band_id = $sth_bands->{'mysql_insertid'};
    		
    		foreach my $album(@$albums) {
    			my $album_name = $album->{"name"};
    			my $album_position = $album->{"position"};
    			
    			# print "$album_name, $album_position\n";
    			
    			unless($sth_albums->execute($album_name, $album_position, $band_id)) {
    				die "Unlable to execute albums insert.\n";
    			}
    		}	
    	}
    	
    	$sth_bands->finish();
    	$sth_albums->finish();
    	
    	$dbh->disconnect();
    	
    	print "Completed.\n";
    }
    
    sub process_files {
    	my ($files, $input_dir) = @_;
    	
    	my @data;
    	
    	foreach my $file(@$files) {
    		push @data, process_file($file, $input_dir);
    	}
    	
    	return @data;
    }
    
    sub process_file {
    	my ($file, $input_dir) = @_;
    	
    	print "Processing $file in $input_dir ... \n";
    	
    	my $filepath = "$input_dir/$file";
    	
    	open(INPUTFILE, $filepath) or die "Unable to open $filepath\n";
    	
    	undef $/;
    	
    	my $content = <INPUTFILE>;
    	
    	close(INPUTFILE);
    	
    	my $parser = new XML::Simple;
    	
    	my $dom = $parser->XMLin($content, ForceArray => 1);
    	
    	my @output;
    	
    	foreach my $band(@{$dom->{"entry"}}) {
    		my $band_name = $band->{"band"}->[0];
    		
    		my $albums = $band->{"album"};
    		
    		my @albums;
    		
    		foreach my $album(@$albums) {
    			my $album_name = $album->{"name"}->[0];
    			my $chartposition =  $album->{"chartposition"}->[0];
    			
    			push @albums, {
    				"name" => $album_name,
    				"position" => $chartposition,
    			};
    		}
    		
    		push @output, {
    			"name" => $band_name,
    			"albums" => \@albums,
    		};
    		
    	} # foreach band
    	
    	return @output;
    }
    
    sub get_files {
    	my $input_dir = shift;
    	
    	unless(opendir(INPUTDIR, $input_dir)) {
    		die "\nUnable to open directory '$input_dir'\n";
    	}
    	
    	my @files = readdir(INPUTDIR);
    	
    	closedir(INPUTDIR);
    	
    	@files = grep(/\.xml$/i, @files);
    	
    	return @files;
    }
    
    sub checkusage {
    	my $opts = shift;
    	
    	my $r = $opts->{"r"};
    	my $d = $opts->{"d"};
    	
    	# Image a is optional; don't really need to refer to it here at all.
    	
    	# r is optional
    	# d is mandatory.
    	
    	unless(defined($d)) {
    		return 0;
    	}
    	
    	return 1;
    }
    
    sub usage {
    	print <<USAGE;
    	
    usage: perl main.pl <options>
    	-d <directory>	specify directory in which to find XML files.
    	-r run the program; process the files
    
    example usage:
    	# Process files in currect directory.
    	perl main.pl -d . -r
    	
    USAGE
    }
    
    main();
    

    T47. Querying Databases

    use strict;
    use warnings;
    use Data::Dumper;
    use Getopt::Std;
    use XML::Simple;
    
    use DBI;
    
    $| = 1;
    
    =pod
    
    	This is ACME XML parser version 1.0
    	Use with care.
    	
    =cut
    
    sub main {
    	my %opts;
    
    	# Get command line options
    	getopts( 'i:e', \%opts );
    
    	if ( !checkusage( \%opts ) ) {
    		usage();
    		exit();
    	}
    
    	my $dbh = DBI->connect( "dbi:mysql:bands", "john", "letmein" );
    
    	unless ( defined($dbh) ) {
    		die "Cannot connect to database.\n";
    	}
    
    	print "Connected to database.\n";
    
    	if ( $opts{"i"} ) {
    		my $input_dir = $opts{"i"};
    
    		my @files = get_files($input_dir);
    		my @data = process_files( \@files, $input_dir );
    		
    		print "Found " . scalar(@files) . " files\n";
    
    		add_to_database( $dbh, \@data );
    	}
    
    	if ( $opts{"e"} ) {
    		export_from_database($dbh);
    	}
    
    	# print Dumper(@data);
    
    	$dbh->disconnect();
    
    	print "Completed.\n";
    }
    
    sub export_from_database {
    	my $dbh = shift;
    
    	print "Exporting ...\n";
    	
    	my $sql = 'select b.id as band_id, b.name as band_name, a.id as album_id, ' .
    		'a.name as album_name, a.position as position  ' .
    		'from bands b join albums a on a.band_id=b.id';
    	
    	my $sth = $dbh->prepare($sql);
    	
    	unless(defined($sth)) {
    		die "Unable to prepare export query.\n";
    	}
    	
    	unless($sth->execute()) {
    		die "Unable to execute export query.\n";
    	}
    	
    	while(my $row = $sth->fetchrow_hashref()) {
    		my $band_id = $row->{"band_id"};
    		my $band_name = $row->{"band_name"};
    		my $album_id = $row->{"album_id"};
    		my $album_name = $row->{"album_name"};
    		my $position = $row->{"position"};
    		
    		print "$band_id, $band_name, $album_id, $album_name, $position\n";
    		
    	}
    	
    	$sth->finish();
    	
    	
    }
    
    sub add_to_database {
    	my ( $dbh, $data ) = @_;
    
    	my $sth_bands  = $dbh->prepare('insert into bands (name) values (?)');
    	my $sth_albums = $dbh->prepare(
    		'insert into albums (name, position, band_id) values (?, ?, ?)');
    
    	unless ($sth_bands) {
    		die "Error preparing band insert SQL\n";
    	}
    
    	unless ($sth_albums) {
    		die "Error preparing album insert SQL\n";
    	}
    
    	$dbh->do('delete from albums') or die "Can't clean bands table.\n";
    	$dbh->do('delete from bands')  or die "Can't clean bands table.\n";
    
    	foreach my $data (@$data) {
    		my $band_name = $data->{"name"};
    		my $albums    = $data->{"albums"};
    
    		print "Inserting $band_name into database ...\n";
    
    		unless ( $sth_bands->execute($band_name) ) {
    			die "Error executing SQL\n";
    		}
    
    		my $band_id = $sth_bands->{'mysql_insertid'};
    
    		foreach my $album (@$albums) {
    			my $album_name     = $album->{"name"};
    			my $album_position = $album->{"position"};
    
    			# print "$album_name, $album_position\n";
    
    			unless (
    				$sth_albums->execute( $album_name, $album_position, $band_id ) )
    			{
    				die "Unlable to execute albums insert.\n";
    			}
    		}
    	}
    
    	$sth_bands->finish();
    	$sth_albums->finish();
    }
    
    sub process_files {
    	my ( $files, $input_dir ) = @_;
    
    	my @data;
    
    	foreach my $file (@$files) {
    		push @data, process_file( $file, $input_dir );
    	}
    
    	return @data;
    }
    
    sub process_file {
    	my ( $file, $input_dir ) = @_;
    
    	print "Processing $file in $input_dir ... \n";
    
    	my $filepath = "$input_dir/$file";
    
    	open( INPUTFILE, $filepath ) or die "Unable to open $filepath\n";
    
    	undef $/;
    
    	my $content = <INPUTFILE>;
    
    	close(INPUTFILE);
    
    	my $parser = new XML::Simple;
    
    	my $dom = $parser->XMLin( $content, ForceArray => 1 );
    
    	my @output;
    
    	foreach my $band ( @{ $dom->{"entry"} } ) {
    		my $band_name = $band->{"band"}->[0];
    
    		my $albums = $band->{"album"};
    
    		my @albums;
    
    		foreach my $album (@$albums) {
    			my $album_name    = $album->{"name"}->[0];
    			my $chartposition = $album->{"chartposition"}->[0];
    
    			push @albums,
    			  {
    				"name"     => $album_name,
    				"position" => $chartposition,
    			  };
    		}
    
    		push @output,
    		  {
    			"name"   => $band_name,
    			"albums" => \@albums,
    		  };
    
    	}    # foreach band
    
    	return @output;
    }
    
    sub get_files {
    	my $input_dir = shift;
    
    	unless ( opendir( INPUTDIR, $input_dir ) ) {
    		die "\nUnable to open directory '$input_dir'\n";
    	}
    
    	my @files = readdir(INPUTDIR);
    
    	closedir(INPUTDIR);
    
    	@files = grep( /\.xml$/i, @files );
    
    	return @files;
    }
    
    sub checkusage {
    	my $opts = shift;
    
    	my $i = $opts->{"i"};
    	my $e = $opts->{"e"};
    
    	unless ( defined($i) or defined($e) ) {
    		return 0;
    	}
    
    	return 1;
    }
    
    sub usage {
    	print <<USAGE;
    	
    usage: perl main.pl <options>
    	-i <directory>	import data; specify directory in which to find XML files.
    	-e export data from database
    
    example usage:
    	# Process files in currect directory.
    	perl main.pl -i . 
    	perl main.pl -e
    	
    USAGE
    }
    
    main();
    

    T48. Exporting Data

    use strict;
    use warnings;
    use Data::Dumper;
    use Getopt::Std;
    use XML::Simple;
    
    use DBI;
    
    $| = 1;
    
    =pod
    
    	This is ACME XML parser version 1.0
    	Use with care.
    	
    =cut
    
    sub main {
    	my %opts;
    
    	# Get command line options
    	getopts( 'i:e', \%opts );
    
    	if ( !checkusage( \%opts ) ) {
    		usage();
    		exit();
    	}
    
    	my $dbh = DBI->connect( "dbi:mysql:bands", "john", "letmein" );
    
    	unless ( defined($dbh) ) {
    		die "Cannot connect to database.\n";
    	}
    
    	print "Connected to database.\n";
    
    	if ( $opts{"i"} ) {
    		my $input_dir = $opts{"i"};
    
    		my @files = get_files($input_dir);
    		my @data = process_files( \@files, $input_dir );
    		
    		print "Found " . scalar(@files) . " files\n";
    
    		add_to_database( $dbh, \@data );
    	}
    
    	if ( $opts{"e"} ) {
    		export_from_database($dbh);
    	}
    
    	# print Dumper(@data);
    
    	$dbh->disconnect();
    
    	print "Completed.\n";
    }
    
    sub export_from_database {
    	my $dbh = shift;
    
    	print "Exporting ...\n";
    	
    	my $output_file = "output.txt";
    	
    	open OUTPUT, '>'.$output_file or die "Cannot create output file $output_file.\n";
    	
    	my $sql = 'select b.id as band_id, b.name as band_name, a.id as album_id, ' .
    		'a.name as album_name, a.position as position  ' .
    		'from bands b join albums a on a.band_id=b.id';
    	
    	my $sth = $dbh->prepare($sql);
    	
    	unless(defined($sth)) {
    		die "Unable to prepare export query.\n";
    	}
    	
    	unless($sth->execute()) {
    		die "Unable to execute export query.\n";
    	}
    	
    	while(my $row = $sth->fetchrow_hashref()) {
    		my $band_id = $row->{"band_id"};
    		my $band_name = $row->{"band_name"};
    		my $album_id = $row->{"album_id"};
    		my $album_name = $row->{"album_name"};
    		my $position = $row->{"position"};
    		
    		print OUTPUT "$band_id, $band_name, $album_id, $album_name, $position\n";
    	}
    	
    	$sth->finish();
    	
    	print "Export completed to $output_file\n";
    	
    	close OUTPUT;
    }
    
    sub add_to_database {
    	my ( $dbh, $data ) = @_;
    
    	my $sth_bands  = $dbh->prepare('insert into bands (name) values (?)');
    	my $sth_albums = $dbh->prepare(
    		'insert into albums (name, position, band_id) values (?, ?, ?)');
    
    	unless ($sth_bands) {
    		die "Error preparing band insert SQL\n";
    	}
    
    	unless ($sth_albums) {
    		die "Error preparing album insert SQL\n";
    	}
    
    	$dbh->do('delete from albums') or die "Can't clean bands table.\n";
    	$dbh->do('delete from bands')  or die "Can't clean bands table.\n";
    
    	foreach my $data (@$data) {
    		my $band_name = $data->{"name"};
    		my $albums    = $data->{"albums"};
    
    		print "Inserting $band_name into database ...\n";
    
    		unless ( $sth_bands->execute($band_name) ) {
    			die "Error executing SQL\n";
    		}
    
    		my $band_id = $sth_bands->{'mysql_insertid'};
    
    		foreach my $album (@$albums) {
    			my $album_name     = $album->{"name"};
    			my $album_position = $album->{"position"};
    
    			# print "$album_name, $album_position\n";
    
    			unless (
    				$sth_albums->execute( $album_name, $album_position, $band_id ) )
    			{
    				die "Unlable to execute albums insert.\n";
    			}
    		}
    	}
    
    	$sth_bands->finish();
    	$sth_albums->finish();
    }
    
    sub process_files {
    	my ( $files, $input_dir ) = @_;
    
    	my @data;
    
    	foreach my $file (@$files) {
    		push @data, process_file( $file, $input_dir );
    	}
    
    	return @data;
    }
    
    sub process_file {
    	my ( $file, $input_dir ) = @_;
    
    	print "Processing $file in $input_dir ... \n";
    
    	my $filepath = "$input_dir/$file";
    
    	open( INPUTFILE, $filepath ) or die "Unable to open $filepath\n";
    
    	undef $/;
    
    	my $content = <INPUTFILE>;
    
    	close(INPUTFILE);
    
    	my $parser = new XML::Simple;
    
    	my $dom = $parser->XMLin( $content, ForceArray => 1 );
    
    	my @output;
    
    	foreach my $band ( @{ $dom->{"entry"} } ) {
    		my $band_name = $band->{"band"}->[0];
    
    		my $albums = $band->{"album"};
    
    		my @albums;
    
    		foreach my $album (@$albums) {
    			my $album_name    = $album->{"name"}->[0];
    			my $chartposition = $album->{"chartposition"}->[0];
    
    			push @albums,
    			  {
    				"name"     => $album_name,
    				"position" => $chartposition,
    			  };
    		}
    
    		push @output,
    		  {
    			"name"   => $band_name,
    			"albums" => \@albums,
    		  };
    
    	}    # foreach band
    
    	return @output;
    }
    
    sub get_files {
    	my $input_dir = shift;
    
    	unless ( opendir( INPUTDIR, $input_dir ) ) {
    		die "\nUnable to open directory '$input_dir'\n";
    	}
    
    	my @files = readdir(INPUTDIR);
    
    	closedir(INPUTDIR);
    
    	@files = grep( /\.xml$/i, @files );
    
    	return @files;
    }
    
    sub checkusage {
    	my $opts = shift;
    
    	my $i = $opts->{"i"};
    	my $e = $opts->{"e"};
    
    	unless ( defined($i) or defined($e) ) {
    		return 0;
    	}
    
    	return 1;
    }
    
    sub usage {
    	print <<USAGE;
    	
    usage: perl main.pl <options>
    	-i <directory>	import data; specify directory in which to find XML files.
    	-e export data from database
    
    example usage:
    	# Process files in currect directory.
    	perl main.pl -i . 
    	perl main.pl -e
    	
    USAGE
    }
    
    main();
    

    Section 8: Modules and OO Perl

    T52. Modules

    # main.pl
    use strict;
    use warnings;
    use Data::Dumper;
    
    use Speak qw(test greet);
    
    $|=1;
    
    sub main {
    	#Speak::test();
    	
    	test();
    	greet();
    	
    	#my @dogs = qw(retriever labrador alsatian);
    	#print Dumper(@dogs);
    }
    
    main();
    
    # Speak.pm
    package Speak;
    
    use Exporter qw(import);
    
    @EXPORT_OK = qw(test greet);
    # @EXPORT = qw(test);
    
    sub test {
    	print "Hello there.\n";
    }
    
    sub greet {
    	print "Hey, how's it goin?\n";
    }
    
    1;
    

    T53. Packages and Directories

    # main.pl
    use strict;
    use warnings;
    use Data::Dumper;
    
    use lib '/Users/johnwpurcell/Documents/work/perl/projects/modules';
    
    use Communication::Speak qw(test greet);
    
    $|=1;
    
    sub main {
    	test();
    	greet();
    }
    
    main();
    
    # Speak.pm
    package Communication::Speak;
    
    use Exporter qw(import);
    
    @EXPORT_OK = qw(test greet);
    
    sub test {
    	print "Hello there.\n";
    }
    
    sub greet {
    	print "Hey, how's it goin?\n";
    }
    
    1;
    

    T55. Implementing OO in Perl

    # main.pl
    use strict;
    use warnings;
    
    use Data::Person;
    
    $|=1;
    
    sub main {
    	
    	my $person1 = new Data::Person("Bob", 45);
    	$person1->greet("Sue");
    	
    	my $person2 = new Data::Person("Mike", 55);
    	$person2->greet("Rogriguez");
    }
    
    main();
    
    # Person.pm
    package Data::Person;
    
    sub new {
    	my $class = shift;
    	
    	my $self = {
    		"name" => shift,
    		"age" => shift,
    	};
    	
    	bless($self, $class);
    	
    	return $self;
    }
    
    sub greet {
    	my ($self, $other) = @_;
    	
    	print "Hello $other; my name is " . $self->{"name"} . "; I am " . $self->{"age"} . " years old.\n";
    }
    
    1;
    

    Section 9: Web Application Basics

    57. A Hello World Web App

    #!/opt/local/bin/perl
    
    use strict;
    use warnings;
    
    sub main {
    	print "Content-type: text/html\n\n";
    
    	print "Hello world";
    
    }
    
    main();
    

    T59. Using URL Parameters

    #!/opt/local/bin/perl
    
    use strict;
    use warnings;
    
    use CGI;
    
    my $CGI = new CGI();
    
    sub main {
    	print $CGI->header();
    
    	my $user = $CGI->param("user");
    	my $password = $CGI->param("pass");
    
    print<<HTML;
    	<html>
    	<b>Hello world</b>
    	User: $user, Pass: $password
    	</html>
    
    HTML
    
    }
    
    main();
    

    T60. Website Forms

    #!/opt/local/bin/perl
    
    use strict;
    use warnings;
    
    use CGI;
    
    my $CGI = new CGI();
    
    sub main {
    	print $CGI->header();
    
    	my @query = $CGI->param();
    
    	@query = map($_ . ": " . $CGI->param($_), @query);
    
    	my $query = join(', ', @query);
    
    print<<HTML;
    	<html>
    
    	<form action="test4.cgi" method="post">
    	<input type="text" name="query" />
    	<input type="hidden" name="go" value="true" />
    	<input type="submit" name="submit" value="Go" /> 
    
    	</form>
    
    	<p>Last submitted: $query</p>
    	
    	</html>
    
    HTML
    
    }
    
    main();
    

    Section 10: Basic Sysadmin Tasks

    T61. Moving, Copying and Deleting Tasks

    use strict;
    use warnings;
    
    use File::Copy;
    
    $|=1;
    
    sub main {
    	if(move( 
    	'/Users/johnwpurcell/Documents/work/perl/projects/Tutorial61 - Moving and Copying Files/logo.png', 'logo2.png')){
    		print "One file moved.\n";
    	}
    	else {
    		print "Unable to move file\n";
    	}
    	
    	unlink('logo2.png');
    }
    
    main();
    

    T62. Executing System Commands

    use strict;
    use warnings;
    
    $|=1;
    
    sub main {
    	
    	my $command = 'cd ..; ls -l';
    	my @output = `$command`;
    	
    	print join('', @output);
    }
    
    main();
    

    T67. References to Hashes and Arrays Review

    use strict;
    use warnings;
    
    $|=1;
    
    sub main {
    	
    	my @fruits = ("apple", "banana", "orange");
    	
    	my %months = (
    		"Jan" => 1,
    		"Feb" => 2,
    	);
    	
    	print $fruits[0] . "\n";
    	$fruits[3] = "kiwi";
    	
    	print $months{"Jan"}. "\n";	
    	$months{"Mar"} = 3;
    	
    	my $fruits_ref = \@fruits;
    	print $fruits_ref->[0] . "\n";
    	
    	my $months_ref = \%months;
    	print $months_ref->{"Jan"}. "\n";	
    	
    	foreach my $fruit(@$fruits_ref) {
    		print "$fruit\n";
    	}
    	# while( my ($key, $value) = each %{$months_ref})
    	# curly brackets are optional
    	while( my ($key, $value) = each %$months_ref) {
    		print "$key - $value\n";
    	}
    	
    }
    
    main();
    
    展开全文
  • It turns out that --原来,竟然 unlike --表不同; Besides,表补充; Despite,表让步; Throughout:表范围,普及;贯穿 restricted to B,B表示对前者的限定 equal to 表示有能力...recall,forget doing sth:过
  • To doing

    2013-06-02 23:03:19
    1. look forward to doing 期待做某 2. object to 反对,抗议  If she does not object to it, why should we? 3. be used to 习惯于  The paper then considers whether antitrust law can be used to ...
  • 大英竞赛复习篇

    2021-03-17 14:16:41
    给xx诊断 undertake: 承接xx, to make yourself responsible for sth and start doing it grant (sth to sb/sth): to agree to give sb what they ask for relaim (sth from sb/sth): to get sth back from sb/sth ...
  • highlight的用法

    千次阅读 2020-12-30 10:01:45
    Vue.use(hljs.vuePlugin); 使用 <div id="app"> <!-- bind to a data property named `code` --> <highlightjs autodetect :code="code" /> </div> export default { data
  • use imagination0

    2018-09-17 10:44:00
    1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main(void) 5 { 6 FILE *fp; 7 /* char arr[100]; */ 8 9 //write sth to T.txt 10 fp = fopen("T.t...
  • accept的用法与搭配是什么

    千次阅读 2021-01-13 08:30:31
    accept的用法有accept sth (as sth),accept sb (into sth) ,accept sb (as sth)等。动词accept的用法1.表示“接受”,可及物或不及物。如:She offered him a lift and he accepted (it). 她请他坐她的车,他就...
  • 动词 + to do OR + doing

    2010-03-08 17:50:43
    下列动词或词组后面都可以接doing: admit 承认 appreciate 感激 avoid 避免 complete完成 consider认为 delay 耽误 deny 否认 detest 讨厌 endure 忍受 enjoy 喜欢 es...
  • 下载 笔记版/无笔记版 pdf资料: GitHub - zhbink/LiuLiYueDu: 流利阅读pdf汇总 本文内容全部来源于流利阅读...Turn down the volume: WHO takes aim at harmful smartphone use 调低音量:世卫组织对不健康的智能手...
  • some phrase for oral english

    千次阅读 2013-11-07 18:31:30
     let's find out by doing sthdoing sth来了解(发现,找出什么意思)   he is always a day late and a dollar short to everything.  a day late and a dollar short 特定短语,形容人 散漫又缺钱。 ...
  • phrases

    千次阅读 2013-12-05 17:03:56
    49. Do/try one’s best/spare no efforts to do/be committed to doing/be devoted to doing / work hard on sth 致力于 53. Beyond 超出…  The professor’s lecture is beyond me/beyond my understanding ...
  • ask asking, find finding, meet meeting write writing, use using, ride riding put putting, cut cutting, prefer preferring, refer referring lie, die, tie -> lying dying tying see seeing, agree agreeing...
  • Writing your own tcp_v4_rcv is essential for programmers who frequently customize TCP protocol, such as doing TCP optimizations. Let’s take a look at an example about how to do this: # include ...
  • 中考必备100句型

    2018-08-21 09:10:11
    中考必备100句型 welcome to sp欢迎到某地 ... What’s the matter with sb./ sth? 出什么毛病了?  Eg. What’s the matter with your watch?  3. be different from 与—不同  Eg. The weather in Bei...
  • 六级-写作

    2021-12-04 16:51:09
    提倡 argue for 支持 be in favor of 反对 object +(to sb/sth)~(to doing sth/to sb doing sth) (object是不及物动词) I really object to being charged for parking 我非常反对停车收费 argue against (argue...
  • 2.杨明翰英语教学系列之名词篇

    千次阅读 2019-10-24 07:00:09
    文章目录前言1. 名词概念2. 名词的分类2.1 普通名词(Common Nouns)2.1.1 `可数名词(Countable Nouns)`2.1.1.1 个体名词(Individual Nouns)2.1.1.2 集体名词(Collective Nouns)2.1.2 不可数名词(Uncountable Nouns)...
  • 四级常用100词组

    千次阅读 2011-12-06 22:16:11
    56. make an attempt at doing sth. (to do sth.) 试图做… 57. attend to (=give one’s attention, care and thought)注意,照顾;attend on(upon)(=wait upon, serve, look after) 侍候,照料 58. ...
  • Currrently I'm working on sth like calculating how many hits on a page. The problem is the raw data can be huge, so it may not scale if you use RDBMS. The raw input is as follows. Date Page Use....
  • 管理类联考-英语: 前导( 二 )

    千次阅读 2019-02-13 18:02:05
    (A)discourage students from doing homework  (B)result in students’ indifference to their report cards  (C)undermine the authority of state tests  (D)restrict teachers’ power in education   ...
  • 英语语法之动词时态

    千次阅读 2011-11-29 12:49:33
    进行 was were doing am is are doing will shall be doing  完成 had done have has done will shall have done should would have done用于虚拟语气   完成进行 had been doing have has ...
  • 非谓语

    2021-03-27 09:50:03
    判断考点: 非谓语动词:句中成分分析,再结合选项。 1.找标志词: 2. 寻找逻辑主语,判断主动被动:...enjoy +doing be busy +doing put off +doing give up |+doing decide to do seem(似乎,好像(半系动词)) +to

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 823
精华内容 329
关键字:

doingsthsthuse