LinuxQuestions.org
Review your favorite Linux distribution.
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 07-03-2003, 11:05 AM   #1
realos
Member
 
Registered: Jul 2002
Location: Germany
Distribution: Redhat 7.3, Debian 3.1, Knoppix 3.1, Ubuntu 6.10
Posts: 113

Rep: Reputation: 15
question about $_ (default variable) in Perl


--------------
1 $! /usr/bin/perl -w
2 print "Your name please: ";
3 $_=chomp($name =<>);
4 print "hello $name! $_ \n';
---------------------------------

This code works fine in this shape but if I leave out "$_" in line 3 then there is a warning message while executing the file: "Use of uninitialized value in concatenation (.) or string at ./hello.pl line 4, <> line 1."

I thought $_ is the default variable and would store the return value of chomp function. Any idea why it is not doing that in our example?
 
Old 07-03-2003, 12:02 PM   #2
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
The reason it's giving you the error is because chomp returns the number of characters removed, and in your assignment, you're telling perl to make $_ be that number, but if you remove it, the return value of chomp is discarded. $_ is only the default going IN to chomp, not out.

So, the value of $_ is undefined.

If you're wanting to assign the chomped version of $name to $_, you could say:
Code:
chomp($_ = <STDIN>);
and avoid the '$name' variable altogether, or
Code:
$_ = <STDIN>;
chomp;
to use the $_ option.

Hope this helps!
 
Old 07-03-2003, 12:23 PM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
surely just avoiding $name altogether would actually imply nothjing more than "chomp(<>);"

i'm assuing the first line of that script is a typo.. $! instead of a normal shebang (#!)
 
Old 07-04-2003, 07:12 AM   #4
realos
Member
 
Registered: Jul 2002
Location: Germany
Distribution: Redhat 7.3, Debian 3.1, Knoppix 3.1, Ubuntu 6.10
Posts: 113

Original Poster
Rep: Reputation: 15
thank you people. Both the posts help me further. $! instead of #! _was_ actually a mistake. I did not copy and paste but had to write down the code because of not proper settings in cygwin I am using to have a Linux-console at workplace.


perldoc -f chomp does say "... it the VARIABLE is omitted, it chomps $_."

I just overlooked that.

thankx again
 
Old 07-04-2003, 06:09 PM   #5
Salz
LQ Newbie
 
Registered: Jun 2003
Distribution: Debian
Posts: 23

Rep: Reputation: 15
chomp <> won't work, because chomp expects a lvalue it can modify and not an operator. Neither would <>;chomp because the <> operator result is only assigned to $_ inside a while test.
$_ is the default variable, but just not for everything
See man perlvar for further confusions.
 
  


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
Tainted variable problem (PERL) rajatgarg Programming 1 07-29-2004 02:54 PM
Perl variable string search ugenn Programming 1 05-07-2004 09:19 PM
Perl Message board variable cadj Programming 3 04-21-2004 07:23 AM
Perl question re arrays/variable names Jon- Linux - Software 1 09-19-2003 11:29 AM
Perl: system() mail losing a variable value m0rl0ck Programming 8 09-09-2003 07:40 PM

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

All times are GMT -5. The time now is 03:34 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