FAQ
Someone posted this regex question which I can't understand for.

perl -e '$_=abc.eeeee.i;
s/(\.\w+)?$/.out/;
print;'

the result is: abceeeeei.out

Why is this?Please help explain it.Thanks!


_____________________________________________________________
FREE Email @ Fadmail.com - http://www.fadmail.com

Search Discussions

  • Chas Owens at Mar 8, 2007 at 2:40 am

    On 3/7/07, Jennifer Foo wrote:
    Someone posted this regex question which I can't understand for.

    perl -e '$_=abc.eeeee.i;
    s/(\.\w+)?$/.out/;
    print;'

    the result is: abceeeeei.out

    Why is this?Please help explain it.Thanks!
    I think you will be less confused if you change the code to

    perl -e '$_="abc.eeeee.i";
    s/(\.\w+)?$/.out/;
    print;'

    abc.eeeee.i is actually 'abc' . 'eeeee' . 'i' or 'abceeeeei'. You
    might want to look up Perl's behaviour with respect to barewords.

    That said the regex replaces either a period followed by one or more
    word characters (A-Z, a-z, 0-9, and _) at the end of the string or
    nothing at the end of a string with ".out".
  • John W. Krahn at Mar 8, 2007 at 2:50 am

    Jennifer Foo wrote:
    Someone posted this regex question which I can't understand for.

    perl -e '$_=abc.eeeee.i;
    s/(\.\w+)?$/.out/;
    print;'

    the result is: abceeeeei.out

    Why is this?Please help explain it.Thanks!
    $_=abc.eeeee.i;

    This is short for:

    $_ = 'abc' . 'eeeee' . 'i';

    Which is the same as saying:

    $_ = 'abceeeeei';


    The substitution:

    s/(\.\w+)?$/.out/;

    Says to match a period (.) followed by one or more 'word' characters (\w+).
    This pattern is enclosed in parentheses and this group should match zero or
    one time. The pattern is anchored to the end of the string. Because the
    pattern will always match, the replacement string '.out' will either be
    appended to the string or replace any (\.\w+) pattern at the end of the string.



    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
  • Oryann9 at Mar 8, 2007 at 4:12 pm

    $_=abc.eeeee.i;

    This is short for:

    $_ = 'abc' . 'eeeee' . 'i';

    Which is the same as saying:

    $_ = 'abceeeeei';
    Why is $_=abc.eeeee.i short for
    $_ = 'abc' . 'eeeee' . 'i';

    Is it b/c each group of characters is a 'token'
    including the periods?

    abc => token
    . => token
    eeeee => token
    . => token
    i => token

    From the Perl CD:
    the lexical analyzer breaks it down into three tokens:
    print, "Hello, world!\n", and the final semicolon/



    ____________________________________________________________________________________
    Sucker-punch spam with award-winning protection.
    Try the free Yahoo! Mail Beta.
    http://advision.webevents.yahoo.com/mailbeta/features_spam.html
  • Chas Owens at Mar 8, 2007 at 5:13 pm
    On 3/8/07, oryann9 wrote:
    snip
    Why is $_=abc.eeeee.i short for
    $_ = 'abc' . 'eeeee' . 'i';
    snip

    from "Programming Perl 3rd Edition":
    bareword
    A word sufficient ambigious to be deemed
    illegal under use strict 'subs'. In the
    absence of that stricture, a bareword is treated
    as if quotes were around it.

    Basically a bareword is anything that matches /[_a-zA-Z][\w\d]*/ and
    does not have enough context around it for the perl interpreter to
    decide what it is. For instance, the FILE in the code below matches
    the bareword pattern, but the context of open and print let the perl
    interpreter know it is actually a file handle*.

    open FILE, '>', $file or die "could not open $file: $!";
    print FILE "FILE is not a bareword";

    But in the case we are talking about the interpreter does not know
    what to do with abc, eeeee, or i. They aren't file handles. They
    aren't functions** being called with no arguments (because the
    interpreter hasn't seen a definition for abc, eeeee, or i yet). So it
    just throws quotes around each of abc, eeeee, and i and moves on.

    Now all of this begs the question "why do barewords exist at all?".
    The short answer is they don't if you use the strict pragma like you
    are supposed to. The longer answer is that Perl is multipurpose
    language. It supports styles and usages that are outside of the
    normal programming scope. One of its many uses is as a commandline
    tool. I use it everyday instead of the common SUS*** utilities grep,
    cut, sort, etc. I don't sit down and write a file to run through the
    interpreter; instead I say things like

    cdparanoia -Q 2>&1 | perl -ane 'print $F[2] if $F[0] eq TOTAL'

    In that environment I often want to cut down the number of characters
    I have to type to get the job done and barewords are one of the things
    Perl lets me use to achieve that goal. Barewords also have uses that
    the strict pragma doesn't ban. Specifically the ones involving
    hashes:

    my %hash = (
    foo => 'foo is a bareword'
    );

    $hash{bar} = 'if the key is a bareword I don't have to quote it';
    $hash{'not this'} = 'this key contains a non-bareword character, so it
    must be quoted';

    I hope this has been helpful to you.

    * but you should be using bareword like file handles anymore either.
    Use IO::File and the "open my $fh, '>', $file" idiom instead.
    ** This is sort of how use constant works, try this at home
    #!/usr/bin/perl

    use strict;
    use warnings;

    sub abc { "abc." }
    sub eeeee { "eeeee." }
    sub i { "i" }

    $_=abc.eeeee.i;

    print "$_\n";

    *** Single Unix Specification, or what used to be known as POSIX
  • Oryann9 at Mar 8, 2007 at 9:43 pm
    Yes I understand now. For some reason I missed the
    missing quotes in the original post and the word token
    came to mind.

    $ perl -MO=Deparse foo.plx
    BEGIN { $^W = 1; }
    use diagnostics;
    sub abc {
    use warnings;
    use strict 'refs';
    'abc.';
    }
    sub eeeee {
    use warnings;
    use strict 'refs';
    'eeeee.';
    }
    sub i {
    use warnings;
    use strict 'refs';
    'i';
    }
    use warnings;
    use strict 'refs';
    $_ = abc() . eeeee() . i();
    print "$_\n";
    foo.plx syntax OK




    ____________________________________________________________________________________
    Need Mail bonding?
    Go to the Yahoo! Mail Q&A for great tips from Yahoo! Answers users.
    http://answers.yahoo.com/dir/?link=list&sid=396546091
  • Jennifer Foo at Mar 8, 2007 at 2:57 am
    $_=abc.eeeee.i;

    This is short for:

    $_ = 'abc' . 'eeeee' . 'i';

    Which is the same as saying:

    $_ = 'abceeeeei';



    Thanks.I never knew that it can write a string like this way.

    _____________________________________________________________
    FREE Email @ Fadmail.com - http://www.fadmail.com
  • Chas Owens at Mar 8, 2007 at 3:45 am

    On 3/7/07, Jennifer Foo wrote:
    $_=abc.eeeee.i;

    This is short for:

    $_ = 'abc' . 'eeeee' . 'i';

    Which is the same as saying:

    $_ = 'abceeeeei';



    Thanks.I never knew that it can write a string like this way.
    You probably shouldn't though. It is a carry over from the earlier
    days of Perl. Modern Perl should use the strict pragma (which bans
    barewords outside of a few special cases).
  • Jeni Zundel at Mar 8, 2007 at 1:09 pm
    I have to say - I am totally enamored with regex. Color me
    'goober'. I just think that is a beautiful, concise, elegant way to
    make a substitution. All of that capability in one short string of
    characters... No if, then, else construct. Just - capture what is
    there; if it matches a .\w then change it to a .out. If there isn't
    a .\w at the end of the string, just append the .out.

    On Mar 7, 2007, at 9:45 PM, Chas Owens wrote:
    On 3/7/07, Jennifer Foo wrote:
    $_=abc.eeeee.i;

    This is short for:

    $_ = 'abc' . 'eeeee' . 'i';

    Which is the same as saying:

    $_ = 'abceeeeei';



    Thanks.I never knew that it can write a string like this way.
    You probably shouldn't though. It is a carry over from the earlier
    days of Perl. Modern Perl should use the strict pragma (which bans
    barewords outside of a few special cases).

    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
    http://learn.perl.org/

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedMar 8, '07 at 2:15a
activeMar 8, '07 at 9:43p
posts9
users5
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase