Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Noob question

by meanroc (Acolyte)
on Jan 20, 2014 at 15:24 UTC ( #1071328=perlquestion: print w/replies, xml ) Need Help??
meanroc has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am new in the site.. And in the perl too..And I am having some problems..when I code:

#!/usr/bin/per use strict; use warnings; my $currDir = `pwd`; chomp $currDir; print "$currDir";

And rum right, but print /rootroot@kali: , and I want to print:

/root root@kali:

So I tried using:

#!/usr/bin/per use strict; use warnings; my $currDir = `pwd`; chomp $currDir; print "$currDir\n";

But does not worked..So watch I do??

Replies are listed 'Best First'.
Re: Noob question
by toolic (Bishop) on Jan 20, 2014 at 15:43 UTC
Re: Noob question
by marto (Bishop) on Jan 20, 2014 at 15:48 UTC

    Welcome meanroc! If this is your code you will get an error which reads:

    /usr/bin/per bad interpreter: No such file or directory

    To resolve this change your shebang line to:


    Within perl there are more portable ways to get the current directory, for example the core module Cwd

    #!/usr/bin/perl use strict; use warnings; use Cwd qw(abs_path); my $path = abs_path(); print "Path: $path\n";
Re: Output runs into prompt
by hippo (Abbot) on Jan 20, 2014 at 16:16 UTC

    Previous answers (sensibly) recommending Cwd notwithstanding, your problem in the top example is that your output is running into your shell prompt for the following command (whatever that might be). Your chomp is removing the trailling newline that you might need to do this. You could therefore chomp after the print if that's the way you want to go about it.

    However, your second script works fine if we adjust the shebang:

    $ cat #!/usr/bin/perl use strict; use warnings; my $currDir = `pwd`; chomp $currDir; print "$currDir\n"; $ ./ /tmp $

    This suggests that the code you've posted may not be the code you are running.

Re: Noob question
by McA (Priest) on Jan 20, 2014 at 16:03 UTC

    A ++ for the proposal using getcwd. An addition:

    use strict; use warnings; use Cwd qw(getcwd); use Benchmark qw(:all); cmpthese(100000, { 'buildin' => sub { my $pwd = getcwd; }, 'backtick' => sub { my $pwd = `pwd`; chomp $pwd; }, });

    Output on my computer:

    $ perl (warning: too few iterations for a reliable count) Rate backtick buildin backtick 4093/s -- -100% buildin 1666667/s 40617% --

    Backtick operator spawns a subshell. This is expensive. So the rule of thumb should be: Before using backtick for gathering "simple" informations, check whether there is a Perl module providing this information.

    Best regards

Re: Noob question
by McA (Priest) on Jan 20, 2014 at 15:35 UTC


    what did not work?

    The shebang path is probably wrong.


Re: Noob question
by Anonymous Monk on Jan 20, 2014 at 16:06 UTC

    What you're doing right:

    use strict; use warnings;

    The above will save you all sorts of grief as your scripts grow beyond a couple lines. If you progress to the point where they get in your way, you can turn them off selectively, e.g. no warnings 'once'; around code that complains about global variables only being used once.

    Under the assumption that your command prompt is "root@kali:", your first script printed "/rootroot@kali:" because the Perl "print" does not append a newline to its output. So your perl script actually printed "/root", and then your shell printed "root@kali:".

    As for your second script: are you getting something like "Can't exec /usr/bin/per at line 1."? If so, you need to spell "perl" correctly in the first line of your script. Actually, as posted your first script should have done this also.

    The "Cwd" recommendation amounts to

    #!/usr/bin/perl use strict; use warnings; use Cwd; my $currDir = cwd; print "$currDir\n";
Re: Noob question
by robby_dobby (Friar) on Jan 21, 2014 at 04:20 UTC
    Welcome meanroc,

    Others have given you suggestions to use Cwd - so I'm just going to focus on something very general, perhaps off-topic. :-)

    I'm not sure if this is particularly true with the current crop of learners, but it has been bothering me enough for some time to write about it. Generally speaking, for any learning path that you studiously pursue, it's not possible for you to remain a newbie/beginner for a long time. In fact, you'll advance your knowledge that this newbie tag will soon be irrelevant and you may find yourself bemused/amused that you even posted this question. :-)

    So my advice to you is - do not identify yourself as a newbie/noob. We have all been there before and we have all gained our knowledge. If you identify yourself as one, you're only stunting your advancement. Bear this in mind and good luck with your learning path!

Re: Noob question
by meanroc (Acolyte) on Jan 20, 2014 at 16:36 UTC

    Please guys, read again, I modified..

      G'day meanroc,

      Welcome to the monastery.

      When you change a node, please indicate in that node what's been changed (a notification several screenfuls later is not particularly helpful). "How do I change/delete my post?" explains how and why.

      -- Ken

      Please guys, read again, I modified..

      Don't do that, please, the whole thread is now incomprehensible, with answers to things that are no longer there. Add an update of you wish to your original post, or add a new post, but don't modify the content of the original post.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1071328]
Approved by toolic
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2018-02-25 18:12 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (314 votes). Check out past polls.