LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-17-2005, 08:57 PM   #1
skibud2
Member
 
Registered: Sep 2003
Location: Waltham MA
Distribution: Mandriva 2007 w/ KDE
Posts: 87

Rep: Reputation: 15
Simple Pearl Question


I think it will be easier for me to show you all what I am trying to do rather than trying to explain it, so here it is:

echo "hello world" | perl -e 'print $ARGV';

Why does this not work. I have been looking all over the web for a reason, but I have not found much documentation pertaining to using perl from the command line. So web resources would be useful to me too.

Thanks,

Mike
 
Old 03-17-2005, 09:23 PM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Well, what are you trying to do? Count arguments? $#ARGV. Look at arguments? @ARGV. $ARGV is set to the input file when reading from <>. In the case above, you are not reading from any file.
 
Old 03-18-2005, 02:56 AM   #3
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 31
I think this is a good question and it involves several important concepts that are specific to perl. So what you're trying to do is print "hello world." The mistake in your code is that you are confusing piping some input to the program and giving arguments on the command line. Also you have some confusion over the use of @ARGV, and scalar/list context.

So, what I'll do is post several ways to do what you want. Please investigate them! If you have any questions, please ask.

Code:
perl -e "print qq(hello world)"
echo "hello world" | perl -e "while (<>) { print }"
perl -e "print @ARGV" "hello world"
perl -e "$, = ' '; print @ARGV" hello world
perl -e "$, = ' '; print @ARGV" `echo "hello world"`
 
Old 03-18-2005, 07:24 AM   #4
skibud2
Member
 
Registered: Sep 2003
Location: Waltham MA
Distribution: Mandriva 2007 w/ KDE
Posts: 87

Original Poster
Rep: Reputation: 15
Puff -- Great answer. I will check out each method.
 
Old 03-18-2005, 07:29 AM   #5
skibud2
Member
 
Registered: Sep 2003
Location: Waltham MA
Distribution: Mandriva 2007 w/ KDE
Posts: 87

Original Poster
Rep: Reputation: 15
After browsing, is:

echo "hello world" | perl -e "while (<> ) { print }"

the same as

echo "hello world" | perl -e "while (<stdin> ) { print }"

It looks like it is, but I just want to confirm.
 
Old 03-18-2005, 09:35 AM   #6
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 31
Yes and no. From the camel book:

"When you read lines from <>, it magically gives you all the lines from all the files mentioned on the command line. If no files were mentioned, it gives you standard input instead, so your program is easy to insert into the middle of a pipeline of processes."
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Ubuntu Fluxbox simple question, simple answer? generallimptoes Linux - Software 3 09-26-2005 02:03 PM
PHP/Pearl/Java/ASP riluve Linux - Software 10 02-14-2005 01:12 PM
Installing Programs - A simple question from my simple mind jmp875 Linux - Newbie 6 02-18-2004 09:03 PM
simple question seeking simple answer enzo250gto Linux - Newbie 1 10-27-2001 04:08 AM
Can PHP replace Pearl for Linux scripts? 360 Programming 1 06-04-2001 10:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration