ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello, i've recently begun learning perl and have came to an issue is where when I use a variable a new line is printed right after it...Example...
Code:
Quote:
#!/usr/bin/perl -w
#==========================================================
# Last updated: June 11, 2007
# NOTE TO SELF: ALL code blocks finish with ;
#==========================================================
######## Variables #######
my $myname = `whoami`;
####### Start of main script #######
print "Hello $myname, how are you doing today?\n";
Output from that code is as follows...
Hello Ike
, how are you doing today?
However, if I add an escape character before variable the string prints out as expected....
Quote:
#!/usr/bin/perl -w
#==========================================================
# Last updated: June 11, 2007
# NOTE TO SELF: ALL code blocks finish with ;
#==========================================================
#>>>>>>> Variables <<<<<<<
my $myname = `whoami`;
#>>>>>>> Start of main script <<<<<<<
print "Hello \$myname, how are you doing today?\n";
output:
Name "main::myname" used only once: possible typo at ./myperl.pl line 9.
Hello $myname, how are you doing today?
-end of output-
Can you help me find what it is that I am missing?
EDIT:
Left out the resource I am using for learning perl ...
site name: http://perldoc.perl.org/
First, I don't think the string prints out as expected when you escape the dollar sign, does it?
I mean, you want the result of whoami to be printed, but what you're getting is the string:
Quote:
$myname
itself.
So get rid of that escape character before the dollar sign, because you want the content of variable $myname, not the variable name itself.
Well, you want most of the content, anyway.
The result of the backtick quotes is to execute the command and place the output in variable $myname. But the output includes a line feed at the end. So you want to get rid of that.
Try this. I just tried it myself and it works just fine:
Code:
#!/usr/bin/perl
$myname=`whoami`;
chomp($myname);
print "Hello, $myname, how are you doing today?\n"
(I put the comma after Hello because my mother was an English teacher, and she brought me up right.)
Distribution: suse, opensuse, debian, others for testing
Posts: 307
Rep:
the output of $myname = `whoami`; contains the username + \n at the end !
that's why a new line is started.
you can get rid of it with a substitition based on "regular expressions" like this:
Code:
#!/usr/bin/perl -w
#==========================================================
# Last updated: June 11, 2007
# NOTE TO SELF: ALL code blocks finish with ;
#==========================================================
######## Variables #######
my $myname = `whoami`;
$myname =~ s/(\n)$//;
####### Start of main script #######
print "Hello $myname, how are you doing today?\n";
Just google for perl and regex to find more on this or take a look at "learning pearl" published by O'Reilly.
what the "$myname =~...." does is this:
substitute the (\n) controll sequence at the very end (indicatd by $) of the initial string with "nothing".
Isn't it funny how you stumble upon answers right after you ask a question....
Ok, so I found a function built into perl, chmop() that can do it for me. (URL: http://perldoc.perl.org/functions/chomp.html) However, is it possible for me to use it for all variables once defined?
the output of $myname = `whoami`; contains the username + \n at the end !
that's why a new line is started.
you can get rid of it with a substitition based on "regular expressions" like this:
Code:
#!/usr/bin/perl -w
#==========================================================
# Last updated: June 11, 2007
# NOTE TO SELF: ALL code blocks finish with ;
#==========================================================
######## Variables #######
my $myname = `whoami`;
$myname =~ s/(\n)$//;
####### Start of main script #######
print "Hello $myname, how are you doing today?\n";
Just google for perl and regex to find more on this or take a look at "learning pearl" published by O'Reilly.
what the "$myname =~...." does is this:
substitute the (\n) controll sequence at the very end (indicatd by $) of the initial string with "nothing".
Thanks rtspitz, I was typing my post when you must've posted yours. I will look into that feature as well.
Sorry, wjevans_7d1@yahoo.co , I was probably looking and typing when you were posting your post as well. Thank you for your input all.
However, is it possible for me to use it for all variables once defined?
As long as said variable is a scalar string, yes. Otherwise, you can call chomp() on it, but there won't be any effect. (In other words, chomping an array or hash does not change the object, chomping undef does nothing; the same goes for chomping a scalar number.)
Yeah, chomp() is safe and has Perl's DWIM (Do What I Mean) functionality that you'll find in a lot of Perl fns/operators.
Incidentally, chomping an array is handy if you want to process small files in mem, instead of reading from the disk & processing rec-by-rec. eg
open(FILE"<yourfile") or die "open failed: $!\n";
@file_recs = <FILE>;
close(FILE) or die "close failed: $!\n";
chomp(@file_recs);
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.