CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Problem with executing shell commands in perl

 



vpadiri
Novice

Jul 6, 2009, 4:41 AM

Post #1 of 15 (1326 views)
Problem with executing shell commands in perl Can't Post

Hi,
I have two files named 'file1' and 'file2', in the same directory in which the below program is also there.

______________________
#!/usr/local/bin/perl -w

$result=`ls file{1,2}`;
print "$result";
______________________

Can any body tell me why I am getting the below error when running the above program.
____________________________
file{1,2}: No such file or directory
____________________________

Any help is greatly appreciated.

Thanks in advance


ichi
User

Jul 6, 2009, 5:08 AM

Post #2 of 15 (1314 views)
Re: [vpadiri] Problem with executing shell commands in perl [In reply to] Can't Post

generally, if you can avoid using shell commands, then don't use them. you are using Perl, so use Perl. For listing directory contents, you can use opendir(), readdir(), or just use while loop

Code
while ( <file[12]>){ 
.....
}



spider
User


Jul 6, 2009, 5:47 AM

Post #3 of 15 (1310 views)
Re: [ichi] Problem with executing shell commands in perl [In reply to] Can't Post

This way of listing files was new to me, but very elegant so I had to try it.
Thank you ichi!

I guess this was the same as vpadiri wanted also.

Code
#!/local/bin/perl -w 
while( <file[12]>) {

print "$_\n" ;

}



shawnhcorey
Enthusiast


Jul 6, 2009, 5:48 AM

Post #4 of 15 (1309 views)
Re: [ichi] Problem with executing shell commands in perl [In reply to] Can't Post


In Reply To
generally, if you can avoid using shell commands, then don't use them. you are using Perl, so use Perl. For listing directory contents, you can use opendir(), readdir(), or just use while loop

Code
while ( <file[12]>){ 
.....
}



I don't think that will work. Try the glob() function instead. See `perldoc -f glob`.

Code
@ARGV = glob( 'file[12]' ); 
while( <> ){
# ...
}


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


ichi
User

Jul 6, 2009, 6:20 AM

Post #5 of 15 (1304 views)
Re: [shawnhcorey] Problem with executing shell commands in perl [In reply to] Can't Post


In Reply To

In Reply To
I don't think that will work.

why don't you try it and see


vpadiri
Novice

Jul 6, 2009, 6:25 AM

Post #6 of 15 (1303 views)
Re: [shawnhcorey] Problem with executing shell commands in perl [In reply to] Can't Post

Thanks a lot for all your replies.

Really nice forum :-)


vpadiri
Novice

Jul 6, 2009, 6:48 AM

Post #7 of 15 (1296 views)
Re: [ichi] Problem with executing shell commands in perl [In reply to] Can't Post

Both angle-brackets and glob function are doing the same.

_________________________
#!/usr/local/bin/perl -w

while(<file.{txt,exe}>) {
print "$_\n";
}

while(glob('file.{txt,exe}')) {
print "$_\n";
}
________________________

Output:
_______
file.txt
file.exe
file.txt
file.exe
_______

But still, just for the sake of curiosity I want to know why my first program did not work.

Thanks


shawnhcorey
Enthusiast


Jul 6, 2009, 6:51 AM

Post #8 of 15 (1293 views)
Re: [ichi] Problem with executing shell commands in perl [In reply to] Can't Post


In Reply To

In Reply To

In Reply To
I don't think that will work.

why don't you try it and see


Oops. You're right. It does work.

One thing I dislike about Perl is that it uses the same syntax to mean two different things.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


1arryb
User

Jul 6, 2009, 12:03 PM

Post #9 of 15 (1277 views)
Re: [shawnhcorey] Problem with executing shell commands in perl [In reply to] Can't Post

Hi shawn,


Quote
One thing I dislike about Perl is that it uses the same syntax to mean two different things.


Heh. One of my standard questions when interviewing applicants for Perl programmer positions is, "What do curly braces ('{}') mean in Perl?" There are, in fact, 5 meanings.

UPDATE: Anyone who gets more than 3 is an immediate hire!

Cheers,

Larry


(This post was edited by 1arryb on Jul 6, 2009, 12:05 PM)


KevinR
Veteran


Jul 6, 2009, 12:31 PM

Post #10 of 15 (1270 views)
Re: [1arryb] Problem with executing shell commands in perl [In reply to] Can't Post

might even be more:

{ block of code}

$hash{hashkey}

print "Foo${scalarname}bar";

$foo = {foo => 'bar'};

@array = @{$array_ref};

print "foo @{[foo()]} bar"; <-- pretty much like above


I can't take less than 80K USD a year to start
-------------------------------------------------


shawnhcorey
Enthusiast


Jul 6, 2009, 12:34 PM

Post #11 of 15 (1268 views)
Re: [1arryb] Problem with executing shell commands in perl [In reply to] Can't Post


In Reply To
Heh. One of my standard questions when interviewing applicants for Perl programmer positions is, "What do curly braces ('{}') mean in Perl?" There are, in fact, 5 meanings.

UPDATE: Anyone who gets more than 3 is an immediate hire!


Well, I thought up of four, so I guess my reputation is safe. Undoubtedly the last will come to me tonight just before I fall asleep and keep me awake for hours :D

I was thinking of things like:

Code
sub foo { 
my $var = shift;
}
{
my $var = shift;
}

The shift does two different things.

UPDATE: I just thought of the last. Thank the Goodness for that. Now I might get some sleep tonight ;)

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".

(This post was edited by shawnhcorey on Jul 6, 2009, 12:54 PM)


1arryb
User

Jul 6, 2009, 12:34 PM

Post #12 of 15 (1268 views)
Re: [KevinR] Problem with executing shell commands in perl [In reply to] Can't Post

Smile


shawnhcorey
Enthusiast


Jul 6, 2009, 12:56 PM

Post #13 of 15 (1263 views)
Re: [KevinR] Problem with executing shell commands in perl [In reply to] Can't Post


In Reply To
might even be more:

{ block of code}

$hash{hashkey}

print "Foo${scalarname}bar";

$foo = {foo => 'bar'};

@array = @{$array_ref};

print "foo @{[foo()]} bar"; <-- pretty much like above


I can't take less than 80K USD a year to start


/\d{3,8}/

I guess I can get 100K.

UPDATE: these are all the same.
qw{ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dev};
s{ \A \s+ }{}msx;
die qq{couldn't open $file: $!\n};

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".

(This post was edited by shawnhcorey on Jul 6, 2009, 1:05 PM)


KevinR
Veteran


Jul 6, 2009, 4:36 PM

Post #14 of 15 (1256 views)
Re: [shawnhcorey] Problem with executing shell commands in perl [In reply to] Can't Post

ahh...good one, the quantifier: /\d{3,8}/

I guess you do deserve 100K Smile
-------------------------------------------------


vpadiri
Novice

Jul 6, 2009, 11:19 PM

Post #15 of 15 (1247 views)
Re: [vpadiri] Problem with executing shell commands in perl [In reply to] Can't Post

I use TCSH to run shell commands.

'file.{txt,exe}' will expand to 'file.txt file.exe' in TCSH.

But this type of file name expansion is not available in SH.

Probably Perl is using SH by default to run shell commands.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives