A doubt about m//'s related variables

Discussion in 'Perl Misc' started by Michele Dondi, Jul 7, 2004.

  1. I have a minor doubt about predefined variables related to pattern
    matching: in particular is it safe to do something like this?

    if ( /\{([\w:]+)\}/ ) {
    do_something($`, $', split /:/, $1);
    }

    I mean: are $`, $' and $1 those coming from the
    *first* pattern matching in all cases?


    Michele
    --
    #!/usr/bin/perl -lp
    BEGIN{*ARGV=do{open $_,q,<,,\$/;$_}}s z^z seek DATA,11,$[;($,
    =ucfirst<DATA>)=~s x .*x q^~ZEX69l^^q,^2$;][@,xe.$, zex,s e1e
    q 1~BEER XX1^q~4761rA67thb ~eex ,s aba m,P..,,substr$&,$.,age
    __END__
    Michele Dondi, Jul 7, 2004
    #1
    1. Advertising

  2. On Wed, 7 Jul 2004, Michele Dondi wrote:

    >I have a minor doubt about predefined variables related to pattern
    >matching: in particular is it safe to do something like this?
    >
    > if ( /\{([\w:]+)\}/ ) {
    > do_something($`, $', split /:/, $1);
    > }
    >
    >I mean: are $`, $' and $1 those coming from the
    >*first* pattern matching in all cases?


    You could have determined that on your own with testing. As it stands,
    yes they are. I'm a little surprised about $` and $', but that's because
    split() doesn't affect them. Had you done:

    if ( /\{([\w:]+)\}/ ) {
    do_something($`, $', $1 =~ /[^:]*/g);
    }

    you would get rather unexpected results.

    I also advise against using $` and $' and $& if you can avoid them.

    --
    Jeff "japhy" Pinyan % How can we ever be the sold short or
    RPI Acacia Brother #734 % the cheated, we who for every service
    RPI Corporation Secretary % have long ago been overpaid?
    http://japhy.perlmonk.org/ %
    http://www.perlmonks.org/ % -- Meister Eckhart
    Jeff 'japhy' Pinyan, Jul 7, 2004
    #2
    1. Advertising

  3. On Wed, 7 Jul 2004 16:38:17 -0400, Jeff 'japhy' Pinyan
    <> wrote:

    >>I mean: are $`, $' and $1 those coming from the
    >>*first* pattern matching in all cases?

    >
    >You could have determined that on your own with testing. As it stands,


    Yes, indeed I could have! :p

    But for once my judgement was to better express my laziness asking on
    clpmisc...

    >yes they are. I'm a little surprised about $` and $', but that's because
    >split() doesn't affect them. Had you done:
    >
    > if ( /\{([\w:]+)\}/ ) {
    > do_something($`, $', $1 =~ /[^:]*/g);
    > }
    >
    >you would get rather unexpected results.


    Well, in this case I'd say "expected unexpected results"!

    >I also advise against using $` and $' and $& if you can avoid them.


    Actually I can hardly remember having used them in more than two or
    three cases before. But in a particular script I have, it happens that
    I "have to", for a particularly relaxed acceptation of "to have to",
    i.e. as it is now, I'm not using them (an the script does work), or
    better, I'm using them in an undoubtedly safe form, but to use them as
    hinted above would really come handy.


    Michele
    --
    you'll see that it shouldn't be so. AND, the writting as usuall is
    fantastic incompetent. To illustrate, i quote:
    - Xah Lee trolling on clpmisc,
    "perl bug File::Basename and Perl's nature"
    Michele Dondi, Jul 8, 2004
    #3
  4. Jeff 'japhy' Pinyan <> writes:

    > On Wed, 7 Jul 2004, Michele Dondi wrote:
    >
    > >I have a minor doubt about predefined variables related to pattern
    > >matching: in particular is it safe to do something like this?
    > >
    > > if ( /\{([\w:]+)\}/ ) {
    > > do_something($`, $', split /:/, $1);
    > > }
    > >
    > >I mean: are $`, $' and $1 those coming from the
    > >*first* pattern matching in all cases?

    >
    > You could have determined that on your own with testing.


    Actually that would only determine if they do in the current version
    of Perl. I suspect it is _unlikely_ that split() will ever change the
    values of $& et al but until and unless it get's put into the
    documentation of split() I wouldn't want to rely on it.

    > I also advise against using $` and $' and $& if you can avoid them.


    Seconded. I also advise against using any of the regex-related
    variables as arguments to a subroutine without double quoting them to
    avoid the undesirable aliasing.

    sub do_something {
    "Unexpected results" =~ /$/;
    print shift,"\n";
    }

    "Expected results" =~ /$/;
    print $`,"\n"; # Expected results
    do_something($`); # Unexpected results
    do_something("$`"); # Expected results

    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\@
    . l___\\
    # ll l\\
    ###LL LL\\
    Brian McCauley, Jul 9, 2004
    #4
    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. doubt related to string pointers.

    , May 16, 2005, in forum: C Programming
    Replies:
    8
    Views:
    349
    Barry Schwarz
    May 22, 2005
  2. Bob Nelson

    doubt about doubt

    Bob Nelson, Jul 28, 2006, in forum: C Programming
    Replies:
    11
    Views:
    604
  3. Replies:
    0
    Views:
    543
  4. Peter Otten
    Replies:
    2
    Views:
    104
    Cousin Stanley
    Aug 10, 2013
  5. Terry Reedy
    Replies:
    0
    Views:
    109
    Terry Reedy
    Aug 10, 2013
Loading...

Share This Page