Does anybody know...

Discussion in 'Perl Misc' started by Ondra, Feb 9, 2004.

  1. Ondra

    Ondra Guest

    Hello,
    does anybody know what is this:

    my $tag
    $line =~ /<(\S+)[^>]*?>/;
    $tag = $1 || ""; /*****exactly this***/
     
    Ondra, Feb 9, 2004
    #1
    1. Advertising

  2. Ondra wrote:
    > Hello,
    > does anybody know what is this:
    >
    > my $tag
    > $line =~ /<(\S+)[^>]*?>/;
    > $tag = $1 || ""; /*****exactly this***/


    Looks like a regex to match htlp tags and extract the body of the tag.
     
    Peter Hickman, Feb 9, 2004
    #2
    1. Advertising

  3. Peter Hickman wrote:

    > Ondra wrote:
    >
    >> Hello,
    >> does anybody know what is this:
    >> my $tag
    >> $line =~ /<(\S+)[^>]*?>/;
    >> $tag = $1 || ""; /*****exactly this***/

    >
    >
    > Looks like a regex to match htlp tags and extract the body of the tag.


    Or even HTML! My fingers are cold this morning
     
    Peter Hickman, Feb 9, 2004
    #3
  4. Ondra <> wrote:

    > Subject: Does anybody know...



    Please put the subject of your article in the Subject of your article.


    > does anybody know what is this:
    >
    > my $tag
    > $line =~ /<(\S+)[^>]*?>/;
    > $tag = $1 || ""; /*****exactly this***/



    I know what it is *not*. It is not Perl code, it has syntax errors.

    It is a bug, you should never use the dollar-digit variables unless
    you have first ensured that the match *succeeded*.

    It is a bug since it tries to parse HTML with pattern matching
    rather than with a real HTML parser. There are a bunch of examples
    in the Perl FAQ of legal HTML that will break it...

    The non-greediness serves no purpose, it is not needed.

    It could be replaced with this:

    my $tag = '';
    $tag = $1 if $line =~ /<(\S+)[^>]*>/;


    The

    $tag = $1 || "";

    is a common idiom for selecting a default value.

    Note that it will do the wrong thing if the data contains "<0>"...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Feb 9, 2004
    #4
  5. Ondra

    Ondra Guest

    Bernard El-Hagin <> wrote in message news:<-tech>...
    > On 2004-02-09, Peter Hickman <> wrote:
    > > Ondra wrote:
    > >> Hello,
    > >> does anybody know what is this:
    > >>
    > >> my $tag
    > >> $line =~ /<(\S+)[^>]*?>/;
    > >> $tag = $1 || ""; /*****exactly this***/

    > >
    > > Looks like a regex to match htlp tags and extract the body of the tag.

    >
    >
    > That doesn't answer the OP's question.
    >
    >
    > $tag = $1 || "";
    >
    >
    > means "set $tag to the value of $1 if the value of $1 is true, otherwise
    > set $tag to the empty string".


    Yes that's it. Thank you
     
    Ondra, Feb 9, 2004
    #5
  6. Ondra

    David Oswald Guest

    "Ondra" <> wrote in message
    news:...
    > Hello,
    > does anybody know what is this:
    >
    > my $tag
    > $line =~ /<(\S+)[^>]*?>/;
    > $tag = $1 || ""; /*****exactly this***/


    Honestly, it's a mistake.
    You should never rely on $1 in any way unless you first ensure that the
    match succeeded. It is entirely possible that $1 may still contain an
    artifact of a previous pattern match, even if the most recent match failed.
    That being the case, you cannot assume that $1 will be 'undef' or 'false' if
    the most recent pattern match failed, for, if there were previously
    successful pattern matches with capturing parens, $1 will still contain the
    results of that successful capture.

    You really should be checking the success of the match before fiddling with
    $1, et. al.
     
    David Oswald, Feb 10, 2004
    #6
    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. Umut Tezduyar
    Replies:
    4
    Views:
    361
    Scott M.
    Sep 29, 2005
  2. Guest
    Replies:
    1
    Views:
    796
    Guest
    Sep 29, 2003
  3. Markus
    Replies:
    5
    Views:
    558
    Roedy Green
    Nov 29, 2005
  4. rakesh
    Replies:
    0
    Views:
    377
    rakesh
    Feb 7, 2006
  5. Replies:
    1
    Views:
    395
Loading...

Share This Page