`

Note of Learning Perl--I/O Basics

    博客分类:
  • Perl
阅读更多

I/O Basics
-------------

1. Input from Standard Input
 1) The difference is under the hood. In the while loop, Perl reads a line of input, puts it into a variable, and runs the body of the loop. Then, it goes back to find another line of input. But in the foreach loop, the line-input operator is being used in a list context (since foreach needs a list to iterate through). So it has to read all of the input before the loop can start running.
 
2. Input from the Diamond Operator
 1) Since the diamond operator is generally being used to process all of the input, it's typically a mistake to use it in more than one place in your program. If you find yourself putting two diamonds into the same program, especially using the second diamond inside the while loop that is reading from the first one, it's almost certainly not going to do what you would like. If you re-initialize @ARGV before using the second diamond, then you're on solid ground.
 2) If the diamond operator can't open one of the files and read from it, it'll print an allegedly helpful diagnostic message.such as: can't open wimla: No such file or directory. The diamond operator will then go on to the next file automatically, much like what you'd expect from cat or another standard utility.
 
3. The Invocation Arguments
 1) This is how the diamond operator knows what filenames it should use: it looks in @ARGV. If it finds an empty list, it uses the standard input stream; otherwise it uses the list of files that it finds. This means that after your program starts and before you start using the diamond, you've got a chance to tinker with @ARGV. For example, here we can process three specific files, regardless of what the user chose on the command line:
  @ARGV = qw# larry moe curly #;  # force these three files to be read
  while (<>) {
    chomp;
    print "It was $_ that I saw in some stooge-like file!\n";
  }
 
4. Output to Standard Output
 1) A rule in Perl is that if the invocation of print looks like a function call, then it is a function call.
  print (2+3);
  That looks like a function call, so it is a function call. It prints 5, but then it returns a value like any other function. The return value of print is a true or false value, indicating the success of the print. It nearly always succeeds, unless you get some I/O error, so the $result in the following statement will normally be 1:
  $result = print("hello world!\n");
  But what if you used the result in some other way? Let's suppose you decide to multiply the return value times four:
  print (2+3)*4;  # Oops!
  When Perl sees this line of code, it prints 5, just as you asked. Then it takes the return value from print, which is 1, and multiplies that times 4. It then throws away the product, wondering why you didn't tell it to do something else with it.
 2) Actually, this rule -- "If it looks like a function call, it is a function call" -- applies to all list functions.

分享到:
评论

相关推荐

    Time-of-Flight-Basics-Application-Note-Melexis.pdf

    of-flight basics 时间飞行原理(Time-of-Flight, TOF)是一种用于测量物体距离的技术,广泛应用于各种领域,如深度感应、3D成像、安全系统等。本应用笔记将深入探讨TOF的基本概念,包括其工作原理、关键组件、性能...

    assignment--dom-manipulations-basics

    mkdir assignment--dom-manipulations-basics cd assignment--dom-manipulations-basics 3-下载.zip文件并解压缩: curl ...

    Jason-Brownlee-deep_learning_with_python(pdf + sourcecode v1.15 2018).zip

    Tap into their power in a few lines of code using Keras, the best-of-breed applied deep learning library. In this mega Ebook is written in the friendly Machine Learning Mastery style that you’re ...

    deep-learning-basics-master

    基于Pytorch架构的一些深度学习基础项目,包括通用功能,核心训练模块,以及图像生成,分割,超分等模型

    家用网络搭建与优化.docx

    - [Home Networking Basics](https://www.cnet.com/how-to/home-networking-basics/) - CNET提供的家用网络基础知识。 2. **无线路由器设置**: - 路由器用户手册 - 通常附带路由器,详细介绍了如何设置和配置...

    《机器学习实战:基于Scikit-Learn、Keras和TensorFlow》第3版,pdf+代码

    该书《Hands_On_Machine_Learning_...该书是tensorflow官方网站的推荐图书之一(https://tensorflow.google.cn/resources/learn-ml/basics-of-machine-learning/?hl=zh-tw)本资源不仅有pdf,而且有配套的代码和数据。

    windows编程,设计6

    - [动画基础](https://docs.microsoft.com/zh-cn/windows/uwp/controls-and-patterns/animation-basics) - [缓动函数](https://docs.microsoft.com/zh-cn/windows/uwp/controls-and-patterns/easing-functions) ###...

    Prototype-Basics:Devleague 原型基础知识 2015

    // ---- * 读我 * ---- // // --- [ Array.prototype ] --- // 1 . 在 Array.prototype 上编写一系列函数来实现以下功能。 [toString] - 此方法必须获取数组的内容并返回该数组内容的字符串,但应检查函数和 null 并...

    01_the-basics-of-convnets_exam.html

    01_the-basics-of-convnets_exam.html

    0591-basics-of-computer-networking_computer_computernetworking_c

    BASICS OF COMPUTER SCIENCE

    Learning Perl (7th Edition)

    Learning Perl teaches you the basics and shows you how to write programs up to 128 lines long—roughly the size of 90% of the Perl programs in use today. Each chapter includes exercises to help you ...

    Basics of Linear Algebra for Machine Learning (Python)

    Basics of Linear Algebra for Machine Learning: Discover the Mathematical Language of Data in Python By 作者: Jason Brownlee Pub Date: 2018 ISBN: n/a Pages: 212 Language: English Format: PDF Linear ...

    math-basics.rar

    链接: http://www.cis.upenn.edu/~jean/math-basics.pdf 这本书讲了什么? 这本书的主要目的是介绍线性代数和最优化理论的基础知识以及这些知识在机器学习、机器人学、计算机视觉等领域的应用。 该书包含以下 10...

    simplify.thatsh.it:简化.thatsh.it

    简化它要遵循的文档...参考 ...detection-javascript-canvashttp://kevinmehall.net/2009/line-and-shape-recognitionhttp://www.createjs.com/#!/EaselJS/ ?

    Basics of Linear Algebra for Machine Learning

    Linear algebra is a pillar of machine learning. You cannot develop a deep understanding and application of machine learning without it. In this new laser-focused Ebook written in the friendly Machine ...

Global site tag (gtag.js) - Google Analytics