LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 12-06-2006, 05:24 AM   #1
noir911
Member
 
Registered: Apr 2004
Posts: 682

Rep: Reputation: Disabled
[Perl] chdir and redirection


I wrote this very basic Perl code but it doesn't change the directory nor does it redirect output to the files. Can anyone tell me what I'm doing wrong?

Code:
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
my $home="/home/me/coding/";
chdir "$home" or die "cannot change directory: $!";
open STDOUT, ">ls.out" or die "can't write to ls.out: $!";
open STDERR, ">ls.err" or die "can't write to ls.err: $!";
system 'ls -al $home';
 
Old 12-06-2006, 05:04 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
For a start you'll need doublequotes ("), not single quotes (').
Note also that, like bash, it effectively starts a subshell/env, so when it terminates, the calling shell will still be in the orig dir you ran the prog from.
See also: http://perldoc.perl.org/functions/system.html

edit: changed backquotes to double quotes

Last edited by chrism01; 12-10-2006 at 04:56 PM.
 
Old 12-08-2006, 09:38 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
your code works for me minus the use warnings clause.

Code:
use Env; # enable you to use $HOME
 
Old 12-08-2006, 10:09 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
At least part of the problem is this line:
Code:
system 'ls -al $home';
Single quotes mean to pass a literal string, but since $home is a variable which you have set in your program, you probably want to have it expanded before it gets passed to ls. Change this line to:
Code:
system "ls -al $home";
By the way, you can get the value of envionrment variables from the %ENV hash. HOME (case sensitive!) is almost always defined in the environment, so you might as well take it from there. For example:
Code:
chdir $ENV{HOME} or die "cannot change directory: $!";
So, all in all, I'd re-write like this:
Code:
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;

my $dir = $ENV{HOME} . "/coding";

chdir $dir or die "cannot change directory to $dir: $!";
open STDOUT, ">ls.out" or die "can't write to ls.out: $!";
open STDERR, ">ls.err" or die "can't write to ls.err: $!";
system "ls -al $dir";

Last edited by matthewg42; 12-08-2006 at 10:10 AM. Reason: removing silly brackets
 
Old 12-10-2006, 05:42 AM   #5
noir911
Member
 
Registered: Apr 2004
Posts: 682

Original Poster
Rep: Reputation: Disabled
Matthew, I tried your code on OpenBSD and it doesn't work. It doesn't give any output & just returns me to the shell ($). I am running it from /home/me.
 
Old 12-10-2006, 06:08 PM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
What do you get when you execute these commands from the shell:
Code:
echo "shell is $SHELL"
echo "home is $HOME"
which perl
perl --version
 
  


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
chdir oldstinkyfish Linux - Software 3 11-14-2004 03:58 AM
about chdir() feetyouwell Programming 3 10-02-2004 12:26 PM
Wine: permission denied to chdir Micro420 Linux - Software 1 11-16-2003 12:06 AM
chdir problem Abe_the_Man Linux - Software 4 08-19-2003 11:39 PM
Unicode chdir sourceman Programming 0 11-04-2002 01:50 AM

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

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