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



vpadiri
Novice

Jul 6, 2009, 4:41 AM


Views: 5202
Problem with executing shell commands in perl

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


Views: 5190
Re: [vpadiri] Problem with executing shell commands in perl

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


Views: 5186
Re: [ichi] Problem with executing shell commands in perl

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


Views: 5185
Re: [ichi] Problem with executing shell commands in perl


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


Views: 5180
Re: [shawnhcorey] Problem with executing shell commands in perl


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


Views: 5179
Re: [shawnhcorey] Problem with executing shell commands in perl

Thanks a lot for all your replies.

Really nice forum :-)


vpadiri
Novice

Jul 6, 2009, 6:48 AM


Views: 5172
Re: [ichi] Problem with executing shell commands in perl

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


Views: 5169
Re: [ichi] Problem with executing shell commands in perl


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


Views: 5153
Re: [shawnhcorey] Problem with executing shell commands in perl

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


Views: 5146
Re: [1arryb] Problem with executing shell commands in perl

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


Views: 5144
Re: [1arryb] Problem with executing shell commands in perl


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


Views: 5144
Re: [KevinR] Problem with executing shell commands in perl

Smile


shawnhcorey
Enthusiast


Jul 6, 2009, 12:56 PM


Views: 5139
Re: [KevinR] Problem with executing shell commands in perl


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


Views: 5132
Re: [shawnhcorey] Problem with executing shell commands in perl

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

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


vpadiri
Novice

Jul 6, 2009, 11:19 PM


Views: 5123
Re: [vpadiri] Problem with executing shell commands in perl

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.