Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Why do you need to escape a string used as a paramater?

by awohld (Hermit)
on May 24, 2005 at 05:12 UTC ( [id://459805]=perlquestion: print w/replies, xml ) Need Help??

awohld has asked for the wisdom of the Perl Monks concerning the following question:

I have the following code:
my $stream = HTML::TokeParser->new( \$intr_html ) || die "Couldn't reat HTML string $intrado_html";


When the "\" is removed infront of "$intr_html" this doesn't work properly any more.

Why is the "\" needed???

Replies are listed 'Best First'.
Re: Why do you need to escape a string used as a paramater?
by Zaxo (Archbishop) on May 24, 2005 at 05:16 UTC

    That's not an escape as in quotes, it takes a reference to $intr_html. That speeds up parameter passing by removing the need to make a copy.

    After Compline,
    Zaxo

      That speeds up parameter passing by removing the need to make a copy.

      That's only part of the story. It's also, even most of all, the way HTML::TokeParser distinguishes between literal HTML in a string, and a string holding the file name of a file containing the HTML:

      $p = HTML::TokeParser->new( $filename ); $p = HTML::TokeParser->new( $filehandle ); $p = HTML::TokeParser->new( \$document );
      Note cases 1 and 2.

      Choosing this convention instead of the other way around, was indeed the wisest choice with regard to efficiency.

Re: Why do you need to escape a string used as a paramater?
by Dietz (Curate) on May 24, 2005 at 05:26 UTC
    You are passing a reference to tell the constructor to read from its referent.
    If you omit the backslash the constructor thinks you want to pass a filehandle or a filename and this will not work for you most likeley because you don't have a filehandle or filename named $intr_html.

    'perldoc HTML::TokeParser' tells you more ;-)
Re: Why do you need to escape a string used as a paramater?
by monkfan (Curate) on May 24, 2005 at 05:19 UTC
    Why is the "\" needed???
    Because the method is constructed in such a way that it requires a 'scalar reference' to be passed into it.
    Seems to me it's a standard practice in creating a new object.

    Regards,
    Edward
Re: Why do you need to escape a string used as a paramater?
by kprasanna_79 (Hermit) on May 24, 2005 at 05:33 UTC
    Hai awohld,
    The HTML::TokeParser takes argument as scalar reference. To know more about reference please read this, which gives u more idea about the reference.
    --prasanna.k
Re: Why do you need to escape a string used as a paramater?
by Aragorn (Curate) on May 24, 2005 at 08:01 UTC
    The "\" in this case makes a reference to the variable $intr_html. When a "normal" variable is given to HTML::TokeParser::new, it is taken to be a filename to open and read. When a reference is given, it is taken to be a "pointer" to the text in $intr_html to be parsed.

    See perlreftut for a tutorial on references and perlref for all the details.

    Arjen

      Could we consider a reference "\" in Perl the same thing as a "&" in C, for example ?

      Tomtom
        Superficially, yes. "Under water", there's a lot more going on with Perl references, like automatic memory management.

        Arjen

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://459805]
Approved by monkfan
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-25 06:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found