LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-22-2011, 03:48 PM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
perl one liner error


for some reason i can't get this to work

Code:
perl –e ‘$_ = “The quick brown fox”; print scalar(split),”\n”;’
I get this error every time
Code:
bash: syntax error near unexpected token `('
 
Old 08-22-2011, 03:58 PM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Starting with the bare basics:
Code:
$ perl -e '$_ = qq(The foxy fox) ; print'
What is it you're trying to do from there? Looks like you want to split the $_ scalar (into a list), then force it back into a scalar context, in order to print it..?

What gives?
 
Old 08-22-2011, 04:00 PM   #3
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,808

Rep: Reputation: 604Reputation: 604Reputation: 604Reputation: 604Reputation: 604Reputation: 604
Stop using "smart" quotes and hyphens.

This works:
Code:
perl -e '$_ = "The quick brown fox"; print scalar(split),"\n";'
‘’ is completely different than two 's, and wrong
“” is completely different than two "s, and wrong
The "–" you have is actually not the same as a "-". It is an EN DASH (E2 80 93) rather than a hyphen (2D).
 
Old 08-22-2011, 04:01 PM   #4
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
does the split function default to a space when nothing is specified, and doesn't the scalar function return the number of fields in an array?
 
Old 08-22-2011, 04:03 PM   #5
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Good catch about the bad characters.

@casperdaghost: what editor are you using to write your Perl?
 
Old 08-22-2011, 04:07 PM   #6
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Quote:
Originally Posted by casperdaghost
does the split function default to a space when nothing is specified, and doesn't the scalar function return the number of fields in an array?
Technically, split() defaults to whitespace, not just "a space".

If all you want is a word count, how about:
Code:
$ perl -e '@sentence = qw(The foxy fox) ; print @sentence . "\n" ;'
 
Old 08-24-2011, 01:16 PM   #7
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
Hey guys - I picked up that one liner from a tutorial pasted on a word doc. I cut and pasted it into a terminal, and it would not work. It drove me crazy because I was pretty sure what the result would be, but it was erring out. Really, really astute observation AlucardZero - and a good lesson for me.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Updating PERL or any package depending on PERL gives error message Guus Bonnema Fedora - Installation 3 07-26-2011 08:46 AM
awk one liner help niknak Linux - Newbie 1 05-07-2009 05:52 AM
IP Forward one-liner? JoeBleaux Linux - Networking 1 04-27-2009 11:24 AM
Share your greatest one-liner!! PenguinPwrdBox General 3 08-20-2005 11:45 PM
Yum error: .conflict between perl and perl-NDBM_File zepplin611 Red Hat 3 10-20-2004 11:22 AM


All times are GMT -5. The time now is 10:28 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration