Perl code explaination help needed!

Discussion in 'Perl Misc' started by keepyourstupidspam@yahoo.co.uk, May 4, 2007.

  1. 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
    , May 4, 2007
    #1
    1. Advertising

  2. Henry Law Guest

    wrote:
    > 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.


    --

    Henry Law Manchester, England
    Henry Law, May 4, 2007
    #2
    1. Advertising

  3. On 4 May 2007 08:11:31 -0700, wrote:

    >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
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, May 4, 2007
    #3
  4. On Fri, 04 May 2007 16:30:21 +0100, Henry Law <>
    wrote:

    >> @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


    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
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, May 4, 2007
    #4
  5. On Fri, 04 May 2007 18:37:18 +0200, Michele Dondi
    <> wrote:

    >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
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, May 4, 2007
    #5
  6. On May 4, 5:37 pm, Michele Dondi <> wrote:

    > my ($new=$_) =~ s/C$/cc/;


    You mean

    (my $new=$_) =~ s/C$/cc/;
    Brian McCauley, May 4, 2007
    #6
  7. On 4 May 2007 10:53:09 -0700, Brian McCauley <>
    wrote:

    >> my ($new=$_) =~ s/C$/cc/;

    >
    >You mean
    >
    > (my $new=$_) =~ s/C$/cc/;


    And what did I write? (... /me rereads better...) Yep, that's what I
    meant.


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, May 4, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. estafford
    Replies:
    3
    Views:
    372
    estafford
    Aug 22, 2003
  2. baumann@pan
    Replies:
    0
    Views:
    314
    baumann@pan
    May 19, 2005
  3. Drew
    Replies:
    4
    Views:
    304
  4. Dave Lenhardt

    Explaination of a bit of code

    Dave Lenhardt, Sep 20, 2008, in forum: Ruby
    Replies:
    6
    Views:
    105
    Brian Candler
    Sep 21, 2008
  5. kim

    Explaination of Javascript code

    kim, Jul 29, 2003, in forum: Javascript
    Replies:
    0
    Views:
    79
Loading...

Share This Page