Find the answer to your Linux question:
Results 1 to 6 of 6
Hi folks, I am sorry for asking this, for sure the stupidest question ever made in this forum. I am trying to learn Perl to manipulate text files produced and ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! hernandeangelis's Avatar
    Join Date
    Mar 2005
    Location
    Planet Earth
    Posts
    79

    Newbie question about Perl


    Hi folks,

    I am sorry for asking this, for sure the stupidest question ever made in this forum. I am trying to learn Perl to manipulate text files produced and used by several programs. I must also say that I am very new to linux. I have perl 5.8.3 running in SuSE 9.1 personal. In my computer Perl is in /usr/bin/perl, as reported by "which". Ok, I wrote a small "Hello World" program just to begin:

    #! /usr/bin/perl
    print "Hello, world";

    saved it on a file named xxx.pl, made it executable and then run in the konsole:

    perl xxx.pl

    and what happens is that now my prompt is:

    Hello, worldhernan@linux:~/Documents/sangra>

    that is, the output of the program appears in the promt.

    Do you have any advice?

    Thanks

    Hernan

  2. #2
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Whats happening is that your perl program isn't putting out a newline, so your prompt is following straight after the 'd' of "world". Change your script to
    Code:
    print "Hello, world\n";
    and see the difference!

  3. #3
    Just Joined! hernandeangelis's Avatar
    Join Date
    Mar 2005
    Location
    Planet Earth
    Posts
    79
    Thanks scm!!!

    Hernan

  4. #4
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by hernandeangelis
    Thanks scm!!!
    Whenever you see Linux/UNIX doing something apparently bizarre, there's usually a logical (if obscure!) reason for it. I once saw a student try to join two files with the command
    Code:
    cat file1 file2 >file2
    and then complain loudly that it didn't work (but not as loudly as we complained that he'd filled our disk!). What happens here is that the shell truncates file2, then invokes cat with file1 and file 2 as arguments. cat starts by copying file1 into the now empty file2, and then opens and starts reading file2, appending each bit it reads to the end of file2, so it's in an infinite write loop that only ends when there's no more space on the disk. The student, to his discredit, could never grasp the concept, and insisted that cat should have taken both files, joined them together, and then stored the result into file2. He didn't understand the "KISS" principles behind the design of UNIX - a program should do something simple, but do it well. This then allows scope for "interesting" pipelines of commands to do more complicated stuff.

    Steve

  5. #5
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    Perhaps the student did not understand that cat and the shell are
    separate programmes.The shell handles the redirection so cat does not know
    that its output has been redirected and in particular that it has been redirected
    to a file which is also its input.
    On the other hand the shell does not know what cat does.In particular
    it does not know that cat is going to treat the argument file2 before the <
    as a file and open it.

    The end result is that neither the shell nor cat can realize that cat file1 file2 >file2
    is likely to cause problems.

  6. #6
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by Santa's little helper
    The end result is that neither the shell nor cat can realize that cat file1 file2 >file2
    is likely to cause problems.
    That's right, the operator has to know what he's doing. As Doug Gwyn once observed: "UNIX was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever things."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •