Perl code explaination help needed!

G

Guest

Hi,

Can someone explain to me what this line of perl is doing.

/(\S*)\.C$/;


it is used in this function


@list = `ls *.C`;

foreach (@list) {
chop;
/(\S*)\.C$/;
rename("$1.C", "$1.cc") || die "Can't rename $1 !\n";


Thank you.
EM
 
H

Henry Law

Can someone explain to me what this line of perl is doing.

/(\S*)\.C$/;
@list = `ls *.C`;

foreach (@list) {
chop;
/(\S*)\.C$/;
rename("$1.C", "$1.cc") || die "Can't rename $1 !\n";

This is the kind of Perl code that exploits the defaults; it can be
confusing to read, because it doesn't look like "sentences" where you
can see what was operated on by what and where the result went. The
line you're querying is a match, operating on the default variable $_
(which is set to each element of @list as, once again, the default in
"foreach"). So it's the same as

$_ =~ m/(\S*)\.C$/;

.... in other words it extracts a non-whitespace string from the front of
a filename like "foo.C" and sets $1 to whatever that string was - "foo"
in this case.
 
M

Michele Dondi

Can someone explain to me what this line of perl is doing.

/(\S*)\.C$/;

Match a sequence of zero or more non-space charachters followed by a
literal dot followed by a C at the end of the string or just before a
newline at the end of the string.
it is used in this function


@list = `ls *.C`;

Get the output of the ls shell command. Probably just a clumsy and non
portable way to do

my @list = glob '*.C';

or roll your own readdir(), etc.
foreach (@list) {
chop;

Old way to do chomp(); anyway, intended to remove a newline at the end
of a string.
/(\S*)\.C$/;

Ditto as above.
rename("$1.C", "$1.cc") || die "Can't rename $1 !\n";

Change the extension of the file from .C to .cc, and -globally- a
horrible and error-prone way to do so.

Personally, I would do

for (glob '*.C') {
my ($new=$_) =~ s/C$/cc/;
rename $_ => $new or
die "Can't rename `$_' to `$new': $!\n";
}


Michele
 
M

Michele Dondi

This is the kind of Perl code that exploits the defaults; it can be
confusing to read, because it doesn't look like "sentences" where you
can see what was operated on by what and where the result went. The

This is the kind of Perl code that makes me want to puke, since it's
clearly aimed at changing the extension of *.C filenames to .cc, but
does so in an error prone way: what if some filename contains any
space? (Ok, unlikely!) Not to mention the other quirks. All this has
*nothing* to do with exploiting the defaults.


Michele
 
M

Michele Dondi

Personally, I would do

for (glob '*.C') {
my ($new=$_) =~ s/C$/cc/;
rename $_ => $new or
die "Can't rename `$_' to `$new': $!\n";
}

Or, with a single statement, (which doesn't seem too much of a
stretch, here)

rename $_ => do { local $_=$_; s/C$/cc/; $_ }
or die "Can't rename `$_': $!\n"
for glob '*.C';


Michele
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top