Rookie Perl Question

S

swangdb

I want to run some unix commands from a perl script.

This works, it prints out all lines that don't begin with a # in the
file $ALIASES:

***
$temp = "egrep -v \^# $ALIASES";
system($temp);
***

This doesn't work, it tries to take the egrep output and cut out part
of it:

***
$temp = "egrep -v \^# $ALIASES | cut -d: -f1";
system($temp);
***

I get the following output:

Usage: egrep [OPTION]... PATTERN [FILE]...
Try `egrep --help' for more information.

How can I make this work?

Thanks!
 
E

Eric R. Meyers

swangdb said:
I want to run some unix commands from a perl script.

This works, it prints out all lines that don't begin with a # in the
file $ALIASES:

***
$temp = "egrep -v \^# $ALIASES";
system($temp);
***

This doesn't work, it tries to take the egrep output and cut out part
of it:

***
$temp = "egrep -v \^# $ALIASES | cut -d: -f1";
system($temp);
***

I get the following output:

Usage: egrep [OPTION]... PATTERN [FILE]...
Try `egrep --help' for more information.

How can I make this work?

Thanks!

$aliases = 'some/file/name';

open( FH, $aliases ) || die "opening aliases: $!\n";

while ( <FH> )
{
print if ( ! m/^[#]/ );

} ## end while

close( FH );

## FYI: Buy Perl programming book "Learning Perl" ISBN: 1-56592-284-0.

## The system() call doesn't return output to you.
## Another means is open( FH, "egrep -v ... |" ) which opens a piped FH.
 
S

swangdb

Eric said:
swangdb wrote:
$aliases = 'some/file/name';

open( FH, $aliases ) || die "opening aliases: $!\n";

while ( <FH> )
{
print if ( ! m/^[#]/ );

} ## end while

close( FH );

Eric,

Thanks for showing a real perl way to do this. I received a quick fix
to my command, add one backslash before the existing backslash:

$temp = "egrep -v \\^# $ALIASES | cut -d: -f1";
system($temp);

I actually want to put this output to a file, so I guess system is
okay:

$temp = "egrep -v \\^# $ALIASES | cut -d: -f1 > $outfile";
system($temp);

Thanks again!
 
E

Eric R. Meyers

Tad said:
Eric R. Meyers said:
print if ( ! m/^[#]/ );


That is fine if you are trying to hide what the code does.

In case you might prefer maintainable code instead, then:

print unless /^#/;
That's a style issue, not a function issue. I personally never use
'unless,' unless I've got a really good reason to 'unless.'

'if' and 'while' for a beginner.
 
J

John Bokma

Eric R. Meyers said:
Tad said:
Eric R. Meyers said:
print if ( ! m/^[#]/ );


That is fine if you are trying to hide what the code does.

In case you might prefer maintainable code instead, then:

print unless /^#/;
That's a style issue, not a function issue. I personally never use
'unless,' unless I've got a really good reason to 'unless.'

'if' and 'while' for a beginner.

Nonsense. I strongly recommend to use unless if the if ! construct looks
like a lot of noise as in "if ( ! m/^[#]/ );", especially since it has a
lot of noise overhead ([] not needed, m not needed, () not needed).
 
E

Eric R. Meyers

John said:
Eric R. Meyers said:
Tad said:
print if ( ! m/^[#]/ );


That is fine if you are trying to hide what the code does.

In case you might prefer maintainable code instead, then:

print unless /^#/;
That's a style issue, not a function issue. I personally never use
'unless,' unless I've got a really good reason to 'unless.'

'if' and 'while' for a beginner.

Nonsense. I strongly recommend to use unless if the if ! construct looks
like a lot of noise as in "if ( ! m/^[#]/ );", especially since it has a
lot of noise overhead ([] not needed, m not needed, () not needed).
Hi John! Again, it's a style issue, not a function issue. I like the
noise, because I have an affinity for the apparently meaningless syntax of
Perl. I can read it. We all have very different personal styles and
patterns, as well as very different personal opinions. Right John.
Everything and everyone coexists in this world, and Perl supports both your
way and my way. That's the beauty of Perl, which I'll always advocate.
This beginner understands awk, sed and grep, but he may not know that Perl
can do anything an everything that we can think of doing, because we just
have to create a module to do what we want, if it isn't already there for
us to use for the public good.
 
D

Dr.Ruud

Tad McClellan schreef:
Eric R. Meyers:
print if ( ! m/^[#]/ );

That is fine if you are trying to hide what the code does.

In case you might prefer maintainable code instead, then:

print unless /^#/;

And if you want to put the focus on skipping comment lines:

/^#/ or print ;
 
J

John Bokma

Eric R. Meyers said:
John said:
"Eric R. Meyers" <[email protected]> wrote:
[..]
'if' and 'while' for a beginner.

Nonsense. I strongly recommend to use unless if the if ! construct
looks like a lot of noise as in "if ( ! m/^[#]/ );", especially since
it has a lot of noise overhead ([] not needed, m not needed, () not
needed).
Hi John! Again, it's a style issue, not a function issue.

It's a beginners issue, and I wouldn't teach a beginner how to write lines
and lines containing a lot of noise, certainly not as being "easier". I
respect beginners enough to assume that they are willing to learn how to
code readable.

A statement like "'if' and 'while'" for a beginner" are extremely
degrading.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,088
Latest member
JeremyMedl

Latest Threads

Top